Search code examples
javaguavapredicates

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? 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)?


Solution

  • 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).