Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.functors

public Class ComparatorPredicate<T>

extends Object
implements Predicate<T>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.collections4.Predicate
Imports
java.io.Serializable, java.util.Comparator, org.apache.commons.collections4.Predicate

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 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 Criterion enumeration values are provided by the predicate:

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

Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private final Comparator<T>
comparator

The comparator to use for comparison

private final ComparatorPredicate.Criterion
criterion

The comparison evaluation criterion to use

private final T
object

The internal object to compare with

private static final long

Constructor Summary

AccessConstructor and Description
public
ComparatorPredicate(final T
the object to compare to
object
,
final Comparator<T>
the comparator to use for comparison
comparator
,
final ComparatorPredicate.Criterion
the criterion to use to evaluate comparison
criterion
)

Constructor that performs no validation.

Method Summary

Modifier and TypeMethod and Description
public static <
the type that the predicate queries
T
>
Predicate<T>

Returns:

the predicate
comparatorPredicate
(final T
the object to compare to
object
,
final Comparator<T>
the comparator to use for comparison
comparator
)

Factory to create the comparator predicate

public static <
the type that the predicate queries
T
>
Predicate<T>

Returns:

the predicate
comparatorPredicate
(final T
the object to compare to
object
,
final Comparator<T>
the comparator to use for comparison
comparator
,
final ComparatorPredicate.Criterion
the criterion to use to evaluate comparison
criterion
)

Factory to create the comparator predicate

public boolean

Returns:

true if the comparison succeeds according to the selected criterion
evaluate
(final T
the target object to compare to
target
)

Implements org.apache.commons.collections4.Predicate.evaluate.

Evaluates the predicate.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorback to summary
private final Comparator<T> comparator

The comparator to use for comparison

criterionback to summary
private final ComparatorPredicate.Criterion criterion

The comparison evaluation criterion to use

objectback to summary
private final T object

The internal object to compare with

serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

ComparatorPredicateback to summary
public ComparatorPredicate(final T object, final Comparator<T> comparator, final ComparatorPredicate.Criterion criterion)

Constructor that performs no validation. Use comparatorPredicate if you want that.

Parameters
object:T

the object to compare to

comparator:Comparator<T>

the comparator to use for comparison

criterion:ComparatorPredicate.Criterion

the criterion to use to evaluate comparison

Method Detail

comparatorPredicateback to summary
public static <T> Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator)

Factory to create the comparator predicate

Parameters
<T>
the type that the predicate queries
object:T

the object to compare to

comparator:Comparator<T>

the comparator to use for comparison

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if comparator is null
comparatorPredicateback to summary
public static <T> Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator, final ComparatorPredicate.Criterion criterion)

Factory to create the comparator predicate

Parameters
<T>
the type that the predicate queries
object:T

the object to compare to

comparator:Comparator<T>

the comparator to use for comparison

criterion:ComparatorPredicate.Criterion

the criterion to use to evaluate comparison

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if comparator or criterion is null
evaluateback to summary
public boolean evaluate(final T target)

Implements org.apache.commons.collections4.Predicate.evaluate.

Evaluates the predicate. The predicate evaluates to true in the following cases:

  • comparator.compare(object, input) == 0 && criterion == EQUAL
  • comparator.compare(object, input) < 0 && criterion == LESS
  • comparator.compare(object, input) > 0 && criterion == GREATER
  • comparator.compare(object, input) >= 0 && criterion == GREATER_OR_EQUAL
  • comparator.compare(object, input) <= 0 && criterion == LESS_OR_EQUAL
Parameters
target:T

the target object to compare to

Returns:boolean

true if the comparison succeeds according to the selected criterion

Annotations
@Override
Exceptions
IllegalStateException:
if the criterion is invalid (really not possible)
See Also
org.apache.commons.collections4.Predicate#evaluate(java.lang.Object), java.util.Comparator#compare(java.lang.Object first, java.lang.Object second)
org.apache.commons.collections4.functors back to summary

public final Enum ComparatorPredicate.Criterion

extends Enum<ComparatorPredicate.Criterion>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ComparatorPredicate.Criterion
public static final ComparatorPredicate.Criterion
public static final ComparatorPredicate.Criterion
public static final ComparatorPredicate.Criterion
public static final ComparatorPredicate.Criterion

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ComparatorPredicate.Criterion
public static ComparatorPredicate.Criterion[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

EQUALback to summary
public static final ComparatorPredicate.Criterion EQUAL
GREATERback to summary
public static final ComparatorPredicate.Criterion GREATER
GREATER_OR_EQUALback to summary
public static final ComparatorPredicate.Criterion GREATER_OR_EQUAL
LESSback to summary
public static final ComparatorPredicate.Criterion LESS
LESS_OR_EQUALback to summary
public static final ComparatorPredicate.Criterion LESS_OR_EQUAL

Constructor Detail

Criterionback to summary
private Criterion()

Method Detail

valueOfback to summary
public static ComparatorPredicate.Criterion valueOf(String name)
valuesback to summary
public static ComparatorPredicate.Criterion[] values()