3.15. Validating Beans with Predicates

Problem

You need to test the state of a bean, testing for the presence or absence of simple and nested bean properties.

Solution

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 null
               Predicate 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 AndPredicate
               Predicate 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? " + hensValid );

Assume that the two Team objects contain two properties: name and coach. The coach property on Team is a Coach object with two properties: firstName and lastName. The first BeanPredicate, teamNotNull, uses a NotNullPredicate to test the simple property name. The second BeanPredicate uses an EqualPredicate to test the nested property coach.firstName. In the previous example, a Team object is only valid if it has a name, and the first name of the coach is “Tom.” Two teams are created and the following output is printed:

Is Swordfish team valid? true
Is Hens team valid? false

Discussion

A BeanPredicate obtains the value of the specified bean property using PropertyUtils and passes the resulting property value to the Predicate it was constructed with; BeanPredicate decorates another Predicate. The following example demonstrates the use of BeanPredicate, wrapping an EqualPredicate, InstanceofPredicate, and a composite AnyPredicate:

import org.apache.commons.collections.Predicate;
import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.collections.functors.AnyPredicate;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.collections.functors.InstanceofPredicate;
import org.apache.commons.collections.functors.OrPredicate;

// A predicate to validate the value of the age property
Predicate example1 = new BeanPredicate( "age", 
                       new EqualPredicate( new Integer( 10 ) );

// A predicate to validate the type of the title property
Predicate example2 = new BeanPredicate( "book[4].title", 
                       new InstanceofPredicate( String.class ) );

// A composite predicate definition
Predicate equalA = new EqualsPredicate("A");
Predicate equalB = new EqualsPredicate("B");
Predicate equalC = new EqualsPredicate("C");
Predicate eitherABC = 
    new AnyPredicate( new Predicate[] { equalA, equalB, equalC } );

// A predicate to validate the type of the title property
Predicate example3 = new BeanPredicate( "mode", eitherABC );

Predicate example1 tests the age property of a bean, passing the property value to an EqualPredicate, which returns true if age is 10. Predicate example2 tests the property title from the fifth element in the book property; if the book’s title property value is of type String, example2 returns true. Predicate example3 tests the value of the mode property of a bean, it evaluates to true if mode equals “A,” “B,” or “C.” These three examples demonstrate that a BeanPredicate is a simple decorator, which allows one to apply any Predicate to a bean property.

See Also

Chapter 4 contains more recipes focused on using Predicate implementations to perform complex validation and to create intelligent, self-validating collections. This recipe introduces two simple predicates; EqualPredicate and NotNullPredicate are discussed in Recipe 4.7. For more information about using predicates, see Chapter 4 and the Commons Collections project site at http://jakarta.apache.org/commons/collections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.140.204