org.apache.commons.collections.functors
Class SwitchTransformer<I,O>

java.lang.Object
  extended by org.apache.commons.collections.functors.SwitchTransformer<I,O>
All Implemented Interfaces:
Serializable, Transformer<I,O>

public class SwitchTransformer<I,O>
extends Object
implements Transformer<I,O>, Serializable

Transformer implementation calls the transformer whose predicate returns true, like a switch statement.

Since:
Commons Collections 3.0
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:34:38 $
Author:
Matt Hall, John Watkinson, Stephen Colebourne
See Also:
Serialized Form

Constructor Summary
SwitchTransformer(Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers, Transformer<? super I,? extends O> defaultTransformer)
          Constructor that performs no validation.
 
Method Summary
 Transformer<? super I,? extends O> getDefaultTransformer()
          Gets the default transformer.
static
<I,O> Transformer<I,O>
getInstance(Map<Predicate<? super I>,Transformer<? super I,? extends O>> predicatesAndTransformers)
          Create a new Transformer that calls one of the transformers depending on the predicates.
static
<I,O> Transformer<I,O>
getInstance(Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers, Transformer<? super I,? extends O> defaultTransformer)
          Factory method that performs validation and copies the parameter arrays.
 Predicate<? super I>[] getPredicates()
          Gets the predicates, do not modify the array.
 Transformer<? super I,? extends O>[] getTransformers()
          Gets the transformers, do not modify the array.
 O transform(I input)
          Transforms the input to result by calling the transformer whose matching predicate returns true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwitchTransformer

public SwitchTransformer(Predicate<? super I>[] predicates,
                         Transformer<? super I,? extends O>[] transformers,
                         Transformer<? super I,? extends O> defaultTransformer)
Constructor that performs no validation. Use getInstance if you want that.

Parameters:
predicates - array of predicates, not cloned, no nulls
transformers - matching array of transformers, not cloned, no nulls
defaultTransformer - the transformer to use if no match, null means nop
Method Detail

getInstance

public static <I,O> Transformer<I,O> getInstance(Predicate<? super I>[] predicates,
                                                 Transformer<? super I,? extends O>[] transformers,
                                                 Transformer<? super I,? extends O> defaultTransformer)
Factory method that performs validation and copies the parameter arrays.

Parameters:
predicates - array of predicates, cloned, no nulls
transformers - matching array of transformers, cloned, no nulls
defaultTransformer - the transformer to use if no match, null means nop
Returns:
the chained transformer
Throws:
IllegalArgumentException - if array is null
IllegalArgumentException - if any element in the array is null

getInstance

public static <I,O> Transformer<I,O> getInstance(Map<Predicate<? super I>,Transformer<? super I,? extends O>> predicatesAndTransformers)
Create a new Transformer that calls one of the transformers depending on the predicates.

The Map consists of Predicate keys and Transformer values. A transformer is called if its matching predicate returns true. Each predicate is evaluated until one returns true. If no predicates evaluate to true, the default transformer is called. The default transformer is set in the map with a null key. The ordering is that of the iterator() method on the entryset collection of the map.

Parameters:
predicatesAndTransformers - a map of predicates to transformers
Returns:
the switch transformer
Throws:
IllegalArgumentException - if the map is null
IllegalArgumentException - if any transformer in the map is null
ClassCastException - if the map elements are of the wrong type

transform

public O transform(I input)
Transforms the input to result by calling the transformer whose matching predicate returns true.

Specified by:
transform in interface Transformer<I,O>
Parameters:
input - the input object to transform
Returns:
the transformed result

getPredicates

public Predicate<? super I>[] getPredicates()
Gets the predicates, do not modify the array.

Returns:
the predicates
Since:
Commons Collections 3.1

getTransformers

public Transformer<? super I,? extends O>[] getTransformers()
Gets the transformers, do not modify the array.

Returns:
the transformers
Since:
Commons Collections 3.1

getDefaultTransformer

public Transformer<? super I,? extends O> getDefaultTransformer()
Gets the default transformer.

Returns:
the default transformer
Since:
Commons Collections 3.1


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.