public class SwitchClosure<E> extends Object implements Closure<E>, Serializable
Constructor and Description |
---|
SwitchClosure(Predicate<? super E>[] predicates,
Closure<? super E>[] closures,
Closure<? super E> defaultClosure)
Constructor that performs no validation.
|
Modifier and Type | Method and Description |
---|---|
void |
execute(E input)
Executes the closure whose matching predicate returns true
|
Closure<? 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(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.
|
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 nullsclosures
- matching array of closures, cloned, no nullsdefaultClosure
- the closure to use if no match, null means noppublic static <E> Closure<E> switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure)
E
- the type that the closure acts onpredicates
- array of predicates, cloned, no nullsclosures
- matching array of closures, cloned, no nullsdefaultClosure
- the closure to use if no match, null means nopchained
closureNullPointerException
- if array is nullNullPointerException
- if any element in the array is nullIllegalArgumentException
- if the array lengths of predicates and closures do not matchpublic 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 onpredicatesAndClosures
- a map of predicates to closuresswitch
closureNullPointerException
- if the map is nullNullPointerException
- if any closure in the map is nullClassCastException
- if the map elements are of the wrong typepublic Predicate<? super E>[] getPredicates()
public Closure<? super E>[] getClosures()
public Closure<? super E> getDefaultClosure()
Copyright © 2001–2019 The Apache Software Foundation. All rights reserved.