⇒ Index (Frames) |  ⇒ Index (No Frames) |  ⇒ Package |  ⇒ Package Tree |  ⇒ Full Tree 
org.apache.commons.collections

Class PredicateUtils

java.lang.Object
|
+--org.apache.commons.collections.PredicateUtils


public class PredicateUtils
extends java.lang.Object

PredicateUtils provides reference implementations and utilities for the Predicate functor interface. The supplied predicates are: All the supplied predicates are Serializable.
Since:
Commons Collections 3.0
Authors:
Stephen Colebourne
Ola Berg

Constructor Summary

PredicateUtils()

This class is not normally instantiated.

Method Summary

static Predicate

allPredicate(Predicate[] predicates)

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

static Predicate

allPredicate(Collection predicates)

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

static Predicate

andPredicate(Predicate predicate1, Predicate predicate2)

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

static Predicate

anyPredicate(Predicate[] predicates)

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

static Predicate

anyPredicate(Collection predicates)

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

static Predicate

asPredicate(Transformer transformer)

Create a new Predicate that wraps a Transformer.

static Predicate

eitherPredicate(Predicate predicate1, Predicate predicate2)

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

static Predicate

equalPredicate(Object value)

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

static Predicate

exceptionPredicate()

Gets a Predicate that always throws an exception.

static Predicate

falsePredicate()

Gets a Predicate that always returns false.

static Predicate

identityPredicate(Object value)

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

static Predicate

instanceofPredicate(Class type)

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

static Predicate

invokerPredicate(String methodName)

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

static Predicate

invokerPredicate(String methodName, Class[] paramTypes, Object[] args)

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

static Predicate

neitherPredicate(Predicate predicate1, Predicate predicate2)

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

static Predicate

nonePredicate(Predicate[] predicates)

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

static Predicate

nonePredicate(Collection predicates)

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

static Predicate

notNullPredicate()

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

static Predicate

notPredicate(Predicate predicate)

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

static Predicate

nullIsExceptionPredicate(Predicate predicate)

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

static Predicate

nullIsFalsePredicate(Predicate predicate)

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

static Predicate

nullIsTruePredicate(Predicate predicate)

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

static Predicate

nullPredicate()

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

static Predicate

onePredicate(Predicate[] predicates)

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

static Predicate

onePredicate(Collection predicates)

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

static Predicate

orPredicate(Predicate predicate1, Predicate predicate2)

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

static Predicate

truePredicate()

Gets a Predicate that always returns true.

static Predicate

uniquePredicate()

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

Constructor Details

PredicateUtils

public PredicateUtils()

This class is not normally instantiated.

Method Details

allPredicate

public static Predicate allPredicate(Collection predicates)

Create a new Predicate that returns true only if all of the specified predicates are true. The predicates are checked in iterator order.

Parameters:
predicates - a collection of predicates to check, may not be null
Returns:
the all predicate
Throws:
- if the predicates collection is null
- if the predicates collection has less than 2 elements
- if any predicate in the collection is null

allPredicate

public static Predicate allPredicate(Predicate[] predicates)

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

Parameters:
predicates - an array of predicates to check, may not be null
Returns:
the all predicate
Throws:
- if the predicates array is null
- if the predicates array has less than 2 elements
- if any predicate in the array is null

andPredicate

public static Predicate andPredicate(Predicate predicate1, Predicate predicate2)

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

Parameters:
predicate1 - the first predicate, may not be null
predicate2 - the second predicate, may not be null
Returns:
the and predicate
Throws:
- if either predicate is null

anyPredicate

public static Predicate anyPredicate(Collection predicates)

Create a new Predicate that returns true if any of the specified predicates are true. The predicates are checked in iterator order.

Parameters:
predicates - a collection of predicates to check, may not be null
Returns:
the any predicate
Throws:
- if the predicates collection is null
- if the predicates collection has less than 2 elements
- if any predicate in the collection is null

anyPredicate

public static Predicate anyPredicate(Predicate[] predicates)

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

Parameters:
predicates - an array of predicates to check, may not be null
Returns:
the any predicate
Throws:
- if the predicates array is null
- if the predicates array has less than 2 elements
- if any predicate in the array is null

asPredicate

