Top Description Constructors Methods
org.apache.commons.collections4

public Class TransformerUtils

extends Object
Class Inheritance
Imports
java.util.Collection, .Map, org.apache.commons.collections4.functors.ChainedTransformer, .CloneTransformer, .ClosureTransformer, .ConstantTransformer, .EqualPredicate, .ExceptionTransformer, .FactoryTransformer, .IfTransformer, .InstantiateTransformer, .InvokerTransformer, .MapTransformer, .NOPTransformer, .PredicateTransformer, .StringValueTransformer, .SwitchTransformer

TransformerUtils provides reference implementations and utilities for the Transformer functor interface. The supplied transformers are:

Since v4.1 only transformers which are considered to be safe are Serializable. Transformers considered to be unsafe for serialization are:

Since
3.0

Constructor Summary

AccessConstructor and Description
private
TransformerUtils()

This class is not normally instantiated.

Method Summary

Modifier and TypeMethod and Description
public static <
the input/output type
T
>
Transformer<T, T>

Returns:

the transformer
asTransformer
(final Closure<? super T>
the closure to run each time in the transformer, not null
closure
)

Creates a Transformer that calls a Closure each time the transformer is used.

public static <
the input type
T
>
Transformer<T, Boolean>

Returns:

the transformer
asTransformer
(final Predicate<? super T>
the predicate to run each time in the transformer, not null
predicate
)

Creates a Transformer that calls a Predicate each time the transformer is used.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
asTransformer
(final Factory<? extends O>
the factory to run each time in the transformer, not null
factory
)

Creates a Transformer that calls a Factory each time the transformer is used.

public static <
the input/output type
T
>
Transformer<T, T>

Returns:

the transformer
chainedTransformer
(final Transformer<? super T, ? extends T>...
an array of transformers to chain
transformers
)

Create a new Transformer that calls each transformer in turn, passing the result into the next transformer.

public static <
the input/output type
T
>
Transformer<T, T>

Returns:

the transformer
chainedTransformer
(final Collection<? extends Transformer<? super T, ? extends T>>
a collection of transformers to chain
transformers
)

Create a new Transformer that calls each transformer in turn, passing the result into the next transformer.

public static <
the input/output type
T
>
Transformer<T, T>

Returns:

the transformer
cloneTransformer
()

Gets a transformer that returns a clone of the input object.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer.
constantTransformer
(final O
the constant object to return each time in the transformer
constantToReturn
)

Creates a Transformer that will return the same object each time the transformer is used.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
exceptionTransformer
()

Gets a transformer that always throws an exception.

public static <
the input / output type
T
>
Transformer<T, T>

Returns:

the transformer
ifTransformer
(final Predicate<? super T>
the predicate to switch on
predicate
,
final Transformer<? super T, ? extends T>
the transformer called if the predicate is true
trueTransformer
)

Create a new Transformer that calls the transformer if the predicate is true, otherwise the input object is returned unchanged.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
ifTransformer
(final Predicate<? super I>
the predicate to switch on
predicate
,
final Transformer<? super I, ? extends O>
the transformer called if the predicate is true
trueTransformer
,
final Transformer<? super I, ? extends O>
the transformer called if the predicate is false
falseTransformer
)

Create a new Transformer that calls one of two transformers depending on the specified predicate.

public static <
the output type
T
>
Transformer<Class<? extends T>, T>

Returns:

the transformer
instantiateTransformer
()

Gets a Transformer that expects an input Class object that it will instantiate.

public static <
the output type
T
>
Transformer<Class<? extends T>, T>

Returns:

the transformer
instantiateTransformer
(final Class<?>[]
parameter types for the constructor, can be null
paramTypes
,
final Object[]
the arguments to pass to the constructor, can be null
args
)

Creates a Transformer that expects an input Class object that it will instantiate.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
invokerTransformer
(final String
the method name to call on the input object, may not be null
methodName
)

Gets a Transformer that invokes a method on the input object.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
invokerTransformer
(final String
the name of the method
methodName
,
final Class<?>[]
the parameter types
paramTypes
,
final Object[]
the arguments
args
)

Gets a Transformer that invokes a method on the input object.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer, or ConstantTransformer#nullTransformer() if the map is null
mapTransformer
(final Map<? super I, ? extends O>
the map to use to transform the objects
map
)

Creates a Transformer that uses the passed in Map to transform the input object (as a simple lookup).

public static <
the input/output type
T
>
Transformer<T, T>

