org.apache.commons.collections.functors
Class SwitchClosure
- Closure, Serializable
public class SwitchClosure
Closure implementation calls the closure whose predicate returns true,
like a switch statement.
Version:
- Stephen Colebourne
- Commons Collections 3.0
SwitchClosure
public SwitchClosure(Predicate[] predicates,
Closure[] closures,
Closure defaultClosure)
Constructor that performs no validation.
Use getInstance
if you want that.
predicates
- array of predicates, not cloned, no nullsclosures
- matching array of closures, not cloned, no nullsdefaultClosure
- the closure to use if no match, null means nop
execute
public void execute(Object input)
Executes the closure whose matching predicate returns true
- execute in interface Closure
input
- the input object
getClosures
public Closure[] getClosures()
Gets the closures, do not modify the array.
- the closures
- Commons Collections 3.1
getDefaultClosure
public Closure getDefaultClosure()
Gets the default closure.
- the default closure
- Commons Collections 3.1
getInstance
public static Closure getInstance(Map 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.
predicatesAndClosures
- a map of predicates to closures
- the
switch
closure
getInstance
public static Closure getInstance(Predicate[] predicates,
Closure[] closures,
Closure defaultClosure)
Factory method that performs validation and copies the parameter arrays.
predicates
- array of predicates, cloned, no nullsclosures
- matching array of closures, cloned, no nullsdefaultClosure
- the closure to use if no match, null means nop
- the
chained
closure
getPredicates
public Predicate[] getPredicates()
Gets the predicates, do not modify the array.
- the predicates
- Commons Collections 3.1
Copyright © 2001-2005 Apache Software Foundation. All Rights Reserved.