November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to perform complex
conditional logic using multiple
Predicate objects, and you need to combine and
expose multiple criteria as one Predicate.
To combine several Predicate instances, create a
Predicate to capture each portion of a compound
condition, and combine each condition with
AndPredicate, OrPredicate,
AllPredicate, OnePredicate,
AnyPredicate, or NonePredicate.
All of these predicate implementations are used to combine the
results of multiple predicates—creating a compound predicate.
The following code demonstrates the use of the
AndPredicate, OrPredicate,
AllPredicate, and OnePredicate:
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.functors.*;
// Create Base Predicates
Predicate isTim = new EqualsPredicate("Tim");
Predicate isDouble = new InstanceOfPredicate( Double.class );
Predicate isNotNull = NotNullPredicate.INSTANCE;
Predicate[] predicates = new Predicate[] { isTim, isDouble, isNotNull };
// Create 2 argument logical predicate composites
Predicate andPredicate = new AndPredicate( isTim, isNotNull );
Predicate orPredicate = new OrPredicate( isTim, isNotNull );
// Create n-argument logical predicate composites
Predicate allPredicate = new AllPredicate( predicates );
Predicate onePredicate = new OnePredicate( predicates ); System.out.println( "'Tim' and not null?: " + andPredicate. evalute( "Tim" ) ); System.out.println( "'Tim' or not null?: " + andPredicate. evalute(new ...Read now
Unlock full access