org.apache.commons.collections.functors
Class SwitchTransformer
java.lang.Object
org.apache.commons.collections.functors.SwitchTransformer
- Serializable, Transformer
public class SwitchTransformer
extends java.lang.Object
Transformer implementation calls the transformer whose predicate returns true,
like a switch statement.
$Revision: 1.5 $ $Date: 2004/05/16 11:36:31 $
SwitchTransformer
public SwitchTransformer(Predicate[] predicates,
Transformer[] transformers,
Transformer defaultTransformer)
Constructor that performs no validation.
Use getInstance
if you want that.
predicates
- array of predicates, not cloned, no nullstransformers
- matching array of transformers, not cloned, no nullsdefaultTransformer
- the transformer to use if no match, null means nop
getDefaultTransformer
public Transformer getDefaultTransformer()
Gets the default transformer.
getInstance
public static Transformer getInstance(Map 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.
predicatesAndTransformers
- a map of predicates to transformers
getInstance
public static Transformer getInstance(Predicate[] predicates,
Transformer[] transformers,
Transformer defaultTransformer)
Factory method that performs validation and copies the parameter arrays.
predicates
- array of predicates, cloned, no nullstransformers
- matching array of transformers, cloned, no nullsdefaultTransformer
- the transformer to use if no match, null means nop
getPredicates
public Predicate[] getPredicates()
Gets the predicates, do not modify the array.
getTransformers
public Transformer[] getTransformers()
Gets the transformers, do not modify the array.
transform
public Object transform(Object input)
Transforms the input to result by calling the transformer whose matching
predicate returns true.
- transform in interface Transformer
input
- the input object to transform
Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.