Class SwitchClosure<E>
java.lang.Object
org.apache.commons.collections4.functors.SwitchClosure<E>
- All Implemented Interfaces:
Serializable, Closure<E>
Closure implementation calls the closure whose predicate returns true,
like a switch statement.
- Since:
- 3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe matching closures to callThe default closure to call if no tests matchThe tests to considerprivate static final longSerial version UID -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateSwitchClosure(boolean clone, Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) Hidden constructor for the use by the static factory methods.SwitchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) Constructor that performs no validation. -
Method Summary
Modifier and TypeMethodDescriptionvoidExecutes the closure whose matching predicate returns trueGets the closures.Gets the default closure.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.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerial version UID- See Also:
-
iPredicates
-
iClosures
-
iDefault
-
-
Constructor Details
-
SwitchClosure
private SwitchClosure(boolean clone, Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) Hidden constructor for the use by the static factory methods.- Parameters:
clone- iftruethe input arguments will be clonedpredicates- array of predicates, no nullsclosures- matching array of closures, no nullsdefaultClosure- the closure to use if no match, null means nop
-
SwitchClosure
public SwitchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) Constructor that performs no validation. UseswitchClosureif 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 Details
-
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
chainedclosure - Throws:
NullPointerException- if array is nullNullPointerException- if any element in the array is nullIllegalArgumentException- if the array lengths of predicates and closures do not match
-
switchClosure
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
switchclosure - Throws:
NullPointerException- if the map is nullNullPointerException- if any closure in the map is nullClassCastException- if the map elements are of the wrong type
-
execute
-
getPredicates
-
getClosures
-
getDefaultClosure
-