public static Predicate asPredicate(Transformer 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:
transformer - the transformer to wrap, may not be null
Returns:
the transformer wrapping predicate
Throws:
- if the transformer is null

eitherPredicate

public static Predicate eitherPredicate(Predicate predicate1, Predicate predicate2)

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

Parameters:
predicate1 - the first predicate, may not be null
predicate2 - the second predicate, may not be null
Returns:
the either predicate
Throws:
- if either predicate is null

equalPredicate

public static Predicate equalPredicate(Object value)

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

Parameters:
value - the value to compare against
Returns:
the predicate

exceptionPredicate

public static Predicate exceptionPredicate()

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

Returns:
the predicate

falsePredicate

public static Predicate falsePredicate()

Gets a Predicate that always returns false.

Returns:
the predicate

identityPredicate

public static Predicate identityPredicate(Object value)

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

Parameters:
value - the value to compare against
Returns:
the predicate

instanceofPredicate

public static Predicate instanceofPredicate(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 - the type to check for, may not be null
Returns:
the predicate
Throws:
- if the class is null

invokerPredicate

public static Predicate invokerPredicate(String methodName, Class[] paramTypes, 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:
methodName - the method name to call on the input object, may not be null
paramTypes - the parameter types
args - the arguments
Returns:
the predicate
Throws:
- if the method name is null
- if the paramTypes and args don't match

invokerPredicate

public static Predicate invokerPredicate(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:
methodName - the method name to call on the input object, may not be null
Returns:
the predicate
Throws:
- if the methodName is null.

neitherPredicate

public static Predicate neitherPredicate(Predicate predicate1, Predicate predicate2)

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

Parameters:
predicate1 - the first predicate, may not be null
predicate2 - the second predicate, may not be null
Returns:
the neither predicate
Throws:
- if either predicate is null

nonePredicate

public static Predicate nonePredicate(Collection predicates)

Create a new Predicate that returns true if none of the specified predicates are true. The predicates are checked in iterator order.

Parameters:
predicates - a collection of predicates to check, may not be null
Returns:
the none predicate
Throws:
- if the predicates collection is null
- if the predicates collection has less than 2 elements
- if any predicate in the collection is null

nonePredicate

public static Predicate nonePredicate(Predicate[] predicates)

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

Parameters:
predicates - an array of predicates to check, may not be null
Returns:
the none predicate
Throws:
- if the predicates array is null
- if the predicates array has less than 2 elements
- if any predicate in the array is null

notNullPredicate

public static Predicate notNullPredicate()

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

Returns:
the predicate

notPredicate

public static Predicate notPredicate(Predicate predicate)

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

Parameters:
predicate - the predicate to not
Returns:
the not predicate
Throws:
- if the predicate is null

nullIsExceptionPredicate

public static Predicate nullIsExceptionPredicate(Predicate 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:
predicate - the predicate to wrap, may not be null
Returns:
the predicate
Throws:
- if the predicate is null.

nullIsFalsePredicate

public static Predicate nullIsFalsePredicate(Predicate 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:
predicate - the predicate to wrap, may not be null
Returns:
the predicate
Throws:
- if the predicate is null.

nullIsTruePredicate

public static Predicate nullIsTruePredicate(Predicate 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:
predicate - the predicate to wrap, may not be null
Returns:
the predicate
Throws:
- if the predicate is null.

nullPredicate

public static Predicate nullPredicate()

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

Returns:
the predicate

onePredicate

public static Predicate onePredicate(Collection predicates)

Create a new Predicate that returns true if only one of the specified predicates are true. The predicates are checked in iterator order.

Parameters:
predicates - a collection of predicates to check, may not be null
Returns:
the one predicate
Throws:
- if the predicates collection is null
- if the predicates collection has less than 2 elements
- if any predicate in the collection is null

onePredicate

public static Predicate onePredicate(Predicate[] predicates)

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

Parameters:
predicates - an array of predicates to check, may not be null
Returns:
the one predicate
Throws:
- if the predicates array is null
- if the predicates array has less than 2 elements
- if any predicate in the array is null

orPredicate

public static Predicate orPredicate(Predicate predicate1, Predicate predicate2)

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

Parameters:
predicate1 - the first predicate, may not be null
predicate2 - the second predicate, may not be null
Returns:
the or predicate
Throws:
- if either predicate is null

truePredicate

public static Predicate truePredicate()

Gets a Predicate that always returns true.

Returns:
the predicate

uniquePredicate

public static Predicate 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.

Returns:
the predicate