4.12. Applying Conditional Transformations

Problem

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

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

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

See Also

Jakarta Commons Functor in the Commons Sandbox introduces a UnaryFunction interface that provides an interface equivalent to Transformer. A UnaryFunction can be predicated with a UnaryPredicate object via the ConditionalUnaryFunction class. For more information about UnaryPredicate and ConditionalUnaryFunction, see the Commons Functor page at http://jakarta.apache.org/commons/sandbox/functor.

Get Jakarta Commons Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.