# Class TransformerUtils

java.lang.Object
org.apache.commons.collections4.TransformerUtils

public class TransformerUtils extends Object
`TransformerUtils` provides reference implementations and utilities for the Transformer functor interface. The supplied transformers are:

- Invoker - returns the result of a method call on the input object
- Clone - returns a clone of the input object
- Constant - always returns the same object
- Closure - performs a Closure and returns the input object
- Predicate - returns the result of the predicate as a Boolean
- Factory - returns a new object from a factory
- Chained - chains two or more transformers together
- If - calls one transformer or another based on a predicate
- Switch - calls one transformer based on one or more predicates
- SwitchMap - calls one transformer looked up from a Map
- Instantiate - the Class input object is instantiated
- Map - returns an object from a supplied Map
- Null - always returns null
- NOP - returns the input object, which should be immutable
- Exception - always throws an exception
- StringValue - returns a `String` representation of the input object

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

- Invoker
- Clone
- Instantiate

Since:
  3.0

- ## Method Summary

  Modifier and Type
  Method
  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,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(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<Class<? extends T>,T>`
  `instantiateTransformer()`
  Gets a Transformer that expects an input Class object that it will instantiate.
  `static <T> Transformer<Class<? extends T>,T>`
  `instantiateTransformer(Class<?>[] paramTypes, Object[] args)`
  Creates a Transformer that expects an input Class object that it will instantiate.
  `static <I, O> Transformer<I,O>`
  `invokerTransformer(String methodName)`
  Gets a Transformer that invokes a method on the input object.
  `static <I, O> Transformer<I,O>`
  `invokerTransformer(String methodName, Class<?>[] paramTypes, Object[] args)`
  Gets a Transformer that invokes a method on the input object.
  `static <I, O> Transformer<I,O>`
  `mapTransformer(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,String>`
  `stringValueTransformer()`
  Gets a transformer that returns a `String` representation of the input object.
  `static <I, O> Transformer<I,O>`
  `switchMapTransformer(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(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 `ifTransformer(Predicate, Transformer, Transformer)`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### asTransformer

    public static <T> Transformer<T,T> asTransformer(Closure<? super T> closure)
    Creates a Transformer that calls a Closure each time the transformer is used. The transformer returns the input object.
    Type Parameters:
      `T` - the input/output type
    Parameters:
      `closure` - the closure to run each time in the transformer, not null
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the closure is null
    See Also:
      - `ClosureTransformer`

  - ### asTransformer

    public static <I, O> Transformer<I,O> asTransformer(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.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `factory` - the factory to run each time in the transformer, not null
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the factory is null
    See Also:
      - `FactoryTransformer`

  - ### asTransformer

    public static <T> Transformer<T,Boolean> asTransformer(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`.
    Type Parameters:
      `T` - the input type
    Parameters:
      `predicate` - the predicate to run each time in the transformer, not null
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the predicate is null
    See Also:
      - `PredicateTransformer`

  - ### chainedTransformer

    public static <T> Transformer<T,T> chainedTransformer(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.
    Type Parameters:
      `T` - the input/output type
    Parameters:
      `transformers` - a collection of transformers to chain
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the transformers collection or any of the transformers is null
    See Also:
      - `ChainedTransformer`

  - ### chainedTransformer

    public 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.
    Type Parameters:
      `T` - the input/output type
    Parameters:
      `transformers` - an array of transformers to chain
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the transformers array or any of the transformers is null
    See Also:
      - `ChainedTransformer`

  - ### cloneTransformer

    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
    Type Parameters:
      `T` - the input/output type
    Returns:
      the transformer
    See Also:
      - `CloneTransformer`

  - ### constantTransformer

    public static <I, O> Transformer<I,O> constantTransformer(O constantToReturn)
    Creates a Transformer that will return the same object each time the transformer is used.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `constantToReturn` - the constant object to return each time in the transformer
    Returns:
      the transformer.
    See Also:
      - `ConstantTransformer`

  - ### exceptionTransformer

    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.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Returns:
      the transformer
    See Also:
      - `ExceptionTransformer`

  - ### ifTransformer

    public 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.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `predicate` - the predicate to switch on
      `trueTransformer` - the transformer called if the predicate is true
      `falseTransformer` - the transformer called if the predicate is false
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if either the predicate or transformer is null
    Since:
      4.1
    See Also:
      - `IfTransformer`

  - ### ifTransformer

    public 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.
    Type Parameters:
      `T` - the input / output type
    Parameters:
      `predicate` - the predicate to switch on
      `trueTransformer` - the transformer called if the predicate is true
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if either the predicate or transformer is null
    Since:
      4.1
    See Also:
      - `IfTransformer`

  - ### instantiateTransformer

    public static <T> Transformer<Class<? extends T>,T> instantiateTransformer()
    Gets a Transformer that expects an input Class object that it will instantiate.
    Type Parameters:
      `T` - the output type
    Returns:
      the transformer
    See Also:
      - `InstantiateTransformer`

  - ### instantiateTransformer

    public static <T> Transformer<Class<? extends T>,T> instantiateTransformer(Class<?>[] paramTypes, 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.
    Type Parameters:
      `T` - the output type
    Parameters:
      `paramTypes` - parameter types for the constructor, can be null
      `args` - the arguments to pass to the constructor, can be null
    Returns:
      the transformer
    Throws:
      `IllegalArgumentException` - if the paramTypes and args don't match
    See Also:
      - `InstantiateTransformer`

  - ### invokerTransformer

    public static <I, O> Transformer<I,O> invokerTransformer(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.

    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `methodName` - the method name to call on the input object, may not be null
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the methodName is null.
    See Also:
      - `InvokerTransformer`

  - ### invokerTransformer

    public static <I, O> Transformer<I,O> invokerTransformer(String methodName, Class<?>[] paramTypes, 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.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `methodName` - the name of the method
      `paramTypes` - the parameter types
      `args` - the arguments
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the method name is null
      `IllegalArgumentException` - if the paramTypes and args don't match
    See Also:
      - `InvokerTransformer`

  - ### mapTransformer

    public static <I, O> Transformer<I,O> mapTransformer(Map<? super I,? extends O> map)
    Creates a Transformer that uses the passed in Map to transform the input object (as a simple lookup).
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `map` - the map to use to transform the objects
    Returns:
      the transformer, or `ConstantTransformer.nullTransformer()` if the `map` is `null`
    See Also:
      - `MapTransformer`

  - ### nopTransformer

    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).
    Type Parameters:
      `T` - the input/output type
    Returns:
      the transformer
    See Also:
      - `NOPTransformer`

  - ### nullTransformer

    public static <I, O> Transformer<I,O> nullTransformer()
    Gets a transformer that always returns null.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Returns:
      the transformer
    See Also:
      - `ConstantTransformer`

  - ### stringValueTransformer

    public static <T> Transformer<T,String> stringValueTransformer()
    Gets a transformer that returns a `String` representation of the input object. This is achieved via the `toString` method, `null` returns 'null'.
    Type Parameters:
      `T` - the input type
    Returns:
      the transformer
    See Also:
      - `StringValueTransformer`

  - ### switchMapTransformer

    public static <I, O> Transformer<I,O> switchMapTransformer(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.

    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `objectsAndTransformers` - a map of objects to transformers
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the map is null
      `NullPointerException` - if any transformer in the map is null
    See Also:
      - `SwitchTransformer`

  - ### switchTransformer

    public static <I, O> Transformer<I,O> switchTransformer(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.

    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `predicatesAndTransformers` - a map of predicates to transformers
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if the map is null
      `NullPointerException` - if any transformer in the map is null
      `ClassCastException` - if the map elements are of the wrong type
    See Also:
      - `SwitchTransformer`

  - ### switchTransformer

    @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)
    Deprecated.
    as of 4.1, use `ifTransformer(Predicate, Transformer, Transformer)`
    Create a new Transformer that calls one of two transformers depending on the specified predicate.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `predicate` - the predicate to switch on
      `trueTransformer` - the transformer called if the predicate is true
      `falseTransformer` - the transformer called if the predicate is false
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if either the predicate or transformer is null
    See Also:
      - `SwitchTransformer`

  - ### switchTransformer

    public 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. 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.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `predicates` - an array of predicates to check
      `transformers` - an array of transformers to call
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if either array is null
      `NullPointerException` - if any element in the arrays is null
      `IllegalArgumentException` - if the arrays have different sizes
    See Also:
      - `SwitchTransformer`

  - ### switchTransformer

    public 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. 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.
    Type Parameters:
      `I` - the input type
      `O` - the output type
    Parameters:
      `predicates` - an array of predicates to check
      `transformers` - an array of transformers to call
      `defaultTransformer` - the default to call if no predicate matches, null means return null
    Returns:
      the transformer
    Throws:
      `NullPointerException` - if either array is null
      `NullPointerException` - if any element in the arrays is null
      `IllegalArgumentException` - if the arrays have different sizes
    See Also:
      - `SwitchTransformer`