Returns:

the transformer
nopTransformer
()

Gets a transformer that returns the input object.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
nullTransformer
()

Gets a transformer that always returns null.

public static <
the input type
T
>
Transformer<T, String>

Returns:

the transformer
stringValueTransformer
()

Gets a transformer that returns a java.lang.String representation of the input object.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
switchMapTransformer
(final Map<I, Transformer<I, O>>
a map of objects to transformers
objectsAndTransformers
)

Create a new Transformer that uses the input object as a key to find the transformer to call.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
switchTransformer
(final Predicate<? super I>
the predicate to switch on
predicate
,
final Transformer<? super I, ? extends O>
the transformer called if the predicate is true
trueTransformer
,
final Transformer<? super I, ? extends O>
the transformer called if the predicate is false
falseTransformer
)

Create a new Transformer that calls one of two transformers depending on the specified predicate.
public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
switchTransformer
(final Predicate<? super I>[]
an array of predicates to check
predicates
,
final Transformer<? super I, ? extends O>[]
an array of transformers to call
transformers
)

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

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
switchTransformer
(final Predicate<? super I>[]
an array of predicates to check
predicates
,
final Transformer<? super I, ? extends O>[]
an array of transformers to call
transformers
,
final Transformer<? super I, ? extends O>
the default to call if no predicate matches, null means return null
defaultTransformer
)

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

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the transformer
switchTransformer
(final Map<Predicate<I>, Transformer<I, O>>
a map of predicates to transformers
predicatesAndTransformers
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

TransformerUtilsback to summary
private TransformerUtils()

This class is not normally instantiated.

Method Detail

asTransformerback to summary
public static <T> Transformer<T, T> asTransformer(final Closure<? super T> closure)

Creates a Transformer that calls a Closure each time the transformer is used. The transformer returns the input object.

Parameters
<T>
the input/output type
closure:Closure<? super T>

the closure to run each time in the transformer, not null

Returns:Transformer<T, T>

the transformer

Exceptions
NullPointerException:
if the closure is null
See Also
ClosureTransformer
asTransformerback to summary
public static <T> Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate)

Creates a Transformer that calls a Predicate each time the transformer is used. The transformer will return either Boolean.TRUE or Boolean.FALSE.

Parameters
<T>
the input type
predicate:Predicate<? super T>

the predicate to run each time in the transformer, not null

Returns:Transformer<T, Boolean>

the transformer

Exceptions
NullPointerException:
if the predicate is null
See Also
PredicateTransformer
asTransformerback to summary
public static <I, O> Transformer<I, O> asTransformer(final Factory<? extends O> factory)

Creates a Transformer that calls a Factory each time the transformer is used. The transformer will return the value returned by the factory.

Parameters
<I>
the input type
<O>
the output type
factory:Factory<? extends O>

the factory to run each time in the transformer, not null

Returns:Transformer<I, O>

the transformer

Exceptions
NullPointerException:
if the factory is null
See Also
FactoryTransformer
chainedTransformerback to summary
public static <T> Transformer<T, T> chainedTransformer(final Transformer<? super T, ? extends T>... transformers)

Create a new Transformer that calls each transformer in turn, passing the result into the next transformer.

Parameters
<T>
the input/output type
transformers:Transformer<? super T, ? extends T>[]

an array of transformers to chain

Returns:Transformer<T, T>

the transformer

Exceptions
NullPointerException:
if the transformers array or any of the transformers is null
See Also
ChainedTransformer
chainedTransformerback to summary
public static <T> Transformer<T, T> chainedTransformer(final Collection<? extends Transformer<? super T, ? extends T>> transformers)

Create a new Transformer that calls each transformer in turn, passing the result into the next transformer. The ordering is that of the iterator() method on the collection.

Parameters
<T>
the input/output type
transformers:Collection<? extends Transformer<? super T, ? extends T>>

a collection of transformers to chain

Returns:Transformer<T, T>

the transformer

Exceptions
NullPointerException:
if the transformers collection or any of the transformers is null
See Also
ChainedTransformer
cloneTransformerback to summary
public static <T> Transformer<T, T> cloneTransformer()

Gets a transformer that returns a clone of the input object. The input object will be cloned using one of these techniques (in order):

  • public clone method
  • public copy constructor
  • serialization clone
Parameters
<T>
the input/output type
Returns:Transformer<T, T>

the transformer

See Also
CloneTransformer
constantTransformerback to summary
public static <I, O> Transformer<I, O> constantTransformer(final O constantToReturn)

