|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.collections.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 Transformer |
asTransformer(Closure closure)
Creates a Transformer that calls a Closure each time the transformer is used. |
static Transformer |
asTransformer(Factory factory)
Creates a Transformer that calls a Factory each time the transformer is used. |
static Transformer |
asTransformer(Predicate predicate)
Creates a Transformer that calls a Predicate each time the transformer is used. |
static Transformer |
chainedTransformer(Collection transformers)
Create a new Transformer that calls each transformer in turn, passing the result into the next transformer. |
static Transformer |
chainedTransformer(Transformer[] transformers)
Create a new Transformer that calls each transformer in turn, passing the result into the next transformer. |
static Transformer |
chainedTransformer(Transformer transformer1,
Transformer 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 Transformer |
constantTransformer(Object 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 |
instantiateTransformer()
Gets a Transformer that expects an input Class object that it will instantiate. |
static Transformer |
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 Transformer |
mapTransformer(Map 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 Transformer |
stringValueTransformer()
Gets a transformer that returns a java.lang.String
representation of the input object. |
static Transformer |
switchMapTransformer(Map objectsAndTransformers)
Create a new Transformer that uses the input object as a key to find the transformer to call. |
static Transformer |
switchTransformer(Map predicatesAndTransformers)
Create a new Transformer that calls one of the transformers depending on the predicates. |
static Transformer |
switchTransformer(Predicate[] predicates,
Transformer[] transformers)
Create a new Transformer that calls one of the transformers depending on the predicates. |
static Transformer |
switchTransformer(Predicate[] predicates,
Transformer[] transformers,
Transformer defaultTransformer)
Create a new Transformer that calls one of the transformers depending on the predicates. |
static Transformer |
switchTransformer(Predicate predicate,
Transformer trueTransformer,
Transformer 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()
public static Transformer nullTransformer()
public static Transformer nopTransformer()
public static Transformer cloneTransformer()
public static Transformer constantTransformer(Object constantToReturn)
constantToReturn
- the constant object to return each time in the transformerpublic static Transformer asTransformer(Closure closure)
closure
- the closure to run each time in the transformer, not nullIllegalArgumentException
- if the closure is nullpublic static Transformer asTransformer(Predicate predicate)
predicate
- the predicate to run each time in the transformer, not nullIllegalArgumentException
- if the predicate is nullpublic static Transformer asTransformer(Factory factory)
factory
- the factory to run each time in the transformer, not nullIllegalArgumentException
- if the factory is nullpublic static Transformer chainedTransformer(Transformer transformer1, Transformer transformer2)
transformer1
- the first transformertransformer2
- the second transformerIllegalArgumentException
- if either transformer is nullpublic static Transformer chainedTransformer(Transformer[] transformers)
transformers
- an array of transformers to chainIllegalArgumentException
- if the transformers array is nullIllegalArgumentException
- if any transformer in the array is nullpublic static Transformer chainedTransformer(Collection transformers)
transformers
- a collection of transformers to chainIllegalArgumentException
- if the transformers collection is nullIllegalArgumentException
- if any transformer in the collection is nullpublic static Transformer switchTransformer(Predicate predicate, Transformer trueTransformer, Transformer falseTransformer)
predicate
- the predicate to switch ontrueTransformer
- the transformer called if the predicate is truefalseTransformer
- the transformer called if the predicate is falseIllegalArgumentException
- if the predicate is nullIllegalArgumentException
- if either transformer is nullpublic static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers)
predicates
- an array of predicates to checktransformers
- an array of transformers to callIllegalArgumentException
- if the either array is nullIllegalArgumentException
- if the either array has 0 elementsIllegalArgumentException
- if any element in the arrays is nullIllegalArgumentException
- if the arrays are different sizespublic static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer 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 nullIllegalArgumentException
- if the either array is nullIllegalArgumentException
- if the either array has 0 elementsIllegalArgumentException
- if any element in the arrays is nullIllegalArgumentException
- if the arrays are different sizespublic static Transformer switchTransformer(Map 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.
predicatesAndTransformers
- a map of predicates to transformersIllegalArgumentException
- if the map is nullIllegalArgumentException
- if the map is emptyIllegalArgumentException
- if any transformer in the map is nullClassCastException
- if the map elements are of the wrong typepublic static Transformer switchMapTransformer(Map 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.
objectsAndTransformers
- a map of objects to transformersIllegalArgumentException
- if the map is nullIllegalArgumentException
- if the map is emptyIllegalArgumentException
- if any transformer in the map is nullpublic static Transformer instantiateTransformer()
public static Transformer instantiateTransformer(Class[] paramTypes, Object[] args)
paramTypes
- parameter types for the constructor, can be nullargs
- the arguments to pass to the constructor, can be nullIllegalArgumentException
- if the paramTypes and args don't matchpublic static Transformer mapTransformer(Map map)
map
- the map to use to transform the objectsIllegalArgumentException
- if the map is nullpublic static Transformer invokerTransformer(String methodName)
For example, TransformerUtils.invokerTransformer("getName");
will call the getName/code> method on the input object to
determine the transformer result.
methodName
- the method name to call on the input object, may not be nullIllegalArgumentException
- if the methodName is null.
public static Transformer invokerTransformer(String methodName, Class[] paramTypes, Object[] args)
methodName
- the name of the methodparamTypes
- the parameter typesargs
- the argumentsIllegalArgumentException
- if the method name is nullIllegalArgumentException
- if the paramTypes and args don't matchpublic static Transformer stringValueTransformer()
java.lang.String
representation of the input object. This is achieved via the
toString
method, null
returns 'null'.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |