org.apache.commons.collections.functors
Class SwitchClosure
java.lang.Object
org.apache.commons.collections.functors.SwitchClosure
- Closure, Serializable
public class SwitchClosure
extends java.lang.Object
Closure implementation calls the closure whose predicate returns true,
like a switch statement.
$Revision: 1.5 $ $Date: 2004/05/16 11:47:38 $
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
getClosures
public Closure[] getClosures()
Gets the closures, do not modify the array.
getDefaultClosure
public Closure getDefaultClosure()
Gets the default closure.
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
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
getPredicates
public Predicate[] getPredicates()
Gets the predicates, do not modify the array.
Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.