Class TransformerUtils


  • @Deprecated(since="2021-04-30")
    public class TransformerUtils
    extends java.lang.Object
    Deprecated.
    Commons Collections 3 is in maintenance mode. Commons Collections 4 should be used instead.
    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
    • 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 java.lang.String representation of the input object
    All the supplied transformers are Serializable.
    Since:
    Commons Collections 3.0
    • Constructor Detail

      • TransformerUtils

        public TransformerUtils()
        Deprecated.
        This class is not normally instantiated.
    • Method Detail

      • exceptionTransformer

        public static Transformer exceptionTransformer()
        Deprecated.
        Gets a transformer that always throws an exception. This could be useful during testing as a placeholder.
        Returns:
        the transformer
        See Also:
        ExceptionTransformer
      • nullTransformer

        public static Transformer nullTransformer()
        Deprecated.
        Gets a transformer that always returns null.
        Returns:
        the transformer
        See Also:
        ConstantTransformer
      • nopTransformer

        public static Transformer nopTransformer()
        Deprecated.
        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).
        Returns:
        the transformer
        See Also:
        NOPTransformer
      • cloneTransformer

        public static Transformer cloneTransformer()
        Deprecated.
        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
          Returns:
          the transformer
          See Also:
          CloneTransformer
        • constantTransformer

          public static Transformer constantTransformer​(java.lang.Object constantToReturn)
          Deprecated.
          Creates a Transformer that will return the same object each time the transformer is used.
          Parameters:
          constantToReturn - the constant object to return each time in the transformer
          Returns:
          the transformer.
          See Also:
          ConstantTransformer
        • asTransformer

          public static Transformer asTransformer​(Closure closure)
          Deprecated.
          Creates a Transformer that calls a Closure each time the transformer is used. The transformer returns the input object.
          Parameters:
          closure - the closure to run each time in the transformer, not null
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the closure is null
          See Also:
          ClosureTransformer
        • asTransformer

          public static Transformer asTransformer​(Predicate predicate)
          Deprecated.
          Creates a Transformer that calls a Predicate each time the transformer is used. The transformer will return either Boolean.TRUE or Boolean.FALSE.
          Parameters:
          predicate - the predicate to run each time in the transformer, not null
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the predicate is null
          See Also:
          PredicateTransformer
        • asTransformer

          public static Transformer asTransformer​(Factory factory)
          Deprecated.
          Creates a Transformer that calls a Factory each time the transformer is used. The transformer will return the value returned by the factory.
          Parameters:
          factory - the factory to run each time in the transformer, not null
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the factory is null
          See Also:
          FactoryTransformer
        • chainedTransformer

          public static Transformer chainedTransformer​(Transformer transformer1,
                                                       Transformer transformer2)
          Deprecated.
          Create a new Transformer that calls two transformers, passing the result of the first into the second.
          Parameters:
          transformer1 - the first transformer
          transformer2 - the second transformer
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if either transformer is null
          See Also:
          ChainedTransformer
        • chainedTransformer

          public static Transformer chainedTransformer​(Transformer[] transformers)
          Deprecated.
          Create a new Transformer that calls each transformer in turn, passing the result into the next transformer.
          Parameters:
          transformers - an array of transformers to chain
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the transformers array is null
          java.lang.IllegalArgumentException - if any transformer in the array is null
          See Also:
          ChainedTransformer
        • chainedTransformer

          public static Transformer chainedTransformer​(java.util.Collection transformers)
          Deprecated.
          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.
          Parameters:
          transformers - a collection of transformers to chain
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the transformers collection is null
          java.lang.IllegalArgumentException - if any transformer in the collection is null
          See Also:
          ChainedTransformer
        • switchTransformer

          public static Transformer switchTransformer​(Predicate predicate,
                                                      Transformer trueTransformer,
                                                      Transformer falseTransformer)
          Deprecated.
          Create a new Transformer that calls one of two transformers depending on the specified predicate.
          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:
          java.lang.IllegalArgumentException - if the predicate is null
          java.lang.IllegalArgumentException - if either transformer is null
          See Also:
          SwitchTransformer
        • switchTransformer

          public static Transformer switchTransformer​(Predicate[] predicates,
                                                      Transformer[] transformers)
          Deprecated.
          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.
          Parameters:
          predicates - an array of predicates to check
          transformers - an array of transformers to call
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the either array is null
          java.lang.IllegalArgumentException - if the either array has 0 elements
          java.lang.IllegalArgumentException - if any element in the arrays is null
          java.lang.IllegalArgumentException - if the arrays are different sizes
          See Also:
          SwitchTransformer
        • switchTransformer

          public static Transformer switchTransformer​(Predicate[] predicates,
                                                      Transformer[] transformers,
                                                      Transformer defaultTransformer)
          Deprecated.
          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.
          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:
          java.lang.IllegalArgumentException - if the either array is null
          java.lang.IllegalArgumentException - if the either array has 0 elements
          java.lang.IllegalArgumentException - if any element in the arrays is null
          java.lang.IllegalArgumentException - if the arrays are different sizes
          See Also:
          SwitchTransformer
        • switchTransformer

          public static Transformer switchTransformer​(java.util.Map predicatesAndTransformers)
          Deprecated.
          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.

          Parameters:
          predicatesAndTransformers - a map of predicates to transformers
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the map is null
          java.lang.IllegalArgumentException - if the map is empty
          java.lang.IllegalArgumentException - if any transformer in the map is null
          java.lang.ClassCastException - if the map elements are of the wrong type
          See Also:
          SwitchTransformer
        • switchMapTransformer

          public static Transformer switchMapTransformer​(java.util.Map objectsAndTransformers)
          Deprecated.
          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.

          Parameters:
          objectsAndTransformers - a map of objects to transformers
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the map is null
          java.lang.IllegalArgumentException - if the map is empty
          java.lang.IllegalArgumentException - if any transformer in the map is null
          See Also:
          SwitchTransformer
        • instantiateTransformer

          public static Transformer instantiateTransformer()
          Deprecated.
          Gets a Transformer that expects an input Class object that it will instantiate.
          Returns:
          the transformer
          See Also:
          InstantiateTransformer
        • instantiateTransformer

          public static Transformer instantiateTransformer​(java.lang.Class[] paramTypes,
                                                           java.lang.Object[] args)
          Deprecated.
          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.
          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:
          java.lang.IllegalArgumentException - if the paramTypes and args don't match
          See Also:
          InstantiateTransformer
        • mapTransformer

          public static Transformer mapTransformer​(java.util.Map map)
          Deprecated.
          Creates a Transformer that uses the passed in Map to transform the input object (as a simple lookup).
          Parameters:
          map - the map to use to transform the objects
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the map is null
          See Also:
          MapTransformer
        • invokerTransformer

          public static Transformer invokerTransformer​(java.lang.String methodName)
          Deprecated.
          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/code> method on the input object to determine the transformer result.

          Parameters:
          methodName - the method name to call on the input object, may not be null
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the methodName is null.
          See Also:
          InvokerTransformer
        • invokerTransformer

          public static Transformer invokerTransformer​(java.lang.String methodName,
                                                       java.lang.Class[] paramTypes,
                                                       java.lang.Object[] args)
          Deprecated.
          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.
          Parameters:
          methodName - the name of the method
          paramTypes - the parameter types
          args - the arguments
          Returns:
          the transformer
          Throws:
          java.lang.IllegalArgumentException - if the method name is null
          java.lang.IllegalArgumentException - if the paramTypes and args don't match
          See Also:
          InvokerTransformer
        • stringValueTransformer

          public static Transformer stringValueTransformer()
          Deprecated.
          Gets a transformer that returns a java.lang.String representation of the input object. This is achieved via the toString method, null returns 'null'.
          Returns:
          the transformer
          See Also:
          StringValueTransformer