Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 671
Peter Recore Avatar asked Apr 02 '11 17:04

Peter Recore


1 Answers

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.

like image 116
ColinD Avatar answered Sep 21 '22 08:09

ColinD