public class ComparatorPredicate<T> extends Objectimplements Predicate <T>, Serializable
Integer ONE = Integer.valueOf(1);
Integer TWO = Integer.valueOf(2);
Comparator comparator = new Comparator() {
public int compare(Object first, Object second) {
return ((Integer) second) - ((Integer) first);
}
};
Using the declared variables, the
ComparatorPredicate can be used used in the following way:
ComparatorPredicate.comparatorPredicate(ONE, comparator).evaluate(TWO);The input variable
TWO in compared to the stored variable
ONE using the supplied
comparator. This is the default usage of the predicate and will return
true if the underlying comparator returns
0. In addition to the default usage of the predicate, it is possible to evaluate the comparator's result in several ways. The following
ComparatorPredicate.Criterion enumeration values are provided by the predicate:
ComparatorPredicate.comparatorPredicate(ONE, comparator,ComparatorPredicate.Criterion.GREATER).evaluate(TWO);The input variable TWO is compared to the stored variable ONE using the supplied
comparator using the
GREATER evaluation criterion constant. This instructs the predicate to return
true if the comparator returns a value greater than
0.
| Modifier and Type | Class and Description |
|---|---|
static class |
ComparatorPredicate
|
| Constructor and Description |
|---|
ComparatorPredicate(T object, Comparator
Constructor that performs no validation.
|
| Modifier and Type | Method and Description |
|---|---|
static <T> Predicate |
comparatorPredicate(T object, Comparator
Factory to create the comparator predicate
|
static <T> Predicate |
comparatorPredicate(T object, Comparator
Factory to create the comparator predicate
|
boolean |
evaluate(T target)
Evaluates the predicate.
|
public ComparatorPredicate(T object, Comparator<T> comparator, ComparatorPredicate .Criterion criterion)
comparatorPredicate if you want that.
object - the object to compare to
comparator - the comparator to use for comparison
criterion - the criterion to use to evaluate comparison
public static <T> Predicate<T> comparatorPredicate(T object, Comparator <T> comparator)
T - the type that the predicate queries
object - the object to compare to
comparator - the comparator to use for comparison
NullPointerException - if comparator is null
public static <T> Predicate<T> comparatorPredicate(T object, Comparator <T> comparator, ComparatorPredicate .Criterion criterion)
T - the type that the predicate queries
object - the object to compare to
comparator - the comparator to use for comparison
criterion - the criterion to use to evaluate comparison
NullPointerException - if comparator or criterion is null
public boolean evaluate(T target)
true in the following cases:
comparator.compare(object, input) == 0 && criterion == EQUALcomparator.compare(object, input) < 0 && criterion == LESScomparator.compare(object, input) > 0 && criterion == GREATERcomparator.compare(object, input) >= 0 && criterion == GREATER_OR_EQUALcomparator.compare(object, input) <= 0 && criterion == LESS_OR_EQUALevaluate in interface
Predicate<T>
target - the target object to compare to
true if the comparison succeeds according to the selected criterion
IllegalStateException - if the criterion is invalid (really not possible)
Predicate.evaluate(java.lang.Object) ,
Comparator.compare(java.lang.Object first, java.lang.Object second)