org.apache.commons.collections.functors
Class SwitchClosure<T>

java.lang.Object
  extended by org.apache.commons.collections.functors.SwitchClosure<T>
All Implemented Interfaces:
Serializable, Closure<T>

public class SwitchClosure<T>
extends Object
implements Closure<T>, Serializable

Closure implementation calls the closure whose predicate returns true, like a switch statement.

Since:
Commons Collections 3.0
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:34:37 $
Author:
Matt Hall, John Watkinson, Stephen Colebourne
See Also:
Serialized Form

Constructor Summary
SwitchClosure(Predicate<? super T>[] predicates, Closure<? super T>[] closures, Closure<? super T> defaultClosure)
          Constructor that performs no validation.
 
Method Summary
 void execute(T input)
          Executes the closure whose matching predicate returns true
 Closure<? super T>[] getClosures()
          Gets the closures, do not modify the array.
 Closure<? super T> getDefaultClosure()
          Gets the default closure.
static
<T> Closure<T>
getInstance(Map<Predicate<? super T>,Closure<? super T>> predicatesAndClosures)
          Create a new Closure that calls one of the closures depending on the predicates.
static
<T> Closure<T>
getInstance(Predicate<? super T>[] predicates, Closure<? super T>[] closures, Closure<? super T> defaultClosure)
          Factory method that performs validation and copies the parameter arrays.
 Predicate<? super T>[] getPredicates()
          Gets the predicates, do not modify the array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwitchClosure

public SwitchClosure(Predicate<? super T>[] predicates,
                     Closure<? super T>[] closures,
                     Closure<? super T> 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 Detail

getInstance

public static <T> Closure<T> getInstance(Predicate<? super T>[] predicates,
                                         Closure<? super T>[] closures,
                                         Closure<? super T> 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:
IllegalArgumentException - if array is null
IllegalArgumentException - if any element in the array is null

getInstance

public static <T> Closure<T> getInstance(Map<Predicate<? super T>,Closure<? super T>> 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:
IllegalArgumentException - if the map is null
IllegalArgumentException - if any closure in the map is null
ClassCastException - if the map elements are of the wrong type

execute

public void execute(T input)
Executes the closure whose matching predicate returns true

Specified by:
execute in interface Closure<T>
Parameters:
input - the input object

getPredicates

public Predicate<? super T>[] getPredicates()
Gets the predicates, do not modify the array.

Returns:
the predicates
Since:
Commons Collections 3.1

getClosures

public Closure<? super T>[] getClosures()
Gets the closures, do not modify the array.

Returns:
the closures
Since:
Commons Collections 3.1

getDefaultClosure

public Closure<? super T> getDefaultClosure()
Gets the default closure.

Returns:
the default closure
Since:
Commons Collections 3.1


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.