public class SwitchTransformer<I,O> extends java.lang.Object implements Transformer<I,O>, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
private Transformer<? super I,? extends O> |
iDefault
The default transformer to call if no tests match
|
private Predicate<? super I>[] |
iPredicates
The tests to consider
|
private Transformer<? super I,? extends O>[] |
iTransformers
The matching transformers to call
|
private static long |
serialVersionUID
Serial version UID
|
Modifier | Constructor and Description |
---|---|
private |
SwitchTransformer(boolean clone,
Predicate<? super I>[] predicates,
Transformer<? super I,? extends O>[] transformers,
Transformer<? super I,? extends O> defaultTransformer)
Hidden constructor for the use by the static factory methods.
|
|
SwitchTransformer(Predicate<? super I>[] predicates,
Transformer<? super I,? extends O>[] transformers,
Transformer<? super I,? extends O> defaultTransformer)
Constructor that performs no validation.
|
Modifier and Type | Method and Description |
---|---|
Transformer<? super I,? extends O> |
getDefaultTransformer()
Gets the default transformer.
|
Predicate<? super I>[] |
getPredicates()
Gets the predicates.
|
Transformer<? super I,? extends O>[] |
getTransformers()
Gets the transformers.
|
static <I,O> Transformer<I,O> |
switchTransformer(java.util.Map<? extends Predicate<? super I>,? extends Transformer<? super I,? extends O>> map)
Create a new Transformer that calls one of the transformers depending
on the predicates.
|
static <I,O> Transformer<I,O> |
switchTransformer(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.
|
O |
transform(I input)
Transforms the input to result by calling the transformer whose matching
predicate returns true.
|
private static final long serialVersionUID
private final Transformer<? super I,? extends O>[] iTransformers
private final Transformer<? super I,? extends O> iDefault
private SwitchTransformer(boolean clone, Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers, Transformer<? super I,? extends O> defaultTransformer)
clone
- if true
the input arguments will be clonedpredicates
- array of predicates, no nullstransformers
- matching array of transformers, no nullsdefaultTransformer
- the transformer to use if no match, null means return nullpublic SwitchTransformer(Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers, Transformer<? super I,? extends O> defaultTransformer)
switchTransformer
if you want that.predicates
- array of predicates, cloned, no nullstransformers
- matching array of transformers, cloned, no nullsdefaultTransformer
- the transformer to use if no match, null means return nullpublic static <I,O> Transformer<I,O> switchTransformer(Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers, Transformer<? super I,? extends O> defaultTransformer)
I
- the input typeO
- the output typepredicates
- array of predicates, cloned, no nullstransformers
- matching array of transformers, cloned, no nullsdefaultTransformer
- the transformer to use if no match, null means return nullchained
transformerjava.lang.IllegalArgumentException
- if array is nulljava.lang.IllegalArgumentException
- if any element in the array is nullpublic static <I,O> Transformer<I,O> switchTransformer(java.util.Map<? extends Predicate<? super I>,? extends Transformer<? super I,? extends O>> map)
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.
I
- the input typeO
- the output typemap
- a map of predicates to transformersswitch
transformerjava.lang.IllegalArgumentException
- if the map is nulljava.lang.IllegalArgumentException
- if any transformer in the map is nulljava.lang.ClassCastException
- if the map elements are of the wrong typepublic O transform(I input)
transform
in interface Transformer<I,O>
input
- the input object to transformpublic Predicate<? super I>[] getPredicates()
public Transformer<? super I,? extends O>[] getTransformers()
public Transformer<? super I,? extends O> getDefaultTransformer()