Top Description Constructors Methods
org.apache.commons.collections4

public Class PredicateUtils

extends Object
Class Inheritance
Imports
java.util.Collection, org.apache.commons.collections4.functors.AllPredicate, .AndPredicate, .AnyPredicate, .EqualPredicate, .ExceptionPredicate, .FalsePredicate, .IdentityPredicate, .InstanceofPredicate, .InvokerTransformer, .NonePredicate, .NotNullPredicate, .NotPredicate, .NullIsExceptionPredicate, .NullIsFalsePredicate, .NullIsTruePredicate, .NullPredicate, .OnePredicate, .OrPredicate, .TransformedPredicate, .TransformerPredicate, .TruePredicate, .UniquePredicate

PredicateUtils provides reference implementations and utilities for the Predicate functor interface. The supplied predicates are: All the supplied predicates are Serializable.
Since
3.0

Constructor Summary

AccessConstructor and Description
private
PredicateUtils()

This class is not normally instantiated.

Method Summary

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

Returns:

the all predicate
allPredicate
(final Predicate<? super T>...
an array of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true only if all of the specified predicates are true.

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

Returns:

the all predicate
allPredicate
(final Collection<? extends Predicate<? super T>>
a collection of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true only if all of the specified predicates are true.

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

Returns:

the and predicate
andPredicate
(final Predicate<? super T>
the first predicate, may not be null
predicate1
,
final Predicate<? super T>
the second predicate, may not be null
predicate2
)

Create a new Predicate that returns true only if both of the specified predicates are true.

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

Returns:

the any predicate
anyPredicate
(final Predicate<? super T>...
an array of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true if any of the specified predicates are true.

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

Returns:

the any predicate
anyPredicate
(final Collection<? extends Predicate<? super T>>
a collection of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true if any of the specified predicates are true.

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

Returns:

the transformer wrapping predicate
asPredicate
(final Transformer<? super T, Boolean>
the transformer to wrap, may not be null
transformer
)

Create a new Predicate that wraps a Transformer.

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

Returns:

the either predicate
eitherPredicate
(final Predicate<? super T>
the first predicate, may not be null
predicate1
,
final Predicate<? super T>
the second predicate, may not be null
predicate2
)

Create a new Predicate that returns true if one, but not both, of the specified predicates are true.

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

Returns:

the predicate
equalPredicate
(final T
the value to compare against
value
)

Creates a Predicate that checks if the input object is equal to the specified object using equals().

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

Returns:

the predicate
exceptionPredicate
()

Gets a Predicate that always throws an exception.

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

Returns:

the predicate
falsePredicate
()

Gets a Predicate that always returns false.

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

Returns:

the predicate
identityPredicate
(final T
the value to compare against
value
)

Creates a Predicate that checks if the input object is equal to the specified object by identity.

public static Predicate<Object>

Returns:

the predicate
instanceofPredicate
(final Class<?>
the type to check for, may not be null
type
)

Creates a Predicate that checks if the object passed in is of a particular type, using instanceof.

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

Returns:

the predicate
invokerPredicate
(final String
the method name to call on the input object, may not be null
methodName
)

Creates a Predicate that invokes a method on the input object.

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

Returns:

the predicate
invokerPredicate
(final String
the method name to call on the input object, may not be null
methodName
,
final Class<?>[]
the parameter types
paramTypes
,
final Object[]
the arguments
args
)

Creates a Predicate that invokes a method on the input object.

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

Returns:

the neither predicate
neitherPredicate
(final Predicate<? super T>
the first predicate, may not be null
predicate1
,
final Predicate<? super T>
the second predicate, may not be null
predicate2
)

Create a new Predicate that returns true if neither of the specified predicates are true.

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

Returns:

the none predicate
nonePredicate
(final Predicate<? super T>...
an array of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true if none of the specified predicates are true.

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

Returns:

the none predicate
nonePredicate
(final Collection<? extends Predicate<? super T>>
a collection of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true if none of the specified predicates are true.

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

Returns:

the predicate
notNullPredicate
()

Gets a Predicate that checks if the input object passed in is not null.

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

Returns:

the not predicate
notPredicate
(final Predicate<? super T>
the predicate to not
predicate
)

Create a new Predicate that returns true if the specified predicate returns false and vice versa.

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

Returns:

the predicate
nullIsExceptionPredicate
(final Predicate<? super T>
the predicate to wrap, may not be null
predicate
)

Gets a Predicate that throws an exception if the input object is null, otherwise it calls the specified Predicate.

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

Returns:

the predicate
nullIsFalsePredicate
(final Predicate<? super T>
the predicate to wrap, may not be null
predicate
)

Gets a Predicate that returns false if the input object is null, otherwise it calls the specified Predicate.

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

Returns:

the predicate
nullIsTruePredicate
(final Predicate<? super T>
the predicate to wrap, may not be null
predicate
)

Gets a Predicate that returns true if the input object is null, otherwise it calls the specified Predicate.

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

Returns:

the predicate
nullPredicate
()

Gets a Predicate that checks if the input object passed in is null.

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

Returns:

the one predicate
onePredicate
(final Predicate<? super T>...
an array of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true if only one of the specified predicates are true.

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

Returns:

the one predicate
onePredicate
(final Collection<? extends Predicate<? super T>>
a collection of predicates to check, may not be null
predicates
)

Create a new Predicate that returns true if only one of the specified predicates are true.

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

Returns:

the or predicate
orPredicate
(final Predicate<? super T>
the first predicate, may not be null
predicate1
,
final Predicate<? super T>
the second predicate, may not be null
predicate2
)

Create a new Predicate that returns true if either of the specified predicates are true.

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

Returns:

the predicate
transformedPredicate
(final Transformer<? super T, ? extends T>
the transformer to call first
transformer
,
final Predicate<? super T>
the predicate to call with the result of the transform
predicate
)

Creates a predicate that transforms the input object before passing it to the predicate.

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

Returns:

the predicate
truePredicate
()

Gets a Predicate that always returns true.

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

Returns:

the predicate
uniquePredicate
()

Creates a Predicate that returns true the first time an object is encountered, and false if the same object is received again.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

PredicateUtilsback to summary
private PredicateUtils()

This class is not normally instantiated.

Method Detail

allPredicateback to summary
public static <T> Predicate<T> allPredicate(final Predicate<? super T>... predicates)

Create a new Predicate that returns true only if all of the specified predicates are true. If the array of predicates is empty, then this predicate returns true.

Parameters
<T>
the type that the predicate queries
predicates:Predicate<? super T>[]

an array of predicates to check, may not be null

Returns:Predicate<T>

the all predicate

Exceptions
NullPointerException:
  • if the predicates array is null
  • if any predicate in the array is null
See Also
AllPredicate
allPredicateback to summary
public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates)

Create a new Predicate that returns true only if all of the specified predicates are true. The predicates are checked in iterator order. If the collection of predicates is empty, then this predicate returns true.

Parameters
<T>
the type that the predicate queries
predicates:Collection<? extends Predicate<? super T>>

a collection of predicates to check, may not be null

Returns:Predicate<T>

the all predicate

Exceptions
NullPointerException:
  • if the predicates collection is null
  • if any predicate in the collection is null
See Also
AllPredicate
andPredicateback to summary
public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2)

Create a new Predicate that returns true only if both of the specified predicates are true.

Parameters
<T>
the type that the predicate queries
predicate1:Predicate<? super T>

the first predicate, may not be null

predicate2:Predicate<? super T>

the second predicate, may not be null

Returns:Predicate<T>

the and predicate

Exceptions
NullPointerException:
if either predicate is null
See Also
AndPredicate
anyPredicateback to summary
public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates)

Create a new Predicate that returns true if any of the specified predicates are true. If the array of predicates is empty, then this predicate returns false.

Parameters
<T>
the type that the predicate queries
predicates:Predicate<? super T>[]

an array of predicates to check, may not be null

Returns:Predicate<T>

the any predicate

Exceptions
NullPointerException:
  • if the predicates array is null
  • if any predicate in the array is null
See Also
AnyPredicate
anyPredicateback to summary
public static <T> Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>> predicates)

Create a new Predicate that returns true if any of the specified predicates are true. The predicates are checked in iterator order. If the collection of predicates is empty, then this predicate returns false.

Parameters
<T>
the type that the predicate queries
predicates:Collection<? extends Predicate<? super T>>

a collection of predicates to check, may not be null

Returns:Predicate<T>

the any predicate

Exceptions
NullPointerException:
  • if the predicates collection is null
  • if any predicate in the collection is null
See Also
AnyPredicate
asPredicateback to summary
public static <T> Predicate<T> asPredicate(final Transformer<? super T, Boolean> transformer)

Create a new Predicate that wraps a Transformer. The Transformer must return either Boolean.TRUE or Boolean.FALSE otherwise a PredicateException will be thrown.

Parameters
<T>
the type that the predicate queries
transformer:Transformer<? super T, Boolean>

the transformer to wrap, may not be null

Returns:Predicate<T>

the transformer wrapping predicate

Exceptions
NullPointerException:
if the transformer is null
See Also
TransformerPredicate
eitherPredicateback to summary
public static <T> Predicate<T> eitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2)

Create a new Predicate that returns true if one, but not both, of the specified predicates are true. XOR

Parameters
<T>
the type that the predicate queries
predicate1:Predicate<? super T>

the first predicate, may not be null

predicate2:Predicate<? super T>

the second predicate, may not be null

Returns:Predicate<T>

the either predicate

Exceptions
NullPointerException:
if either predicate is null
See Also
OnePredicate
equalPredicateback to summary
public static <T> Predicate<T> equalPredicate(final T value)

Creates a Predicate that checks if the input object is equal to the specified object using equals().

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

the value to compare against

Returns:Predicate<T>

the predicate

See Also
EqualPredicate
exceptionPredicateback to summary
public static <T> Predicate<T> exceptionPredicate()

Gets a Predicate that always throws an exception. This could be useful during testing as a placeholder.

Parameters
<T>
the type that the predicate queries
Returns:Predicate<T>

the predicate

See Also
ExceptionPredicate
falsePredicateback to summary
public static <T> Predicate<T> falsePredicate()

Gets a Predicate that always returns false.

Parameters
<T>
the type that the predicate queries
Returns:Predicate<T>

the predicate

See Also
FalsePredicate
identityPredicateback to summary
public static <T> Predicate<T> identityPredicate(final T value)

Creates a Predicate that checks if the input object is equal to the specified object by identity.

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

the value to compare against

Returns:Predicate<T>

the predicate

See Also
IdentityPredicate
instanceofPredicateback to summary
public static Predicate<Object> instanceofPredicate(final Class<?> type)

Creates a Predicate that checks if the object passed in is of a particular type, using instanceof. A null input object will return false.

Parameters
type:Class<?>

the type to check for, may not be null

Returns:Predicate<Object>

the predicate

Exceptions
NullPointerException:
if the class is null
See Also
InstanceofPredicate
invokerPredicateback to summary
public static <T> Predicate<T> invokerPredicate(final String methodName)

Creates a Predicate that invokes a method on the input object. The method must return either a boolean or a non-null Boolean, and have no parameters. If the input object is null, a PredicateException is thrown.

For example, PredicateUtils.invokerPredicate("isEmpty"); will call the isEmpty method on the input object to determine the predicate result.

Parameters
<T>
the type that the predicate queries
methodName:String

the method name to call on the input object, may not be null

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if the methodName is null.
See Also
InvokerTransformer, TransformerPredicate
invokerPredicateback to summary
public static <T> Predicate<T> invokerPredicate(final String methodName, final Class<?>[] paramTypes, final Object[] args)

Creates a Predicate that invokes a method on the input object. The method must return either a boolean or a non-null Boolean, and have no parameters. If the input object is null, a PredicateException is thrown.

For example, PredicateUtils.invokerPredicate("isEmpty"); will call the isEmpty method on the input object to determine the predicate result.

Parameters
<T>
the type that the predicate queries
methodName:String

the method name to call on the input object, may not be null

paramTypes:Class<?>[]

the parameter types

args:Object[]

the arguments

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if the method name is null
IllegalArgumentException:
if the paramTypes and args don't match
See Also
InvokerTransformer, TransformerPredicate
neitherPredicateback to summary
public static <T> Predicate<T> neitherPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2)

Create a new Predicate that returns true if neither of the specified predicates are true.

Parameters
<T>
the type that the predicate queries
predicate1:Predicate<? super T>

the first predicate, may not be null

predicate2:Predicate<? super T>

the second predicate, may not be null

Returns:Predicate<T>

the neither predicate

Exceptions
NullPointerException:
if either predicate is null
See Also
NonePredicate
nonePredicateback to summary
public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates)

