[英]Where to find basic Predicates like greaterThan for Guava?
I am using the guava library and have noticed that a very useful Predicate is not defined - "greater than". 我正在使用番石榴库,并注意到一个非常有用的谓词没有定义 - “大于”。 Is there another place I should be looking for basic predicates like this, or am I doomed to create my own functional support jar that includes things like this, and import it into all of my projects?
还有其他地方我应该寻找像这样的基本谓词,还是我注定要创建我自己的功能支持jar包含这样的东西,并将其导入我的所有项目? Is there a reason they wouldn't include this,but would take the time to do a bunch of other predicates (In the Predicates class)?
有没有理由他们不会包含这个,但会花时间去做一堆其他的谓词(在Predicates类中)?
Range and Ranges (update: the static methods on Ranges
have been folded into Range
as of Guava 14.0) have now been added for r10. 范围和
范围 (更新:在静态方法Ranges
已被折叠成Range
为番石榴14.0的)现在已经被添加为R10。 You'll be able to just do: 你可以做到:
Iterable<Integer> positive = Iterables.filter(numbers, Range.greaterThan(0));
Range
s have a lot of other powerful functionality, including the ability to view a Range
as a contiguous ImmutableSortedSet
over a discrete domain: Range
具有许多其他强大的功能,包括在一个离散域上查看Range
作为连续的ImmutableSortedSet
的能力:
ContiguousSet<Integer> oneToOneHundred = ContiguousSet.create(
Range.closed(1, 100), DiscreteDomains.integers());
I just showed Integer
s here, but the Range
stuff works for any Comparable
. 我刚刚在这里展示了
Integer
,但Range
东西适用于任何Comparable
。 ContiguousSet
requires a DiscreteDomain for the type... Guava provides DiscreteDomain.integers()
, .longs()
and .bigIntegers()
at the moment. ContiguousSet
需要一个DiscreteDomain类型... Guava目前提供DiscreteDomain.integers()
.longs()
和.bigIntegers()
。
With the Predicate
interface and the various utility methods to filter collections with a Predicate, Guava provides a core you can build upon. 使用
Predicate
接口和各种实用程序方法来过滤使用Predicate的集合,Guava提供了一个可以构建的核心。
The Predicates
class lets you create some commonly used predicates. Predicates
类允许您创建一些常用的谓词。 I guess you could do a request for enhancement in the issue tracker, as suggested by Mike, but I'm not sure they would add it, since Guava strives for a high power-to-weight ratio. 我猜你可以按照迈克的建议在问题跟踪器上做一个增强请求,但是我不确定他们是否会添加它,因为Guava正在努力实现高功率重量比。
If they were to add the "greaterThan" predicate, they would also need to add "greaterOrEqualThan", "lesserThan", "lesserOrEqualThan"... This would be useful, but this is a lot of "API bloat" for a Predicate that only takes one line to implement. 如果他们要添加“greaterThan”谓词,他们还需要添加“greaterOrEqualThan”,“lessserThan”,“lesserOrEqualThan”......这将是有用的,但这对于谓词来说是很多“API膨胀”只需要一行来实现。 Worth a try, though.
值得一试。
A better solution might be to have an open-source project that extends Guava with all the "nice-to-have" functionality that is not available in Guava proper. 一个更好的解决方案可能是拥有一个开源项目,扩展Guava,具有Guava本身无法提供的所有“好用”功能。 We could call it "guava-leftovers" or something ;) Or maybe ask the Biscotti project to add such utility methods (they already have some "nice-to-have" functionality that's not in Guava).
我们可以把它称为“guava-leftovers”或其他东西;)或者可能要求Biscotti项目添加这样的实用程序方法(它们已经有一些“很好的”功能,而不是Guava)。
I have previously requested this functionality and been referred to this issue . 我之前已经请求过这个功能并且已经引用了这个问题 。 Apparently this functionality will be implemented through Ranges, which will implement
Predicate
. 显然,这个功能将通过Ranges实现,Ranges将实现
Predicate
。
Predicates
collects some common predicates but not a greater than one. Predicates
收集一些常见谓词但不大于一个。 I don't think guava provides such a thing. 我不认为番石榴提供这样的东西。 You can try filing a bug/feature request at the guava project site : http://code.google.com/p/guava-libraries/issues/list
您可以尝试在番石榴项目网站上提交错误/功能请求: http : //code.google.com/p/guava-libraries/issues/list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.