net.sf.collections15.functors.transformer
Class ChainedTransformer<I,M,O>

java.lang.Object
  extended by net.sf.collections15.functors.transformer.ChainedTransformer<I,M,O>
All Implemented Interfaces:
java.io.Serializable, Transformer<I,O>

public class ChainedTransformer<I,M,O>
extends java.lang.Object
implements Transformer<I,O>, java.io.Serializable

Transformer implementation that chains two specified Transformers together. The principal restriction on the chaining together of two Transformers into a chain is that the generic output type of the first Transformer must match the generic input type of the second Transformer. For example:

 Transformer<A, B> t1;
 Transformer<B, C> t2;
 ...
 Transformer<A, C> chained = ChainedTransformer.getInstance(t1, t2);
 
The transform() method of a ChainedTransformer works by transforming an input object using the first Transformer, resulting in an intermediate object. This is then transformed by the second Transformer, resulting in the returned output object.

The methods prepend(net.sf.collections15.Transformer) and append(net.sf.collections15.Transformer) can be used to add additional Transformers to the chain, allowing multiple Transformers to be chained together in a typesafe manner. In this way, a tree of Transformers can be built up, whereby the leaves of the tree form a typesafe chain of Transformers The input type of the first leaf Transformer is I, and the output type of the last leaf Transformer is O. The transform process takes an input object, transforms it using the first leaf Transformer, and passes the result on to the next leaf. This process repeats along the chain of leaves, until the last leaf Transformer in the chain produces the final output object.

Here's an example, showing the use of the static factory method, and both the prepend and append methods:

 Transformer<A, B> t1;
 Transformer<B, C> t2;
 Transformer<C, D> t3;
 Transformer<D, E> t4;
 ...
 Transformer<A, E> chained = ChainedTransformer.getInstance(t2, t3)
     .append(t4).prepend(t1);

Since:
Collections15 1.0
Author:
Chris Lambrou
See Also:
Serialized Form

Constructor Summary
protected ChainedTransformer(Transformer<? super I,? extends M> firstTransformer, Transformer<? super M,? extends O> secondTransformer)
          Creates a new instance that chains together the two specified Transformers.
 
Method Summary
<T> ChainedTransformer<I,O,T>
append(Transformer<? super O,T> transformer)
          Appends a specified Transformer to the end of the chain, resulting in a new ChainedTransformer that transforms an input object through the existing Transformers in this chain, and then transforms the result through the specified Transformer.
static
<I,M,O> ChainedTransformer<I,M,O>
getInstance(Transformer<? super I,? extends M> firstTransformer, Transformer<? super M,? extends O> secondTransformer)
          Returns a new ChainedTransformer instance that chains together the two specified Transformers.
<T> ChainedTransformer<T,I,O>
prepend(Transformer<T,? extends I> transformer)
          Prepends a specified Transformer to the start of the chain, resulting in a new ChainedTransformer that transforms an input object through the specified Transformer, and then transforms the result through the existing Transformers in this chain.
 O transform(I input)
          Transforms the input object by transforming it using the first Transformer, and then transforming the result using the second Transformer, returning the resulting output object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChainedTransformer

protected ChainedTransformer(Transformer<? super I,? extends M> firstTransformer,
                             Transformer<? super M,? extends O> secondTransformer)
Creates a new instance that chains together the two specified Transformers.

Parameters:
firstTransformer - The first Transformer in the chain.
secondTransformer - The second Transformer in the chain.
Throws:
java.lang.IllegalArgumentException - Thrown if either Transformer is null.
Method Detail

getInstance

public static <I,M,O> ChainedTransformer<I,M,O> getInstance(Transformer<? super I,? extends M> firstTransformer,
                                                            Transformer<? super M,? extends O> secondTransformer)
Returns a new ChainedTransformer instance that chains together the two specified Transformers.

Parameters:
firstTransformer - The first Transformer in the chain.
secondTransformer - The second Transformer in the chain.
Returns:
A new ChainedTransformer instance that chains together the two specified Transformers.
Throws:
java.lang.IllegalArgumentException - Thrown if either Transformer is null.

append

public <T> ChainedTransformer<I,O,T> append(Transformer<? super O,T> transformer)
Appends a specified Transformer to the end of the chain, resulting in a new ChainedTransformer that transforms an input object through the existing Transformers in this chain, and then transforms the result through the specified Transformer.

Parameters:
transformer - The Transformer to append to the chain.
Returns:
A new ChainedTransformer that transforms an input object through the existing Transformers in this chain, and then transforms the result through the specified Transformer.
Throws:
java.lang.IllegalArgumentException - Thrown if the specified Transformer is null.

prepend

public <T> ChainedTransformer<T,I,O> prepend(Transformer<T,? extends I> transformer)
Prepends a specified Transformer to the start of the chain, resulting in a new ChainedTransformer that transforms an input object through the specified Transformer, and then transforms the result through the existing Transformers in this chain.

Parameters:
transformer - The Transformer to prepend to the chain.
Returns:
A new ChainedTransformer that transforms an input object through the specified Transformer, and then transforms the result through the existing Transformers in this chain.
Throws:
java.lang.IllegalArgumentException - Thrown if the specified Transformer is null.

transform

public O transform(I input)
Transforms the input object by transforming it using the first Transformer, and then transforming the result using the second Transformer, returning the resulting output object.

Specified by:
transform in interface Transformer<I,O>
Parameters:
input - The object to be transformed.
Returns:
The transformed object


Copyright © 2001-2005 SourceForge.net. All Rights Reserved.