November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to test the state of a bean, testing for the presence or absence of simple and nested bean properties.
Use a
BeanPredicate
from Commons BeanUtils. A BeanPredicate is an
implementation of the Predicate interface defined
in Commons Collections. As described in Chapter 4, a
Predicate’s evaluate() method takes an object and returns a
boolean; BeanPredicate
decorates another Predicate, allowing that
Predicate to evaluate a bean property: simple,
nested, indexed, or mapped. The following code demonstrates the use
of BeanPredicate to validate the condition of a
bean:
import org.apache.commons.beanutils.*; import org.apache.commons.collections.*;// A Predicate that returns true if the "name" property is not nullPredicate teamNotNull = new BeanPredicate( "name", new NotNullPredicate( ) );// A Predicate that returns true if the "coach.firstName" property// is "Tom"Predicate coachFirstName = new BeanPredicate( "coach.firstName",new EqualsPredicate("Tom") );// Tie two Predicates together into an AndPredicatePredicate validateTeam = new AllPredicate( predicateArray );// Create Team Objects Team fish = new Team( "Swordfish", new Coach( "Tom", "O'Connell") ); Team hens = new Team( "Hens", new Coach( "Bob", "McGarry") );boolean fishValid = validateTeam.evaluate( fish );boolean henValid = validateTeam.evaluate( hens );System.out.println( "Is Swordfish team valid? " + fishValid ); System.out.println( "Is Hens team valid? ...
Read now
Unlock full access