Skip to content
pyricau edited this page Oct 18, 2011 · 3 revisions

Detailed example

The classical way to use a Guava Predicate:

List<Integer> values = Arrays.asList(16, 21);
Predicate<Integer> minorPredicate = new Predicate<Integer>() {
	@Override
	public boolean apply(Integer input) {
		return input < 18;
	}
};
Iterable<Integer> minors = Iterables.filter(values, minorPredicate);
System.out.println(minors); // prints [16]

With FunkyJFunctional our predicate is much shorter:

List<Integer> values = Arrays.asList(16, 21);
class Minor extends Pred<Integer> {{ out = in < 18; }}
Iterable<Integer> minors = Iterables.filter(values, FunkyGuava.withPred(Minor.class));
System.out.println(minors); // prints [16]

With static imports, it's even shorter:

import static info.piwai.funkyjfunctional.FunkyGuava.*;
import static com.google.common.collect.Iterables.*;
import static java.util.Arrays.*;
// [...]
class Minor extends Pred<Integer> {{ out = in < 18; }}
Iterable<Integer> minors = filter(asList(16, 21), withPred(Minor.class));
System.out.println(minors); // prints [16]

And... much more!

Sounds funky? We think it is ;-).

Clone this wiki locally