net.sf.collections15.functors.closure
Class SwitchMapClosure<E>

java.lang.Object
  extended by net.sf.collections15.functors.closure.SwitchMapClosure<E>
All Implemented Interfaces:
java.io.Serializable, Closure<E>

public class SwitchMapClosure<E>
extends java.lang.Object
implements Closure<E>, java.io.Serializable

Closure implementation that executes one of a number of Closures in a map depending on which key in the map matches the input object.

Since:
Collections15 1.0
Author:
Chris Lambrou
See Also:
Serialized Form

Constructor Summary
protected SwitchMapClosure(java.util.Map<E,Closure<? super E>> objectsAndClosures, Closure<? super E> defaultClosure)
          Creates a new instance that executes one of a number of Closures in a map depending on which key in the map matches the input object.
 
Method Summary
 void execute(E input)
          Executes one of the Closures on the input object, using the input object to look up which Closure to execute.
 Closure<? super E> getDefaultClosure()
          Returns the default Closure.
static
<T> SwitchMapClosure<T>
getInstance(java.util.Map<T,Closure<? super T>> objectsAndClosures)
          Creates a new instance of a SwitchMapClosure that executes one of a number of Closures in a map depending on which key in the map matches the input object, throwing an exception if the input object doesn't match any of the keys.
static
<T> SwitchMapClosure<T>
getInstance(java.util.Map<T,Closure<? super T>> objectsAndClosures, Closure<? super T> defaultClosure)
          Creates a new instance of a SwitchMapClosure that executes one of a number of Closures in a map depending on which key in the map matches the input object, or the default Closure if the input object doesn't match any of the keys.
 java.util.Map<E,Closure<? super E>> getMappings()
          Returns the mappings defined by this SwitchMapClosure.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwitchMapClosure

protected SwitchMapClosure(java.util.Map<E,Closure<? super E>> objectsAndClosures,
                           Closure<? super E> defaultClosure)
Creates a new instance that executes one of a number of Closures in a map depending on which key in the map matches the input object.

The map consists of Object keys and Closure values. If the input object is mapped to a Closure, that Closure is executed on the input object. In addition, a default Closure can be specified that will be executed on an input object if it doesn't match any of the keys in the map.

Parameters:
objectsAndClosures - A map of Objectss to Closures. If an input object is mapped to a Closure in the map, that Closure is executed.

This map is defensively copied into either a TreeMap (if this map is sorted) or a HashMap (if it's not).

defaultClosure - If an input object doesn't map to a Closure, then this Closure, if non-null, will be executed on the input object. If this default is null then the execute(E) method will throw an IllegalArgumentException.
Throws:
java.lang.IllegalArgumentException - If the map is null.
java.lang.IllegalArgumentException - If any Closure in the map is null.
Method Detail

getInstance

public static <T> SwitchMapClosure<T> getInstance(java.util.Map<T,Closure<? super T>> objectsAndClosures)
Creates a new instance of a SwitchMapClosure that executes one of a number of Closures in a map depending on which key in the map matches the input object, throwing an exception if the input object doesn't match any of the keys.

The map consists of Object keys and Closure values. If the input object is mapped to a Closure, that Closure is executed on the input object. In addition, a default Closure can be specified that will be executed on an input object if it doesn't match any of the keys in the map.

Parameters:
objectsAndClosures - A map of Objectss to Closures. If an input object is mapped to a Closure in the map, that Closure is executed.

This map is defensively copied into either a TreeMap (if this map is sorted) or a HashMap (if it's not).

Returns:
A new instance of a SwitchMapClosure that executes one of a number of Closures in a map depending on which key in the map matches the input object, throwing an exception if the input object doesn't match any of the keys.
Throws:
java.lang.IllegalArgumentException - If the map is null.
java.lang.IllegalArgumentException - If any Closure in the map is null.

getInstance

public static <T> SwitchMapClosure<T> getInstance(java.util.Map<T,Closure<? super T>> objectsAndClosures,
                                                  Closure<? super T> defaultClosure)
Creates a new instance of a SwitchMapClosure that executes one of a number of Closures in a map depending on which key in the map matches the input object, or the default Closure if the input object doesn't match any of the keys.

The map consists of Object keys and Closure values. If the input object is mapped to a Closure, that Closure is executed on the input object. In addition, a default Closure can be specified that will be executed on an input object if it doesn't match any of the keys in the map.

Parameters:
objectsAndClosures - A map of Objectss to Closures. If an input object is mapped to a Closure in the map, that Closure is executed.

This map is defensively copied into either a TreeMap (if this map is sorted) or a HashMap (if it's not).

defaultClosure - If an input object doesn't map to a Closure, then this Closure, if non-null, will be executed on the input object. If this default is null then the execute(E) method will throw an IllegalArgumentException.
Returns:
A new instance of a SwitchMapClosure that executes one of a number of Closures in a map depending on which key in the map matches the input object, or the default Closure if the input object doesn't match any of the keys.
Throws:
java.lang.IllegalArgumentException - If the map is null.
java.lang.IllegalArgumentException - If any Closure in the map is null.

execute

public void execute(E input)
             throws FunctorException
Executes one of the Closures on the input object, using the input object to look up which Closure to execute.

Specified by:
execute in interface Closure<E>
Parameters:
input - The input to execute the Closure on.
Throws:
FunctorException - Thrown if the input object doesn't match any of the keys and no default Closure is defined.

getMappings

public java.util.Map<E,Closure<? super E>> getMappings()
Returns the mappings defined by this SwitchMapClosure.

Returns:
A read-only view of the mappings defined by this SwitchMapClosure.
Since:
Collections15 1.0
See Also:
getDefaultClosure()

getDefaultClosure

public Closure<? super E> getDefaultClosure()
Returns the default Closure.

Returns:
The default Closure applied to an input object if it doesn't satisfy any of the cases defined for this SwitchClosure. May be null if no default is defined.
Since:
Collections15 1.0
See Also:
getMappings()


Copyright © 2001-2005 SourceForge.net. All Rights Reserved.