简体   繁体   English

为什么Guava Resources类没有方法的String版本?

[英]Why does the Guava Resources class not have String versions of the methods?

The construct Resources.toString(Resources.getResource("foo"), Charsets.UTF_8) feels a little cumbersome. 构造Resources.toString(Resources.getResource("foo"), Charsets.UTF_8)有点麻烦。 Why the insistence on conversion to URL's first? 为什么坚持要首先转换到URL? Since the getResource() method doesn't throw an exception, why not have parallel String methods as well? 由于getResource()方法不会引发异常,所以为什么也没有并行的String方法呢?

I'm pretty sure this all comes down to orthogonality and composability. 我很确定这一切都归结为正交性和可组合性。 The API clearly separates getting the URL of a resource from doing something with that resource. 该API明确分离得到URL 做某事与资源的资源的。 This is important because there are a number of ways that you can get the URL of a resource. 这很重要,因为您可以通过多种方式获取资源的URL Resources.getResource("foo") is one, but it just won't work in some situations. Resources.getResource("foo")是其中之一,但是在某些情况下它不起作用。 If you need to ensure that a specific ClassLoader is used (because Guava may be loaded by a different ClassLoader than your application files), you need an alternative way of getting the URL such as Resources.getResource("foo", SomeApplicationClass.class) . 如果您需要确保使用特定的ClassLoader (因为Guava可能由与应用程序文件不同的ClassLoader ),则需要获取URL的另一种方法,例如Resources.getResource("foo", SomeApplicationClass.class)

If Resources were to provide overloads of its methods to handle all those cases, the number of methods in the class would triple. 如果Resources提供其方法的重载来处理所有这些情况,则该类中的方法数量将增加三倍。 That might seem acceptable in this particular case, but if similar "shortcuts" were added throughout the library, the number of methods would balloon up very quickly. 可能在这种特殊情况下似乎可以接受的,但如果类似的“快捷方式”在整个库添加,方法的数量将非常迅速膨胀起来。 The library would become much more difficult to digest because you'd have to dig through a sea of methods that do almost the same thing to find what you want. 该库将变得更加难以消化,因为您必须挖掘大量的方法,这些方法几乎可以完成相同的工作才能找到所需的内容。 For that reason, Guava favors powerful methods that do one thing and which combine well with other methods. 出于这个原因,Guava偏爱功能强大的方法,这些方法可以做一件事,并且可以与其他方法很好地结合在一起。 Combining Resources.toString with Resources.getResource is an example of this. Resources.toStringResources.getResource结合在一起就是一个例子。

Of course, that doesn't mean that Guava never provides such shortcuts... it just only does so when the addition really seems worth it. 当然,这并不意味着Guava 从不提供这样的快捷方式……它只是在真正值得添加时才这样做。 For example, most of the methods in the Files class could be removed since you can just combine Files.newInputStreamSupplier , Files.newWriterSupplier , etc. with the methods in the ByteStreams and CharStreams classes to accomplish the same things. 例如,大多数在方法Files可以被删除,因为你可以结合Files.newInputStreamSupplierFiles.newWriterSupplier等用的方法ByteStreamsCharStreams类来完成同样的事情。 Given how common operations on File s are, though, the shortcuts were deemed worth it. 鉴于对File的常见操作是多么,快捷方式被认为是值得的。 (Note that overloads that take a String filename were not added, though.) (但是请注意, 添加采用String文件名的重载。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM