# Class SwitchClosure<T>

java.lang.Object
org.apache.commons.collections4.functors.SwitchClosure<T>

Type Parameters:
  `T` - the type of the input to the operation.

All Implemented Interfaces:
  `Serializable`, `Consumer<T>`, `Closure<T>`

public class SwitchClosure<T> extends Object implements Closure<T>, Serializable
Closure implementation calls the closure whose predicate returns true, like a switch statement.

Since:
  3.0
See Also:
  - Serialized Form

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SwitchClosure(Predicate<? super T>[] predicates, Closure<? super T>[] closures, Closure<? super T> defaultClosure)`
  Constructor that performs no validation.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `execute(T input)`
  Executes the closure whose matching predicate returns true
  `Closure<? super T>[]`
  `getClosures()`
  Gets the closures.
  `Closure<? super T>`
  `getDefaultClosure()`
  Gets the default closure.
  `Predicate<? super T>[]`
  `getPredicates()`
  Gets the predicates.
  `static <E> Closure<E>`
  `switchClosure(Map<Predicate<E>,Closure<E>> predicatesAndClosures)`
  Create a new Closure that calls one of the closures depending on the predicates.
  `static <E> Closure<E>`
  `switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure)`
  Factory method that performs validation and copies the parameter arrays.

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

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

  ### Methods inherited from interface org.apache.commons.collections4.Closure

  `accept`

  ### Methods inherited from interface java.util.function.Consumer

  `andThen`

- ## Constructor Details

  - ### SwitchClosure

    public SwitchClosure(Predicate<? super T>[] predicates, Closure<? super T>[] closures, Closure<? super T> defaultClosure)
    Constructor that performs no validation. Use `switchClosure` if you want that.
    Parameters:
      `predicates` - array of predicates, cloned, no nulls
      `closures` - matching array of closures, cloned, no nulls
      `defaultClosure` - the closure to use if no match, null means nop

- ## Method Details

  - ### switchClosure

    public static <E> Closure<E> switchClosure(Map<Predicate<E>,Closure<E>> predicatesAndClosures)
    Create a new Closure that calls one of the closures depending on the predicates.

    The Map consists of Predicate keys and Closure values. A closure is called if its matching predicate returns true. Each predicate is evaluated until one returns true. If no predicates evaluate to true, the default closure is called. The default closure 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:
      `E` - the type that the closure acts on
    Parameters:
      `predicatesAndClosures` - a map of predicates to closures
    Returns:
      the `switch` closure
    Throws:
      `NullPointerException` - if the map is null
      `NullPointerException` - if any closure in the map is null
      `ClassCastException` - if the map elements are of the wrong type

  - ### switchClosure

    public static <E> Closure<E> switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure)
    Factory method that performs validation and copies the parameter arrays.
    Type Parameters:
      `E` - the type that the closure acts on
    Parameters:
      `predicates` - array of predicates, cloned, no nulls
      `closures` - matching array of closures, cloned, no nulls
      `defaultClosure` - the closure to use if no match, null means nop
    Returns:
      the `chained` closure
    Throws:
      `NullPointerException` - if array is null
      `NullPointerException` - if any element in the array is null
      `IllegalArgumentException` - if the array lengths of predicates and closures do not match

  - ### execute

    public void execute(T input)
    Executes the closure whose matching predicate returns true
    Specified by:
      `execute` in interface `Closure<T>`
    Parameters:
      `input` - the input object

  - ### getClosures

    public Closure<? super T>[] getClosures()
    Gets the closures.
    Returns:
      a copy of the closures
    Since:
      3.1

  - ### getDefaultClosure

    public Closure<? super T> getDefaultClosure()
    Gets the default closure.
    Returns:
      the default closure
    Since:
      3.1

  - ### getPredicates

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