Class ComparatorPredicate<T>
- java.lang.Object
-
- org.apache.commons.collections4.functors.AbstractPredicate<T>
-
- org.apache.commons.collections4.functors.ComparatorPredicate<T>
-
- Type Parameters:
T- the type of the input to the predicate.
- All Implemented Interfaces:
java.io.Serializable,java.util.function.Predicate<T>,Predicate<T>
public class ComparatorPredicate<T> extends AbstractPredicate<T> implements java.io.Serializable
Predicate that compares the input object with the one stored in the predicate using a comparator. In addition, the comparator result can be evaluated in accordance to a supplied criterion value.In order to demonstrate the use of the predicate, the following variables are declared:
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
ComparatorPredicatecan be used in the following way:ComparatorPredicate.comparatorPredicate(ONE, comparator).test(TWO);
The input variable
TWOin compared to the stored variableONEusing the suppliedcomparator. This is the default usage of the predicate and will returntrueif the underlying comparator returns0. In addition to the default usage of the predicate, it is possible to evaluate the comparator's result in several ways. The followingComparatorPredicate.Criterionenumeration values are provided by the predicate:- EQUAL
- GREATER
- GREATER_OR_EQUAL
- LESS
- LESS_OR_EQUAL
The following examples demonstrates how these constants can be used in order to manipulate the evaluation of a comparator result.
ComparatorPredicate.comparatorPredicate(ONE, comparator,ComparatorPredicate.Criterion.GREATER).test(TWO);
The input variable TWO is compared to the stored variable ONE using the supplied
comparatorusing theGREATERevaluation criterion constant. This instructs the predicate to returntrueif the comparator returns a value greater than0.- Since:
- 4.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classComparatorPredicate.CriterionEnumerates the comparator criteria.
-
Constructor Summary
Constructors Constructor Description ComparatorPredicate(T object, java.util.Comparator<T> comparator, ComparatorPredicate.Criterion criterion)Constructor that performs no validation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> Predicate<T>comparatorPredicate(T object, java.util.Comparator<T> comparator)Creates the comparator predicatestatic <T> Predicate<T>comparatorPredicate(T object, java.util.Comparator<T> comparator, ComparatorPredicate.Criterion criterion)Creates the comparator predicatebooleantest(T target)Evaluates the predicate.-
Methods inherited from class org.apache.commons.collections4.functors.AbstractPredicate
evaluate
-
-
-
-
Constructor Detail
-
ComparatorPredicate
public ComparatorPredicate(T object, java.util.Comparator<T> comparator, ComparatorPredicate.Criterion criterion)
Constructor that performs no validation. UsecomparatorPredicateif you want that.- Parameters:
object- the object to compare tocomparator- the comparator to use for comparisoncriterion- the criterion to use to evaluate comparison
-
-
Method Detail
-
comparatorPredicate
public static <T> Predicate<T> comparatorPredicate(T object, java.util.Comparator<T> comparator)
Creates the comparator predicate- Type Parameters:
T- the type that the predicate queries- Parameters:
object- the object to compare tocomparator- the comparator to use for comparison- Returns:
- the predicate
- Throws:
java.lang.NullPointerException- if comparator is null
-
comparatorPredicate
public static <T> Predicate<T> comparatorPredicate(T object, java.util.Comparator<T> comparator, ComparatorPredicate.Criterion criterion)
Creates the comparator predicate- Type Parameters:
T- the type that the predicate queries- Parameters:
object- the object to compare tocomparator- the comparator to use for comparisoncriterion- the criterion to use to evaluate comparison- Returns:
- the predicate
- Throws:
java.lang.NullPointerException- if comparator or criterion is null
-
test
public boolean test(T target)
Evaluates the predicate. The predicate evaluates totruein 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_EQUAL
- Specified by:
testin interfacejava.util.function.Predicate<T>- Specified by:
testin interfacePredicate<T>- Parameters:
target- the target object to compare to- Returns:
trueif the comparison succeeds according to the selected criterion- Throws:
java.lang.IllegalStateException- if the criterion is invalid (really not possible)- See Also:
Predicate.test(Object),Comparator.compare(Object first, Object second)
-
-