Package org.jutil.predicate

Provides for predicates that map an object to true or false.

See:
          Description

Interface Summary
Predicate A class of predicates that evaluate to true or false for an object. The Predicate class is actually an implementation of the Strategy pattern.
 

Class Summary
AbstractPredicate  
And A class of predicates that evaluates to the and of a number of other predicates. For efficiency reasons, the conditional and will be computed.
BinaryPredicate A class of predicates that evaluates an object using two other predicates.
CompositePredicate A class of predicates that evaluates an object using a number of other predicates.
Equal A class of predicate that check whether or not the given argument equals getObject().
False A special total predicate that always returns false for eval().
Identical A class of predicate that check whether or not the given argument is the same as some object.
Implication A class of predicates that evaluates to the implication of two other predicates.
NegationAsFailure A class of predicates that wrap another predicate and treat the occurrence of an exception in eval() as false. Note that if you wrap a NegationAsFailure object in another predicate, you will not get the same result as when switching the wrapping predicate and the NegationAsFailure.
Not A class of predicates that negate another predicate.
NotNull A special total predicate that checks whether or not objects are different from null.
Or A class of predicates that evaluates to the or of a number of other predicates. For efficiency reasons, the conditional or will be computed.
PrimitivePredicate A class of predicates that have no subpredicates. Since a primitive predicate does not contain any subpredicates, this abstract class implements nbSubPredicates() and getSubPredicates(). Typically, this class will be used as an anonymous inner class as follows: Predicate myPredicate = new PrimitivePredicate() { /oo o also public behavior o o post postcondition; o/ public boolean eval(Object o) throws MyException { //calculate boolean value //using the given object } };
PrimitiveTotalPredicate A class of total predicates that have no subpredicates. This class implements the nbSubPredicates() and getSubPredicates() methods for predicates that don't contain any sub predicates.
TotalPredicate A class of Predicates that do not throw exceptions. If your predicate don't throw exceptions, you should subclass TotalPredicate.
True A special total predicate that always returns true for eval().
TypePredicate A class of predicate that check whether or not an object conforms to a certain type. This class is typically used as follows: TotalPredicate predicate = new TypePredicate(MyClass.class); When a TypePredicate is used as a constant in an interface, the .class operator will return null, so in that case you must pass the name of the class as a String: TotalPredicate predicate = new TypePredicate("mypackage.MyClass");
Xor A class of predicates that evaluates to the xor of two other predicates.
 

Package org.jutil.predicate Description

Provides for predicates that map an object to true or false. A predicate encapsulates a unary function of type Object -> boolean. Using a predicate, it is possible to reuse a function so that it can be passed to a method as an argument.

Class Diagram

Examples

Retrieving all wednesdays from a collection

Suppose that I want to retrieve all wednesdays from a collection, and let's assume they are represented by a Calendar object with a DAY_OF_WEEK field that equals Calendar.WEDNESDAY.

Let's assume further that our collection contains other objects than Calendar objects, so that we know nothing about the type of the objects.


Collection myClone = new ArrayList(myCollection);
TotalPredicate isWednesday = new PrimitiveTotalPredicate() {
                public boolean eval(Object o) {
                  return (o instanceof Calendar) && (((Calendar)o).DAY_OF_WEEK == Calendar.WEDNESDAY);
                }
  };
isWednesday.filter(myClone);

Checking whether or not there is a party on wednesday

Suppose I've got all days on which there is a party in a collection. Now I want to know whether or not there is a party on a wednesday. I can reuse the isWednesday predicate from above.


boolean partyOnWednesday = isWednesday.exists(partyDays);

Parties not no a wednesday

Now I want all parties that are not on a wednesday. Although the criterion is slightly different now, I can still reuse isWednesday. All I have to do is invert is with Not.


Collection partiesNotOnWednesday = new ArrayList(partyDays);
new Not(isWednesday).filter(partiesNotOnWednesday);