public class TransformerUtils
extends java.lang.Object
TransformerUtils
provides reference implementations and
utilities for the Transformer functor interface. The supplied transformers are:
java.lang.String
representation of the input object
Since v4.1 only transformers which are considered to be unsafe are Serializable. Transformers considered to be unsafe for serialization are:
Modifier and Type | Method and Description |
---|---|
static <T> Transformer<T,T> |
asTransformer(Closure<? super T> closure)
Creates a Transformer that calls a Closure each time the transformer is used.
|
static <I,O> Transformer<I,O> |
asTransformer(Factory<? extends O> factory)
Creates a Transformer that calls a Factory each time the transformer is used.
|
static <T> Transformer<T,java.lang.Boolean> |
asTransformer(Predicate<? super T> predicate)
Creates a Transformer that calls a Predicate each time the transformer is used.
|
static <T> Transformer<T,T> |
chainedTransformer(java.util.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.
|
static <T> Transformer<T,T> |
chainedTransformer(Transformer<? super T,? extends T>... transformers)
Create a new Transformer that calls each transformer in turn, passing the
result into the next transformer.
|
static <T> Transformer<T,T> |
cloneTransformer()
Gets a transformer that returns a clone of the input object.
|
static <I,O> Transformer<I,O> |
constantTransformer(O constantToReturn)
Creates a Transformer that will return the same object each time the
transformer is used.
|
static <I,O> Transformer<I,O> |
exceptionTransformer()
Gets a transformer that always throws an exception.
|
static <I,O> Transformer<I,O> |
ifTransformer(Predicate<? super I> predicate,
Transformer<? super I,? extends O> trueTransformer,
Transformer<? super I,? extends O> falseTransformer)
Create a new Transformer that calls one of two transformers depending
on the specified predicate.
|
static <T> Transformer<T,T> |
ifTransformer(Predicate<? super T> predicate,
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.
|
static <T> Transformer<java.lang.Class<? extends T>,T> |
instantiateTransformer()
Gets a Transformer that expects an input Class object that it will instantiate.
|
static <T> Transformer<java.lang.Class<? extends T>,T> |
instantiateTransformer(java.lang.Class<?>[] paramTypes,
java.lang.Object[] args)
Creates a Transformer that expects an input Class object that it will
instantiate.
|
static <I,O> Transformer<I,O> |
invokerTransformer(java.lang.String methodName)
Gets a Transformer that invokes a method on the input object.
|
static <I,O> Transformer<I,O> |
invokerTransformer(java.lang.String methodName,
java.lang.Class<?>[] paramTypes,
java.lang.Object[] args)
Gets a Transformer that invokes a method on the input object.
|
static <I,O> Transformer<I,O> |
mapTransformer(java.util.Map<? super I,? extends O> map)
Creates a Transformer that uses the passed in Map to transform the input
object (as a simple lookup).
|
static <T> Transformer<T,T> |
nopTransformer()
Gets a transformer that returns the input object.
|
static <I,O> Transformer<I,O> |
nullTransformer()
Gets a transformer that always returns null.
|
static <T> Transformer<T,java.lang.String> |
stringValueTransformer()
Gets a transformer that returns a
java.lang.String
representation of the input object. |
static <I,O> Transformer<I,O> |
switchMapTransformer(java.util.Map<I,Transformer<I,O>> objectsAndTransformers)
Create a new Transformer that uses the input object as a key to find the
transformer to call.
|
static <I,O> Transformer<I,O> |
switchTransformer(java.util.Map<Predicate<I>,Transformer<I,O>> predicatesAndTransformers)
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)
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)
Create a new Transformer that calls one of the transformers depending
on the predicates.
|
static <I,O> Transformer<I,O> |
switchTransformer(Predicate<? super I> predicate,
Transformer<? super I,? extends O> trueTransformer,
Transformer<? super I,? extends O> falseTransformer)
Deprecated.
as of 4.1, use {@link #ifTransformer(Predicate, Transformer, Transformer))
|
public static <I,O> Transformer<I,O> exceptionTransformer()
I
- the input typeO
- the output typeExceptionTransformer
public static <I,O> Transformer<I,O> nullTransformer()
I
- the input typeO
- the output typeConstantTransformer
public static <T> Transformer<T,T> nopTransformer()
T
- the input/output typeNOPTransformer
public static <T> Transformer<T,T> cloneTransformer()
T
- the input/output typeCloneTransformer
public static <I,O> Transformer<I,O> constantTransformer(O constantToReturn)
I
- the input typeO
- the output typeconstantToReturn
- the constant object to return each time in the transformerConstantTransformer
public static <T> Transformer<T,T> asTransformer(Closure<? super T> closure)
T
- the input/output typeclosure
- the closure to run each time in the transformer, not nulljava.lang.NullPointerException
- if the closure is nullClosureTransformer
public static <T> Transformer<T,java.lang.Boolean> asTransformer(Predicate<? super T> predicate)
T
- the input typepredicate
- the predicate to run each time in the transformer, not nulljava.lang.NullPointerException
- if the predicate is nullPredicateTransformer
public static <I,O> Transformer<I,O> asTransformer(Factory<? extends O> factory)
I
- the input typeO
- the output typefactory
- the factory to run each time in the transformer, not nulljava.lang.NullPointerException
- if the factory is nullFactoryTransformer
public static <T> Transformer<T,T> chainedTransformer(Transformer<? super T,? extends T>... transformers)
T
- the input/output typetransformers
- an array of transformers to chainjava.lang.NullPointerException
- if the transformers array or any of the transformers is nullChainedTransformer
public static <T> Transformer<T,T> chainedTransformer(java.util.Collection<? extends Transformer<? super T,? extends T>> transformers)
T
- the input/output typetransformers
- a collection of transformers to chainjava.lang.NullPointerException
- if the transformers collection or any of the transformers is nullChainedTransformer
public static <T> Transformer<T,T> ifTransformer(Predicate<? super T> predicate, Transformer<? super T,? extends T> trueTransformer)
T
- the input / output typepredicate
- the predicate to switch ontrueTransformer
- the transformer called if the predicate is truejava.lang.NullPointerException
- if either the predicate or transformer is nullIfTransformer
public static <I,O> Transformer<I,O> ifTransformer(Predicate<? super I> predicate, Transformer<? super I,? extends O> trueTransformer, Transformer<? super I,? extends O> falseTransformer)
I
- the input typeO
- the output typepredicate
- the predicate to switch ontrueTransformer
- the transformer called if the predicate is truefalseTransformer
- the transformer called if the predicate is falsejava.lang.NullPointerException
- if either the predicate or transformer is nullIfTransformer
@Deprecated public static <I,O> Transformer<I,O> switchTransformer(Predicate<? super I> predicate, Transformer<? super I,? extends O> trueTransformer, Transformer<? super I,? extends O> falseTransformer)
I
- the input typeO
- the output typepredicate
- the predicate to switch ontrueTransformer
- the transformer called if the predicate is truefalseTransformer
- the transformer called if the predicate is falsejava.lang.NullPointerException
- if either the predicate or transformer is nullSwitchTransformer
public static <I,O> Transformer<I,O> switchTransformer(Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers)
I
- the input typeO
- the output typepredicates
- an array of predicates to checktransformers
- an array of transformers to calljava.lang.NullPointerException
- if the either array is nulljava.lang.NullPointerException
- if any element in the arrays is nulljava.lang.IllegalArgumentException
- if the arrays have different sizesSwitchTransformer
public 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
- an array of predicates to checktransformers
- an array of transformers to calldefaultTransformer
- the default to call if no predicate matches, null means return nulljava.lang.NullPointerException
- if the either array is nulljava.lang.NullPointerException
- if any element in the arrays is nulljava.lang.IllegalArgumentException
- if the arrays have different sizesSwitchTransformer
public static <I,O> Transformer<I,O> switchTransformer(java.util.Map<Predicate<I>,Transformer<I,O>> predicatesAndTransformers)
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.
I
- the input typeO
- the output typepredicatesAndTransformers
- a map of predicates to transformersjava.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 typeSwitchTransformer
public static <I,O> Transformer<I,O> switchMapTransformer(java.util.Map<I,Transformer<I,O>> objectsAndTransformers)
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.
I
- the input typeO
- the output typeobjectsAndTransformers
- a map of objects to transformersjava.lang.NullPointerException
- if the map is nulljava.lang.NullPointerException
- if any transformer in the map is nullSwitchTransformer
public static <T> Transformer<java.lang.Class<? extends T>,T> instantiateTransformer()
T
- the output typeInstantiateTransformer
public static <T> Transformer<java.lang.Class<? extends T>,T> instantiateTransformer(java.lang.Class<?>[] paramTypes, java.lang.Object[] args)
T
- the output typeparamTypes
- parameter types for the constructor, can be nullargs
- the arguments to pass to the constructor, can be nulljava.lang.IllegalArgumentException
- if the paramTypes and args don't matchInstantiateTransformer
public static <I,O> Transformer<I,O> mapTransformer(java.util.Map<? super I,? extends O> map)
I
- the input typeO
- the output typemap
- the map to use to transform the objectsConstantTransformer.nullTransformer()
if the
map
is null
MapTransformer
public static <I,O> Transformer<I,O> invokerTransformer(java.lang.String methodName)
For example, TransformerUtils.invokerTransformer("getName");
will call the getName/code> method on the input object to
determine the transformer result.
I
- the input typeO
- the output typemethodName
- the method name to call on the input object, may not be nulljava.lang.NullPointerException
- if the methodName is null.InvokerTransformer
public static <I,O> Transformer<I,O> invokerTransformer(java.lang.String methodName, java.lang.Class<?>[] paramTypes, java.lang.Object[] args)
null
,
null
is returned.I
- the input typeO
- the output typemethodName
- the name of the methodparamTypes
- the parameter typesargs
- the argumentsjava.lang.NullPointerException
- if the method name is nulljava.lang.IllegalArgumentException
- if the paramTypes and args don't matchInvokerTransformer
public static <T> Transformer<T,java.lang.String> stringValueTransformer()
java.lang.String
representation of the input object. This is achieved via the
toString
method, null
returns 'null'.T
- the input typeStringValueTransformer
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"