Create a new Predicate that returns true if none of the specified predicates are true. If the array of predicates is empty, then this predicate returns true.

Parameters
<T>
the type that the predicate queries
predicates:Predicate<? super T>[]

an array of predicates to check, may not be null

Returns:Predicate<T>

the none predicate

Exceptions
NullPointerException:
  • if the predicates array is null
  • if any predicate in the array is null
See Also
NonePredicate
nonePredicateback to summary
public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates)

Create a new Predicate that returns true if none of the specified predicates are true. The predicates are checked in iterator order. If the collection of predicates is empty, then this predicate returns true.

Parameters
<T>
the type that the predicate queries
predicates:Collection<? extends Predicate<? super T>>

a collection of predicates to check, may not be null

Returns:Predicate<T>

the none predicate

Exceptions
NullPointerException:
  • if the predicates collection is null
  • if any predicate in the collection is null
See Also
NonePredicate
notNullPredicateback to summary
public static <T> Predicate<T> notNullPredicate()

Gets a Predicate that checks if the input object passed in is not null.

Parameters
<T>
the type that the predicate queries
Returns:Predicate<T>

the predicate

See Also
NotNullPredicate
notPredicateback to summary
public static <T> Predicate<T> notPredicate(final Predicate<? super T> predicate)

Create a new Predicate that returns true if the specified predicate returns false and vice versa.

