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? 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)?
With the Predicate
interface and the various utility methods to filter collections with a Predicate, Guava provides a core you can build upon.
The Predicates
class lets you create some commonly used 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.
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. 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. 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).
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. 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:
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
. ContiguousSet
requires a DiscreteDomain for the type... Guava provides DiscreteDomain.integers()
, .longs()
and .bigIntegers()
at the moment.
With the Predicate
interface and the various utility methods to filter collections with a Predicate, Guava provides a core you can build upon.
The Predicates
class lets you create some commonly used 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.
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. 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. 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).
I have previously requested this functionality and been referred to this issue. Apparently this functionality will be implemented through Ranges, which will implement Predicate
.
Predicates
collects some common predicates but not a greater than one. 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
© 2022 - 2024 — McMap. All rights reserved.