Creates a Transformer that will return the same object each time the transformer is used.

Parameters
<I>
the input type
<O>
the output type
constantToReturn:O

the constant object to return each time in the transformer

Returns:Transformer<I, O>

the transformer.

See Also
ConstantTransformer
exceptionTransformerback to summary
public static <I, O> Transformer<I, O> exceptionTransformer()

Gets a transformer that always throws an exception. This could be useful during testing as a placeholder.

Parameters
<I>
the input type
<O>
the output type
Returns:Transformer<I, O>

the transformer

See Also
ExceptionTransformer
ifTransformerback to summary
public static <T> Transformer<T, T> ifTransformer(final Predicate<? super T> predicate, final Transformer<? super T, ? extends T> trueTransformer)

Create a new Transformer that calls the transformer if the predicate is true, otherwise the input object is returned unchanged.

Parameters
<T>
the input / output type
predicate:Predicate<? super T>

the predicate to switch on

trueTransformer:Transformer<? super T, ? extends T>

the transformer called if the predicate is true

Returns:Transformer<T, T>

the transformer

Exceptions
NullPointerException:
if either the predicate or transformer is null
Since
4.1
See Also
IfTransformer
ifTransformerback to summary
public static <I, O> Transformer<I, O> ifTransformer(final Predicate<? super I> predicate, final Transformer<? super I, ? extends O> trueTransformer, final Transformer<? super I, ? extends O> falseTransformer)

Create a new Transformer that calls one of two transformers depending on the specified predicate.

Parameters
<I>
the input type
<O>
the output type
predicate:Predicate<? super I>

the predicate to switch on

trueTransformer:Transformer<? super I, ? extends O>

the transformer called if the predicate is true

falseTransformer:Transformer<? super I, ? extends O>

the transformer called if the predicate is false

Returns:Transformer<I, O>

the transformer

Exceptions
NullPointerException:
if either the predicate or transformer is null
Since
4.1
See Also
IfTransformer
instantiateTransformerback to summary
public static <T> Transformer<Class<? extends T>, T> instantiateTransformer()

Gets a Transformer that expects an input Class object that it will instantiate.

Parameters
<T>
the output type
Returns:Transformer<Class<? extends T>, T>

the transformer

See Also
InstantiateTransformer
instantiateTransformerback to summary
public static <T> Transformer<Class<? extends T>, T> instantiateTransformer(final Class<?>[] paramTypes, final Object[] args)

Creates a Transformer that expects an input Class object that it will instantiate. The constructor used is determined by the arguments specified to this method.

Parameters
<T>
the output type
paramTypes:Class<?>[]

parameter types for the constructor, can be null

args:Object[]

the arguments to pass to the constructor, can be null

Returns:Transformer<Class<? extends T>, T>

the transformer

Exceptions
IllegalArgumentException:
if the paramTypes and args don't match
See Also
InstantiateTransformer
invokerTransformerback to summary
public static <I, O> Transformer<I, O> invokerTransformer(final String methodName)

Gets a Transformer that invokes a method on the input object. The method must have no parameters. If the input object is null, null is returned.

For example, TransformerUtils.invokerTransformer("getName"); will call the getName method on the input object to determine the transformer result.

Parameters
<I>
the input type
<O>
the output type
methodName:String

the method name to call on the input object, may not be null

Returns:Transformer<I, O>

the transformer

Exceptions
NullPointerException:
if the methodName is null.
See Also
InvokerTransformer
invokerTransformerback to summary
public static <I, O> Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes, final Object[] args)

Gets a Transformer that invokes a method on the input object. The method parameters are specified. If the input object is null, null is returned.

Parameters
<I>
the input type
<O>
the output type
methodName:String

the name of the method

paramTypes:Class<?>[]

the parameter types

args:Object[]

the arguments

Returns:Transformer<I, O>

the transformer

Exceptions
NullPointerException:
if the method name is null
IllegalArgumentException:
if the paramTypes and args don't match
See Also
InvokerTransformer
mapTransformerback to summary
public static <I, O> Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map)

Creates a Transformer that uses the passed in Map to transform the input object (as a simple lookup).

Parameters
<I>
the input type
<O>
the output type
map:Map<? super I, ? extends O>

the map to use to transform the objects

Returns:Transformer<I, O>

the transformer, or ConstantTransformer#nullTransformer() if the map is null

See Also
MapTransformer
nopTransformerback to summary
public static <T> Transformer<T, T> nopTransformer()