Parameters
<T>
the type that the predicate queries
predicate:Predicate<? super T>

the predicate to not

Returns:Predicate<T>

the not predicate

Exceptions
NullPointerException:
if the predicate is null
See Also
NotPredicate
nullIsExceptionPredicateback to summary
public static <T> Predicate<T> nullIsExceptionPredicate(final Predicate<? super T> predicate)

Gets a Predicate that throws an exception if the input object is null, otherwise it calls the specified Predicate. This allows null handling behaviour to be added to Predicates that don't support nulls.

Parameters
<T>
the type that the predicate queries
predicate:Predicate<? super T>

the predicate to wrap, may not be null

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if the predicate is null.
See Also
NullIsExceptionPredicate
nullIsFalsePredicateback to summary
public static <T> Predicate<T> nullIsFalsePredicate(final Predicate<? super T> predicate)

Gets a Predicate that returns false if the input object is null, otherwise it calls the specified Predicate. This allows null handling behaviour to be added to Predicates that don't support nulls.

Parameters
<T>
the type that the predicate queries
predicate:Predicate<? super T>

the predicate to wrap, may not be null

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if the predicate is null.
See Also
NullIsFalsePredicate
nullIsTruePredicateback to summary
public static <T> Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate)

Gets a Predicate that returns true if the input object is null, otherwise it calls the specified Predicate. This allows null handling behaviour to be added to Predicates that don't support nulls.

