Class SwitchClosure<E>
- java.lang.Object
-
- org.apache.commons.collections4.functors.SwitchClosure<E>
-
- All Implemented Interfaces:
java.io.Serializable
,Closure<E>
public class SwitchClosure<E> extends java.lang.Object implements Closure<E>, java.io.Serializable
Closure implementation calls the closure whose predicate returns true, like a switch statement.- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(E input)
Executes the closure whose matching predicate returns trueClosure<? super E>[]
getClosures()
Gets the closures.Closure<? super E>
getDefaultClosure()
Gets the default closure.Predicate<? super E>[]
getPredicates()
Gets the predicates.static <E> Closure<E>
switchClosure(java.util.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.
-
-
-
Constructor Detail
-
SwitchClosure
public SwitchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure)
Constructor that performs no validation. UseswitchClosure
if you want that.- Parameters:
predicates
- array of predicates, cloned, no nullsclosures
- matching array of closures, cloned, no nullsdefaultClosure
- the closure to use if no match, null means nop
-
-
Method Detail
-
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 nullsclosures
- matching array of closures, cloned, no nullsdefaultClosure
- the closure to use if no match, null means nop- Returns:
- the
chained
closure - Throws:
java.lang.NullPointerException
- if array is nulljava.lang.NullPointerException
- if any element in the array is nulljava.lang.IllegalArgumentException
- if the array lengths of predicates and closures do not match
-
switchClosure
public static <E> Closure<E> switchClosure(java.util.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:
java.lang.NullPointerException
- if the map is nulljava.lang.NullPointerException
- if any closure in the map is nulljava.lang.ClassCastException
- if the map elements are of the wrong type
-
execute
public void execute(E input)
Executes the closure whose matching predicate returns true
-
getPredicates
public Predicate<? super E>[] getPredicates()
Gets the predicates.- Returns:
- a copy of the predicates
- Since:
- 3.1
-
getClosures
public Closure<? super E>[] getClosures()
Gets the closures.- Returns:
- a copy of the closures
- Since:
- 3.1
-
-