Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

4.12. Applying Conditional Transformations

4.12.1. Problem

You need to perform a different transformation depending on a series of conditions or cases.

4.12.2. Solution

Use a SwitchTransformer to apply a Transformer that is dependent on a Predicate. A SwitchTransformer models a switch statement, and it takes three parameters: an array of Predicate instances, an array of Transformer instances, and a default Transformer. Example 4-9 uses a SwitchTransformer to apply a different Transformer implementation to odd and even numbers.

Example 4-9. Using a SwitchTransformer

Transformer oddTransform = new Transformer( ) {
    public Object transform(Object input) {
        Integer number = new Integer( input );
        return ( new Integer( number.intValue( ) * 2 );
    }
}
Transformer evenTransform = new Transformer( ) {
    public Object transform(Object input) {
        Integer number = new Integer( input );
        return ( new Integer( number.intValue( ) * 3 );
    }
}
Predicate isEven = new Predicate( ) {
    public boolean evaluate(Object input) {
        Integer number = (Integer) input;
        return( number.intValue( ) % 2 == 0 );
    }
}
Predicate isOdd = new NotPredicate(isEven);
                  Predicate[] pArray = new Predicate[] { isOdd, isEven };
                  Transformer[] tArray = new Transformer[] { oddTransform, evenTransform };
                  Transform predicateTransform = 
                      new SwitchTransform( pArray, tArray, new NOPTransformer( ) );
Integer one = new Integer( 1 );
Integer two = new Integer( 2 );
Integer three = new Integer( 3 );
Integer four = new Integer( 4 );
System.out.println( "Transform of 1 = " + predicateTransform.
transform( one ) );
System.out.println( "Transform of 2 = " + predicateTransform.
transform( two ) );
System.out.println( "Transform of 3 = " + predicateTransform.
transform( three ) );
System.out.println( "Transform of 4 = " + predicateTransform.
transform( four ) );

If an object being transformed satisfies the isOdd Predicate, it is passed to the oddTransform Transformer. If an object being transformed satisfies the isEven Predicate, it is passed to the evenTransform Predicate. If the object satisfies neither Predicate, it is passed to an instance of NOPTransformer, which is a Transformer implementation that returns the object passed to transform( ):

Transform of 1 = 2
Transform of 2 = 6
Transform of 3 = 6
Transform of 4 = 12

4.12.3. Discussion

The array of Predicate instances and the array of Transformer instances passed to the constructor of SwitchTransformer must be of equal length. The SwitchTransformer evaluates each of the Predicate instances in the array. If a Predicate evaluates to true, the SwitchTransformer retrieves the Transformer that corresponds to the matching Predicate. If no Predicate evaluates to true, the SwitchTransformer passes the object being transformed to the third parameter—the default Transformer. In Example 4-9, the default Transformer was a NOPTransformer, which is a Transformer implementation that performs no transformation and returns the object passed to transform(). Figure 4-5 illustrates the SwitchTransformer from the Solution; the two Predicate instances correspond to two Transformer instances.

A SwitchTransform with two Predicate instances, two Transformer instances, and a default Transformer

Figure 4-5. A SwitchTransform with two Predicate instances, two Transformer instances, and a default Transformer



Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.