⇒ Index (Frames) |  ⇒ Index (No Frames) |  ⇒ Package |  ⇒ Package Tree |  ⇒ Full Tree 
org.apache.commons.collections.functors

Class SwitchTransformer

java.lang.Object
|
+--org.apache.commons.collections.functors.SwitchTransformer

All Implemented Interfaces:
Transformer, Serializable


public class SwitchTransformer
extends java.lang.Object
implements Transformer, Serializable

Transformer implementation calls the transformer whose predicate returns true, like a switch statement.
Since:
Commons Collections 3.0
Author:
Stephen Colebourne

Constructor Summary

SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer)

Constructor that performs no validation.

Method Summary

static Transformer

getInstance(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer)

Factory method that performs validation and copies the parameter arrays.

static Transformer

getInstance(Map predicatesAndTransformers)

Create a new Transformer that calls one of the transformers depending on the predicates.

Object

transform(Object input)

Execute the transformer whose predicate returns true.

Constructor Details

SwitchTransformer

public SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer 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 Details

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.

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

getInstance

public static Transformer getInstance(Predicate[] predicates, Transformer[] transformers, Transformer 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:
- if array is null
- if any element in the array is null

transform

public Object transform(Object input)

Execute the transformer whose predicate returns true.

Parameters:
input