Gets a transformer that returns the input object. The input object should be immutable to maintain the contract of Transformer (although this is not checked).

Parameters
<T>
the input/output type
Returns:Transformer<T, T>

the transformer

See Also
NOPTransformer
nullTransformerback to summary
public static <I, O> Transformer<I, O> nullTransformer()

Gets a transformer that always returns null.

Parameters
<I>
the input type
<O>
the output type
Returns:Transformer<I, O>

the transformer

See Also
ConstantTransformer
stringValueTransformerback to summary
public static <T> Transformer<T, String> stringValueTransformer()

Gets a transformer that returns a java.lang.String representation of the input object. This is achieved via the toString method, null returns 'null'.

Parameters
<T>
the input type
Returns:Transformer<T, String>

the transformer

See Also
StringValueTransformer
switchMapTransformerback to summary
public static <I, O> Transformer<I, O> switchMapTransformer(final Map<I, Transformer<I, O>> objectsAndTransformers)

Create a new Transformer that uses the input object as a key to find the transformer to call.

The Map consists of object keys and Transformer values. A transformer is called if the input object equals the key. If there is no match, the default transformer is called. The default transformer is set in the map using a null key. If no default is set, null will be returned in a default case.

Parameters
<I>
the input type
<O>
the output type
objectsAndTransformers:Map<I, Transformer<I, O>>

a map of objects to transformers

Returns:Transformer<I, O>

the transformer

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
  • if the map is null
  • if any transformer in the map is null
See Also
SwitchTransformer
switchTransformerback to summary
public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I> predicate, final Transformer<? super I, ? extends O> trueTransformer, final Transformer<? super I, ? extends O> falseTransformer)

Create a new Transformer that calls one of two transformers depending on the specified predicate.

Parameters
<I>
the input type
<O>
the output type
predicate:Predicate<? super I>

the predicate to switch on

trueTransformer:Transformer<? super I, ? extends O>

the transformer called if the predicate is true

falseTransformer:Transformer<? super I, ? extends O>

the transformer called if the predicate is false

Returns:Transformer<I, O>

the transformer

Annotations
@SuppressWarnings:unchecked
@Deprecated
Exceptions
NullPointerException:
if either the predicate or transformer is null
See Also
SwitchTransformer
switchTransformerback to summary
public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers)

Create a new Transformer that calls one of the transformers depending on the predicates. The transformer at array location 0 is called if the predicate at array location 0 returned true. Each predicate is evaluated until one returns true. If no predicates evaluate to true, null is returned.

Parameters
<I>
the input type
<O>
the output type
predicates:Predicate<? super I>[]

an array of predicates to check

transformers:Transformer<? super I, ? extends O>[]

an array of transformers to call

Returns:Transformer<I, O>

the transformer

Exceptions
NullPointerException:
  • if the either array is null
  • if any element in the arrays is null
IllegalArgumentException:
if the arrays have different sizes
See Also
SwitchTransformer
switchTransformerback to summary
public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O> defaultTransformer)

Create a new Transformer that calls one of the transformers depending on the predicates. The transformer at array location 0 is called if the predicate at array location 0 returned true. Each predicate is evaluated until one returns true. If no predicates evaluate to true, the default transformer is called. If the default transformer is null, null is returned.

Parameters
<I>
the input type
<O>
the output type
predicates:Predicate<? super I>[]

an array of predicates to check

transformers:Transformer<? super I, ? extends O>[]

an array of transformers to call

defaultTransformer:Transformer<? super I, ? extends O>

the default to call if no predicate matches, null means return null

Returns:Transformer<I, O>

the transformer

Exceptions
NullPointerException:
  • if the either array is null
  • if any element in the arrays is null
IllegalArgumentException:
if the arrays have different sizes
See Also
SwitchTransformer
switchTransformerback to summary
public static <I, O> Transformer<I, O> switchTransformer(final Map<Predicate<I>, Transformer<I, O>> 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. If no default transformer is set, null will be returned in a default case. The ordering is that of the iterator() method on the entryset collection of the map.

Parameters
<I>
the input type
<O>
the output type
predicatesAndTransformers:Map<Predicate<I>, Transformer<I, O>>

a map of predicates to transformers

Returns:Transformer<I, O>

the transformer

Exceptions
NullPointerException:
  • if the map is null
  • if any transformer in the map is null
ClassCastException:
if the map elements are of the wrong type
See Also
SwitchTransformer