Class IfTransformer<I,O>
- java.lang.Object
-
- org.apache.commons.collections4.functors.IfTransformer<I,O>
-
- Type Parameters:
I
- The input type for the transformerO
- The output type for the transformer
- All Implemented Interfaces:
Serializable
,Transformer<I,O>
public class IfTransformer<I,O> extends Object implements Transformer<I,O>, Serializable
Transformer implementation that will call one of two closures based on whether a predicate evaluates as true or false.- Since:
- 4.1
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description IfTransformer(Predicate<? super I> predicate, Transformer<? super I,? extends O> trueTransformer, Transformer<? super I,? extends O> falseTransformer)
Constructor that performs no validation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Transformer<? super I,? extends O>
getFalseTransformer()
Gets the transformer used when false.Predicate<? super I>
getPredicate()
Gets the predicate.Transformer<? super I,? extends O>
getTrueTransformer()
Gets the transformer used when true.static <I,O>
Transformer<I,O>ifTransformer(Predicate<? super I> predicate, Transformer<? super I,? extends O> trueTransformer, Transformer<? super I,? extends O> falseTransformer)
Factory method that performs validation.static <T> Transformer<T,T>
ifTransformer(Predicate<? super T> predicate, Transformer<? super T,? extends T> trueTransformer)
Factory method that performs validation.O
transform(I input)
Transforms the input using the true or false transformer based to the result of the predicate.
-
-
-
Constructor Detail
-
IfTransformer
public IfTransformer(Predicate<? super I> predicate, Transformer<? super I,? extends O> trueTransformer, Transformer<? super I,? extends O> falseTransformer)
Constructor that performs no validation. Use the static factory methodifTransformer
if you want that.- Parameters:
predicate
- predicate to switch on, not nulltrueTransformer
- transformer used if true, not nullfalseTransformer
- transformer used if false, not null
-
-
Method Detail
-
ifTransformer
public static <I,O> Transformer<I,O> ifTransformer(Predicate<? super I> predicate, Transformer<? super I,? extends O> trueTransformer, Transformer<? super I,? extends O> falseTransformer)
Factory method that performs validation.- Type Parameters:
I
- input type for the transformerO
- output type for the transformer- Parameters:
predicate
- predicate to switch ontrueTransformer
- transformer used if truefalseTransformer
- transformer used if false- Returns:
- the
if
transformer - Throws:
NullPointerException
- if either argument is null
-
ifTransformer
public static <T> Transformer<T,T> ifTransformer(Predicate<? super T> predicate, Transformer<? super T,? extends T> trueTransformer)
Factory method that performs validation.This factory creates a transformer that just returns the input object when the predicate is false.
- Type Parameters:
T
- input and output type for the transformer- Parameters:
predicate
- predicate to switch ontrueTransformer
- transformer used if true- Returns:
- the
if
transformer - Throws:
NullPointerException
- if either argument is null
-
transform
public O transform(I input)
Transforms the input using the true or false transformer based to the result of the predicate.- Specified by:
transform
in interfaceTransformer<I,O>
- Parameters:
input
- the input object to transform- Returns:
- the transformed result
-
getTrueTransformer
public Transformer<? super I,? extends O> getTrueTransformer()
Gets the transformer used when true.- Returns:
- the transformer
-
getFalseTransformer
public Transformer<? super I,? extends O> getFalseTransformer()
Gets the transformer used when false.- Returns:
- the transformer
-
-