Class SwitchTransformer<I,O>

java.lang.Object
org.apache.commons.collections4.functors.SwitchTransformer<I,O>
All Implemented Interfaces:
Serializable, Transformer<I,O>

public class SwitchTransformer<I,O> extends Object implements Transformer<I,O>, Serializable
Transformer implementation calls the transformer whose predicate returns true, like a switch statement.
Since:
3.0
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serial version UID
      See Also:
    • iPredicates

      private final Predicate<? super I>[] iPredicates
      The tests to consider
    • iTransformers

      private final Transformer<? super I,? extends O>[] iTransformers
      The matching transformers to call
    • iDefault

      private final Transformer<? super I,? extends O> iDefault
      The default transformer to call if no tests match
  • Constructor Details

    • SwitchTransformer

      private SwitchTransformer(boolean clone, Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers, Transformer<? super I,? extends O> defaultTransformer)
      Hidden constructor for the use by the static factory methods.
      Parameters:
      clone - if true the input arguments will be cloned
      predicates - array of predicates, no nulls
      transformers - matching array of transformers, no nulls
      defaultTransformer - the transformer to use if no match, null means return null
    • SwitchTransformer

      public SwitchTransformer(Predicate<? super I>[] predicates, Transformer<? super I,? extends O>[] transformers, Transformer<? super I,? extends O> defaultTransformer)
      Constructor that performs no validation. Use switchTransformer if you want that.
      Parameters:
      predicates - array of predicates, cloned, no nulls
      transformers - matching array of transformers, cloned, no nulls
      defaultTransformer - the transformer to use if no match, null means return null
  • Method Details

    • 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)
      Factory method that performs validation and copies the parameter arrays.
      Type Parameters:
      I - the input type
      O - the output type
      Parameters:
      predicates - array of predicates, cloned, no nulls
      transformers - matching array of transformers, cloned, no nulls
      defaultTransformer - the transformer to use if no match, null means return null
      Returns:
      the chained transformer
      Throws:
      NullPointerException - if array is null
      NullPointerException - if any element in the array is null
    • switchTransformer

      public static <I, O> Transformer<I,O> switchTransformer(Map<? extends Predicate<? super I>,? extends Transformer<? super I,? extends O>> map)
      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. 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:
      map - a map of predicates to transformers
      Returns:
      the switch 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
    • transform

      public O transform(I input)
      Transforms the input to result by calling the transformer whose matching predicate returns true.
      Specified by:
      transform in interface Transformer<I,O>
      Parameters:
      input - the input object to transform
      Returns:
      the transformed result
    • getPredicates

      public Predicate<? super I>[] getPredicates()
      Gets the predicates.
      Returns:
      a copy of the predicates
      Since:
      3.1
    • getTransformers

      public Transformer<? super I,? extends O>[] getTransformers()
      Gets the transformers.
      Returns:
      a copy of the transformers
      Since:
      3.1
    • getDefaultTransformer

      public Transformer<? super I,? extends O> getDefaultTransformer()
      Gets the default transformer.
      Returns:
      the default transformer
      Since:
      3.1