Class SwitchTransformer<T,R>
- java.lang.Object
-
- org.apache.commons.collections4.functors.SwitchTransformer<T,R>
-
- Type Parameters:
T- the type of the input to the function.R- the type of the result of the function.
- All Implemented Interfaces:
java.io.Serializable,java.util.function.Function<T,R>,Transformer<T,R>
public class SwitchTransformer<T,R> extends java.lang.Object implements Transformer<T,R>, java.io.Serializable
Transformer implementation calls the transformer whose predicate returns true, like a switch statement.- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SwitchTransformer(Predicate<? super T>[] predicates, Transformer<? super T,? extends R>[] transformers, Transformer<? super T,? extends R> defaultTransformer)Constructor that performs no validation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Transformer<? super T,? extends R>getDefaultTransformer()Gets the default transformer.Predicate<? super T>[]getPredicates()Gets the predicates.Transformer<? super T,? extends R>[]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.Rtransform(T 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
-
Methods inherited from interface org.apache.commons.collections4.Transformer
apply
-
-
-
-
Constructor Detail
-
SwitchTransformer
public SwitchTransformer(Predicate<? super T>[] predicates, Transformer<? super T,? extends R>[] transformers, Transformer<? super T,? extends R> defaultTransformer)
Constructor that performs no validation. UseswitchTransformerif you want that.- Parameters:
predicates- array of predicates, cloned, no nullstransformers- matching array of transformers, cloned, no nullsdefaultTransformer- the transformer to use if no match, null means return null
-
-
Method Detail
-
switchTransformer
public 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.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.
- Type Parameters:
I- the input typeO- the output type- Parameters:
map- a map of predicates to transformers- Returns:
- the
switchtransformer - Throws:
java.lang.NullPointerException- if the map is nulljava.lang.NullPointerException- if any transformer in the map is nulljava.lang.ClassCastException- if the map elements are of the wrong type
-
switchTransformer
public 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.- Type Parameters:
I- the input typeO- the output type- Parameters:
predicates- array of predicates, cloned, no nullstransformers- matching array of transformers, cloned, no nullsdefaultTransformer- the transformer to use if no match, null means return null- Returns:
- the
chainedtransformer - Throws:
java.lang.NullPointerException- if either array is nulljava.lang.NullPointerException- if any element in the arrays is nulljava.lang.IllegalArgumentException- if the arrays have different sizes
-
getDefaultTransformer
public Transformer<? super T,? extends R> getDefaultTransformer()
Gets the default transformer.- Returns:
- the default transformer
- Since:
- 3.1
-
getPredicates
public Predicate<? super T>[] getPredicates()
Gets the predicates.- Returns:
- a copy of the predicates
- Since:
- 3.1
-
getTransformers
public Transformer<? super T,? extends R>[] getTransformers()
Gets the transformers.- Returns:
- a copy of the transformers
- Since:
- 3.1
-
-