|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.apache.commons.collections15.TransformerUtils
public class TransformerUtils
TransformerUtils provides reference implementations and
utilities for the Transformer functor interface. The supplied transformers are:
java.lang.String representation of the input object
| Constructor Summary | |
|---|---|
TransformerUtils()
This class is not normally instantiated. |
|
| Method Summary | ||
|---|---|---|
static
|
asTransformer(Closure<T> closure)
Creates a Transformer that calls a Closure each time the transformer is used. |
|
static
|
asTransformer(Factory<T> factory)
Creates a Transformer that calls a Factory each time the transformer is used. |
|
static
|
asTransformer(Predicate<T> predicate)
Creates a Transformer that calls a Predicate each time the transformer is used. |
|
static
|
chainedTransformer(Collection transformers)
Create a new Transformer that calls each transformer in turn, passing the result into the next transformer. |
|
static
|
chainedTransformer(Transformer[] transformers)
Create a new Transformer that calls each transformer in turn, passing the result into the next transformer. |
|
static
|
chainedTransformer(Transformer<I,? extends M> transformer1,
Transformer<? super M,O> transformer2)
Create a new Transformer that calls two transformers, passing the result of the first into the second. |
|
static Transformer |
cloneTransformer()
Gets a transformer that returns a clone of the input object. |
|
static
|
constantTransformer(T constantToReturn)
Creates a Transformer that will return the same object each time the transformer is used. |
|
static Transformer |
exceptionTransformer()
Gets a transformer that always throws an exception. |
|
static Transformer<Class,Object> |
instantiateTransformer()
Gets a Transformer that expects an input Class object that it will instantiate. |
|
static Transformer<Class,Object> |
instantiateTransformer(Class[] paramTypes,
Object[] args)
Creates a Transformer that expects an input Class object that it will instantiate. |
|
static Transformer |
invokerTransformer(String methodName)
Gets a Transformer that invokes a method on the input object. |
|
static Transformer |
invokerTransformer(String methodName,
Class[] paramTypes,
Object[] args)
Gets a Transformer that invokes a method on the input object. |
|
static
|
mapTransformer(Map<I,O> map)
Creates a Transformer that uses the passed in Map to transform the input object (as a simple lookup). |
|
static Transformer |
nopTransformer()
Gets a transformer that returns the input object. |
|
static Transformer |
nullTransformer()
Gets a transformer that always returns null. |
|
static
|
stringValueTransformer()
Gets a transformer that returns a java.lang.String
representation of the input object. |
|
static
|
switchMapTransformer(Map<I,Transformer<? super I,? extends O>> objectsAndTransformers)
Create a new Transformer that uses the input object as a key to find the transformer to call. |
|
static
|
switchTransformer(Map<Predicate<? super I>,Transformer<? super I,? extends O>> predicatesAndTransformers)
Create a new Transformer that calls one of the transformers depending on the predicates. |
|
static
|
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
|
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
|
switchTransformer(Predicate<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. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public TransformerUtils()
| Method Detail |
|---|
public static Transformer exceptionTransformer()
ExceptionTransformerpublic static Transformer nullTransformer()
ConstantTransformerpublic static Transformer nopTransformer()
NOPTransformerpublic static Transformer cloneTransformer()
CloneTransformerpublic static <T> Transformer<Object,T> constantTransformer(T constantToReturn)
constantToReturn - the constant object to return each time in the transformer
ConstantTransformerpublic static <T> Transformer<T,T> asTransformer(Closure<T> closure)
closure - the closure to run each time in the transformer, not null
IllegalArgumentException - if the closure is nullClosureTransformerpublic static <T> Transformer<T,Boolean> asTransformer(Predicate<T> predicate)
predicate - the predicate to run each time in the transformer, not null
IllegalArgumentException - if the predicate is nullPredicateTransformerpublic static <T> Transformer<Object,T> asTransformer(Factory<T> factory)
factory - the factory to run each time in the transformer, not null
IllegalArgumentException - if the factory is nullFactoryTransformer
public static <I,M,O> Transformer<I,O> chainedTransformer(Transformer<I,? extends M> transformer1,
Transformer<? super M,O> transformer2)
transformer1 - the first transformertransformer2 - the second transformer
IllegalArgumentException - if either transformer is nullChainedTransformerpublic static <I,O> Transformer<I,O> chainedTransformer(Transformer[] transformers)
transformers - an array of transformers to chain
IllegalArgumentException - if the transformers array is null
IllegalArgumentException - if any transformer in the array is nullChainedTransformerpublic static <I,O> Transformer<I,O> chainedTransformer(Collection transformers)
transformers - a collection of transformers to chain
IllegalArgumentException - if the transformers collection is null
IllegalArgumentException - if any transformer in the collection is nullChainedTransformer
public static <I,O> Transformer<I,O> switchTransformer(Predicate<I> predicate,
Transformer<? super I,? extends O> trueTransformer,
Transformer<? super I,? extends O> falseTransformer)
predicate - the predicate to switch ontrueTransformer - the transformer called if the predicate is truefalseTransformer - the transformer called if the predicate is false
IllegalArgumentException - if the predicate is null
IllegalArgumentException - if either transformer is nullSwitchTransformer
public static <I,O> Transformer<I,O> switchTransformer(Predicate<? super I>[] predicates,
Transformer<? super I,? extends O>[] transformers)
predicates - an array of predicates to checktransformers - an array of transformers to call
IllegalArgumentException - if the either array is null
IllegalArgumentException - if the either array has 0 elements
IllegalArgumentException - if any element in the arrays is null
IllegalArgumentException - if the arrays are 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)
predicates - an array of predicates to checktransformers - an array of transformers to calldefaultTransformer - the default to call if no predicate matches, null means return null
IllegalArgumentException - if the either array is null
IllegalArgumentException - if the either array has 0 elements
IllegalArgumentException - if any element in the arrays is null
IllegalArgumentException - if the arrays are different sizesSwitchTransformerpublic static <I,O> Transformer<I,O> switchTransformer(Map<Predicate<? super I>,Transformer<? super I,? extends O>> predicatesAndTransformers)
predicatesAndTransformers - a map of predicates to transformers
IllegalArgumentException - if the map is null
IllegalArgumentException - if the map is empty
IllegalArgumentException - if any transformer in the map is null
ClassCastException - if the map elements are of the wrong typeSwitchTransformerpublic static <I,O> Transformer<I,O> switchMapTransformer(Map<I,Transformer<? super I,? extends O>> objectsAndTransformers)
objectsAndTransformers - a map of objects to transformers
IllegalArgumentException - if the map is null
IllegalArgumentException - if the map is empty
IllegalArgumentException - if any transformer in the map is nullSwitchTransformerpublic static Transformer<Class,Object> instantiateTransformer()
InstantiateTransformer
public static Transformer<Class,Object> instantiateTransformer(Class[] paramTypes,
Object[] args)
paramTypes - parameter types for the constructor, can be nullargs - the arguments to pass to the constructor, can be null
IllegalArgumentException - if the paramTypes and args don't matchInstantiateTransformerpublic static <I,O> Transformer<I,O> mapTransformer(Map<I,O> map)
map - the map to use to transform the objects
IllegalArgumentException - if the map is nullMapTransformerpublic static Transformer invokerTransformer(String methodName)
TransformerUtils.invokerTransformer("getName");
will call the getName/code> method on the input object to
determine the transformer result.
Note: no type saftey provided by Java 1.5.
- Parameters:
methodName - the method name to call on the input object, may not be null
- Returns:
- the transformer
- Throws:
IllegalArgumentException - if the methodName is null.- See Also:
InvokerTransformer
public static Transformer invokerTransformer(String methodName,
Class[] paramTypes,
Object[] args)
methodName - the name of the methodparamTypes - the parameter typesargs - the arguments
IllegalArgumentException - if the method name is null
IllegalArgumentException - if the paramTypes and args don't matchInvokerTransformerpublic static <T> Transformer<T,String> stringValueTransformer()
java.lang.String
representation of the input object. This is achieved via the
toString method, null returns 'null'.
StringValueTransformer
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||