8

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

0

4 Answers 4

15

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

Ranges 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 Integers 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.

0
7

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

2
  • Yeah, i guess since they wrote it they get to decide what's "bloat" and what's core. As far as the one liner argument goes, I would argue that you don't need all 3 logical operations (and, or, not) when those can all be implemented in one line as combinations of "nand" predicates :) Apr 5, 2011 at 15:12
  • 1
    Good point. Actually, I think I sounded kind of harsh when I said it would be "API bloat" to add such predicates. I believe these would be quite useful. I really like the idea of having a "Range" class that produces these predicates, though (see Sean Patrick Floyd's answer). It would also work for intervals, and so on. Apr 5, 2011 at 21:34
7

I have previously requested this functionality and been referred to this issue. Apparently this functionality will be implemented through Ranges, which will implement Predicate.

0
1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.