public class SwitchClosure<E> extends Objectimplements Closure <E>, Serializable
| Constructor and Description |
|---|
SwitchClosure(Predicate
Constructor that performs no validation.
|
| Modifier and Type | Method and Description |
|---|---|
void |
execute(E input)
Executes the closure whose matching predicate returns true
|
Closure |
getClosures()
Gets the closures.
|
Closure |
getDefaultClosure()
Gets the default closure.
|
Predicate |
getPredicates()
Gets the predicates.
|
static <E> Closure |
switchClosure(Map
Create a new Closure that calls one of the closures depending on the predicates.
|
static <E> Closure |
switchClosure(Predicate
Factory method that performs validation and copies the parameter arrays.
|
public SwitchClosure(Predicate<? super E>[] predicates, Closure <? super E>[] closures, Closure <? super E> defaultClosure)
switchClosure if you want that.
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
public static <E> Closure<E> switchClosure(Predicate <? super E>[] predicates, Closure <? super E>[] closures, Closure <? super E> defaultClosure)
E - the type that the closure acts on
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
chained closure
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
public static <E> Closure<E> switchClosure(Map <Predicate <E> ,Closure <E>> predicatesAndClosures)
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.
E - the type that the closure acts on
predicatesAndClosures - a map of predicates to closures
switch closure
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
public void execute(E input)
public Predicate<? super E>[] getPredicates()
public Closure<? super E>[] getClosures()