Parameters
<T>
the type that the predicate queries
predicate:Predicate<? super T>

the predicate to wrap, may not be null

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if the predicate is null.
See Also
NullIsTruePredicate
nullPredicateback to summary
public static <T> Predicate<T> nullPredicate()

Gets a Predicate that checks if the input object passed in is null.

Parameters
<T>
the type that the predicate queries
Returns:Predicate<T>

the predicate

See Also
NullPredicate
onePredicateback to summary
public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates)

Create a new Predicate that returns true if only one of the specified predicates are true. If the array of predicates is empty, then this predicate returns false.

Parameters
<T>
the type that the predicate queries
predicates:Predicate<? super T>[]

an array of predicates to check, may not be null

Returns:Predicate<T>

the one predicate

Exceptions
NullPointerException:
  • if the predicates array is null
  • if any predicate in the array is null
See Also
OnePredicate
onePredicateback to summary
public static <T> Predicate<T> onePredicate(final Collection<? extends Predicate<? super T>> predicates)

Create a new Predicate that returns true if only one of the specified predicates are true. The predicates are checked in iterator order. If the collection of predicates is empty, then this predicate returns false.

Parameters
<T>
the type that the predicate queries
predicates:Collection<? extends Predicate<? super T>>

a collection of predicates to check, may not be null

Returns:Predicate<T>

the one predicate

Exceptions
NullPointerException:
  • if the predicates collection is null
  • if any predicate in the collection is null
See Also
OnePredicate
orPredicateback to summary
public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2)

Create a new Predicate that returns true if either of the specified predicates are true.

Parameters
<T>
the type that the predicate queries
predicate1:Predicate<? super T>

the first predicate, may not be null

predicate2:Predicate<? super T>

the second predicate, may not be null

Returns:Predicate<T>

the or predicate

Exceptions
NullPointerException:
if either predicate is null
See Also
OrPredicate
transformedPredicateback to summary
public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate)

Creates a predicate that transforms the input object before passing it to the predicate.

Parameters
<T>
the type that the predicate queries
transformer:Transformer<? super T, ? extends T>

the transformer to call first

predicate:Predicate<? super T>

the predicate to call with the result of the transform

Returns:Predicate<T>

the predicate

Exceptions
NullPointerException:
if the transformer or the predicate is null
Since
3.1
See Also
TransformedPredicate
truePredicateback to summary
public static <T> Predicate<T> truePredicate()

Gets a Predicate that always returns true.

Parameters
<T>
the type that the predicate queries
Returns:Predicate<T>

the predicate

See Also
TruePredicate
uniquePredicateback to summary
public static <T> Predicate<T> uniquePredicate()

Creates a Predicate that returns true the first time an object is encountered, and false if the same object is received again. The comparison is by equals(). A null input object is accepted and will return true the first time, and false subsequently as well.

Parameters
<T>
the type that the predicate queries
Returns:Predicate<T>

the predicate

See Also
UniquePredicate