|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.collections15.functors.transformer.TransformerUtils
public class TransformerUtils
TransformerUtils
provides reference implementations and
utilities for the Transformer
functor interface. The supplied
Transformer
s are:
java.lang.String
representation of the input object
Transformer
s are Serializable.
Constructor Summary | |
---|---|
protected |
TransformerUtils()
Protected constructor prevents direct instantiation, but allows users to extend this library to provide their own augmented static library class. |
Method Summary | ||
---|---|---|
static
|
asTransformer(Closure<T> closure)
Returns a Transformer that executes a Closure
on the input, before returning it as output. |
|
static
|
asTransformer(Factory<O> factory)
Returns a Transformer implementation whose
transform method uses a Factory to create the
returned output object. |
|
static
|
asTransformer(Predicate<T> predicate)
Returns a Transformer that evaluates a
Predicate each time the Transformer is used,
with the result of the Predicate returned as output. |
|
static
|
chainedTransformer(Transformer<? super I,? extends M> transformer1,
Transformer<? super M,? extends O> transformer2)
Returns a Transformer whose transform method
chains together the transform methods of two existing
Transformer s
If you need to chain together more than two Transformer s,
you can use ChainedTransformer.getInstance(net.sf.collections15.Transformer super I, ? extends M>, net.sf.collections15.Transformer super M, ? extends O>) and the convenience
methods ChainedTransformer.append(net.sf.collections15.Transformer super O, T>) and ChainedTransformer.prepend(net.sf.collections15.Transformer instead. |
|
static
|
cloneTransformer()
Returns a Transformer that returns a clone of the input
object. |
|
static
|
constantTransformer(O constantToReturn)
Returns a Transformer that returns the same object each time
the transformer is used. |
|
static
|
exceptionTransformer()
Returns a Transformer that always throws an exception. |
|
static
|
ifElseTransformer(Predicate<I> predicate,
Transformer<I,O> trueTransformer,
Transformer<I,O> falseTransformer)
Returns a Transformer that delegates to one of two existing
Transformers , depending on the result of a
Predicate . |
|
static
|
instantiateTransformer()
Returns a Transformer that takes an input Class
object and uses it to output an instance of that class, created by
reflection using the parameterless constructor of the
Class . |
|
static
|
instantiateTransformer(java.lang.Class[] parameterTypes,
java.lang.Object[] arguments)
Returns a Transformer that takes an input Class
object and uses it to output an instance of that class, created by
reflection using the constructor of the Class specified by
the parameterTypes argument, passing in the corresponding
arguments to the constructor. |
|
static
|
invokerTransformer(java.lang.String methodName)
Returns a Transformer that invokes a specified method on the
input object. |
|
static
|
invokerTransformer(java.lang.String methodName,
java.lang.Class[] parameterTypes,
java.lang.Object[] arguments)
Returns a Transformer that invokes a specified method on the
input object. |
|
static
|
mapTransformer(java.util.Map<? extends I,? extends O> map)
Returns a Transformer that uses the specified map as a
lookup to transform the input objects into the output objects. |
|
static
|
nopTransformer()
Returns a Transformer that simply returns the input object. |
|
static
|
nullTransformer()
Returns a Transformer that always returns
null . |
|
static
|
stringValueTransformer()
Returns a Transformer that outputs the string value of the
input, using the String.valueOf method. |
|
static
|
switchMapTransformer(java.util.Map<I,Transformer<I,O>> objectsAndTransformers)
Returns a Transformer that uses the input object as a key to
find the Transformer to delegate to. |
|
static
|
switchTransformer(java.util.Map<Predicate<I>,Transformer<I,O>> predicatesAndTransformers)
Returns a Transformer instance that uses the specified map
of Predicates and Transformer s, delegating to
the Transformer whose corresponding Predicate
evaluates to true . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected TransformerUtils()
Method Detail |
---|
public static <I,O> Transformer<I,O> exceptionTransformer()
Transformer
that always throws an exception. This
could be useful during testing as a placeholder.
Transformer
that always throws an exception.ExceptionTransformer
public static <I,O> Transformer<I,O> nullTransformer()
Transformer
that always returns
null
.
Transformer
that always returns
null
.ConstantTransformer
public static <T> Transformer<T,T> nopTransformer()
Transformer
that simply returns the input object.
The input object should be immutable to maintain the contract of
Transformer
(although this is not checked).
Transformer
that simply returns the input object.NOPTransformer
public static <T> Transformer<T,T> cloneTransformer()
Transformer
that returns a clone of the input
object. The input object will be cloned using one of these techniques (in
order):
Transformer
that returns a clone of the input
object.CloneTransformer
public static <I,O> Transformer<I,O> constantTransformer(O constantToReturn)
Transformer
that returns the same object each time
the transformer is used.
constantToReturn
- The constant object to be returned by the
Transformer
's transform
method.
Transformer
that returns the same object each time
the transformer is used.ConstantTransformer
public static <T> Transformer<T,T> asTransformer(Closure<T> closure)
Transformer
that executes a Closure
on the input, before returning it as output.
closure
- The Closure
to execute in the transform.
Transformer
that executes a Closure
on the input, before returning it as output.
java.lang.IllegalArgumentException
- Throws if the Closure
is
null
.ClosureTransformer
public static <T> Transformer<T,java.lang.Boolean> asTransformer(Predicate<T> predicate)
Transformer
that evaluates a
Predicate
each time the Transformer
is used,
with the result of the Predicate
returned as output.
predicate
- The Predicate
to execute in the transform.
Transformer
that evaluates a Predicate
each time the Transformer
is used, with the result
of the Predicate
returned as output.
java.lang.IllegalArgumentException
- Thrown if the Predicate
is
null
.PredicateTransformer
public static <I,O> Transformer<I,O> asTransformer(Factory<O> factory)
Transformer
implementation whose
transform
method uses a Factory
to create the
returned output object.
factory
- The factory used to create the transform
output objects.
- Returns:
- A
Transformer
implementation whose
transform
method uses a Factory
to create
the returned output object.
- Throws:
java.lang.IllegalArgumentException
- Thrown if the Factory
is
null
.- See Also:
FactoryTransformer
public static <I,M,O> Transformer<I,O> chainedTransformer(Transformer<? super I,? extends M> transformer1, Transformer<? super M,? extends O> transformer2)
Transformer
whose transform
method
chains together the transform
methods of two existing
Transformer
s
If you need to chain together more than two Transformer
s,
you can use ChainedTransformer.getInstance(net.sf.collections15.Transformer super I, ? extends M>, net.sf.collections15.Transformer super M, ? extends O>)
and the convenience
methods ChainedTransformer.append(net.sf.collections15.Transformer super O, T>)
and ChainedTransformer.prepend(net.sf.collections15.Transformer)
instead.
transformer1
- The first Transformer
.transformer2
- the second Transformer
.
Transformer
whose transform
method
chains together the transform
methods of two
existing Transformer
s
java.lang.IllegalArgumentException
- Thrown if either Transformer
is null
.ChainedTransformer
public static <I,O> Transformer<I,O> ifElseTransformer(Predicate<I> predicate, Transformer<I,O> trueTransformer, Transformer<I,O> falseTransformer)
Transformer
that delegates to one of two existing
Transformers
, depending on the result of a
Predicate
.
predicate
- The Predicate
evaluated to determine
which of the Transformer
s to
delegate to.trueTransformer
- The Transformer
used if the
Predicate
evaluates true
.falseTransformer
- The Transformer
used if the
Predicate
evaluates false
.
Transformer
that delegates to one of two existing
Transformers
, depending on the result of a
Predicate
.
java.lang.IllegalArgumentException
- Thrown if any argument is null
.SwitchTransformer
public static <I,O> Transformer<I,O> switchTransformer(java.util.Map<Predicate<I>,Transformer<I,O>> predicatesAndTransformers)
Transformer
instance that uses the specified map
of Predicates
and Transformer
s, delegating to
the Transformer
whose corresponding Predicate
evaluates to true
.
predicatesAndTransformers
- A map of Predicate
s to
Transformer
s. Each non-null
Predicate
key defines a
single switch case, with the
corresponding Transformer
being used to transform an input object
if it matches the Predicate
.
Predicate
s are evaluated in
the iteration order of the map's key
set, with the exception of the
null Predicate
key. If a
null Predicate
key is
specified in the set, it's corresponding
Transformer
is treated as
the default case, used to transform an
input object if it doesn't satisfy any
of the non-null Predicate
cases. If there is no default
Transformer
, an
IllegalArgumentException
will be thrown by the transform
method.
Note that the map is not modified by
this method.
Predicates
and Transformer
s.
java.lang.IllegalArgumentException
- Thrown if the map is null
.
java.lang.IllegalArgumentException
- Thrown if any Transformer
in the map is null
.public static <I,O> Transformer<I,O> switchMapTransformer(java.util.Map<I,Transformer<I,O>> objectsAndTransformers)
Transformer
that uses the input object as a key to
find the Transformer
to delegate to.
objectsAndTransformers
- A map consisting of object keys and
Transformer
values. The map is
used to obtain a Transformer
for an input object, which is then used to
tansform the input object. If there is no
matching Transformer
, the
default Transformer
is used
instead (specified in the map using a
null
key). If there is no
default Transformer
, an
IllegalArgumentException
will
be thrown by the transform
method.
Transformer
that uses the input object as a key to
find the Transformer
to delegate to.
java.lang.IllegalArgumentException
- Thrown if the map is null
,
empty, or any Transformer
in the map is null
.SwitchTransformer
public static <T> Transformer<java.lang.Class<? extends T>,T> instantiateTransformer()
Transformer
that takes an input Class
object and uses it to output an instance of that class, created by
reflection using the parameterless constructor of the
Class
.
Transformer
that takes an input Class
object and uses it to output an instance of that class, created
by reflection using the parameterless constructor of the
Class
.InstantiateTransformer
,
InstantiateTransformer.transform(java.lang.Class extends E>)
public static <T> Transformer<java.lang.Class<? extends T>,T> instantiateTransformer(java.lang.Class[] parameterTypes, java.lang.Object[] arguments)
Transformer
that takes an input Class
object and uses it to output an instance of that class, created by
reflection using the constructor of the Class
specified by
the parameterTypes
argument, passing in the corresponding
arguments
to the constructor.
parameterTypes
- The parameter types of the constructer to invoke on
an input Class
. May be
null
or empty if arguments
is also null
or empty, in which case
the parameterless constructor will be used to
create an output object from an input
Class
. The contents of this array are
defensively copied.arguments
- The arguments to pass to the constructor invoked on
an input Class
.
Transformer
that takes an input Class
object and uses it to output an instance of that class, created
by reflection using the constructor of the Class
specified by the parameterTypes
argument, passing in
the corresponding arguments
to the constructor.
java.lang.IllegalArgumentException
- Thrown if either of parameterTypes
or arguments
is
null
, but the other is
not.
java.lang.IllegalArgumentException
- Thrown if any element in parameterTypes
is null
.
java.lang.IllegalArgumentException
- Thrown if any none-null
element in arguments
is not
an instance of its corresponding
parameterTypes
class.InstantiateTransformer
,
InstantiateTransformer.transform(java.lang.Class extends E>)
public static <I,O> Transformer<I,O> mapTransformer(java.util.Map<? extends I,? extends O> map)
Transformer
that uses the specified map as a
lookup to transform the input objects into the output objects.
map
- The map used as a lookup to transform the input objects into
output objects.
Transformer
that uses the specified map as a
lookup to transform the input objects into the output objects.
java.lang.IllegalArgumentException
- if the map is null
.MapTransformer
public static <I,O> Transformer<I,O> invokerTransformer(java.lang.String methodName)
Transformer
that invokes a specified method on the
input object. The method must have no parameters.
For example, TransformerUtils.invokerTransformer("getName");
will call the getName/code> method on the input object and output
the return value.
If a null Class
is input to the Transformer
it
will throw an IllegalArgumentException
.
- Parameters:
methodName
- the method name to call on the input object, may not be
null. The return type of the method must be assignable
to the generic type <O>
- Returns:
- A
Transformer
that invokes a specified method on the
input object. The method must have no parameters.
- Throws:
java.lang.IllegalArgumentException
- if methodName
is null.- See Also:
InvokerTransformer
,
InvokerTransformer.transform(I)
public static <I,O> Transformer<I,O> invokerTransformer(java.lang.String methodName, java.lang.Class[] parameterTypes, java.lang.Object[] arguments)
Transformer
that invokes a specified method on the
input object.
methodName
- the method name to call on the input object, may
not be null. The return type of the method must be
assignable to the generic type <O>
parameterTypes
- The parameter types of the method to invoke on an
input object. May be null
or empty if
arguments
is also null
or
empty, in which case the parameterless mehod of the
specified name will be used. The contents of this
array are defensively copied.arguments
- The arguments to pass to the method invoked on an
input Class
.
Transformer
that invokes a specified method on the
input object.
java.lang.IllegalArgumentException
- Thrown if the method name argument is
null
.
java.lang.IllegalArgumentException
- Thrown if either of parameterTypes
or arguments
is
null
, but the other is
not.
java.lang.IllegalArgumentException
- Thrown if any element in parameterTypes
is null
.
java.lang.IllegalArgumentException
- Thrown if any none-null
element in arguments
is not
an instance of its corresponding
parameterTypes
class.InvokerTransformer
,
InvokerTransformer.transform(I)
public static <E> Transformer<E,java.lang.String> stringValueTransformer()
Transformer
that outputs the string value of the
input, using the String.valueOf
method. null
returns 'null'.
Transformer
that outputs the string value of the
input, using the String.valueOf
method.StringValueTransformer
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |