⇒ Index (Frames) |  ⇒ Index (No Frames) |  ⇒ Package |  ⇒ Package Tree |  ⇒ Full Tree 
org.apache.commons.collections.functors

Class SwitchClosure

java.lang.Object
|
+--org.apache.commons.collections.functors.SwitchClosure

All Implemented Interfaces:
Closure, Serializable


public class SwitchClosure
extends java.lang.Object
implements Closure, Serializable

Closure implementation calls the closure whose predicate returns true, like a switch statement.
Since:
Commons Collections 3.0
Author:
Stephen Colebourne

Constructor Summary

SwitchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure)

Constructor that performs no validation.

Method Summary

void

execute(Object input)

Execute the closure whose predicate returns true

static Closure

getInstance(Predicate[] predicates, Closure[] closures, Closure defaultClosure)

Factory method that performs validation and copies the parameter arrays.

static Closure

getInstance(Map predicatesAndClosures)

Create a new Closure that calls one of the closures depending on the predicates.

Constructor Details

SwitchClosure

public SwitchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure)

Constructor that performs no validation. Use getInstance if you want that.

Parameters:
predicates - array of predicates, not cloned, no nulls
closures - matching array of closures, not cloned, no nulls
defaultClosure - the closure to use if no match, null means nop

Method Details

execute

public void execute(Object input)

Execute the closure whose predicate returns true

Parameters:
input

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.

Parameters:
predicatesAndClosures - a map of predicates to closures
Returns:
the switch closure
Throws:
- if the map is null
- if any closure in the map is null
- if the map elements are of the wrong type

getInstance

public static Closure getInstance(Predicate[] predicates, Closure[] closures, Closure defaultClosure)

Factory method that performs validation and copies the parameter arrays.

Parameters:
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
Returns:
the chained closure
Throws:
- if array is null
- if any element in the array is null