| java.lang.Object org.apache.commons.collections.ClosureUtils
ClosureUtils | public class ClosureUtils (Code) | | ClosureUtils provides reference implementations and utilities
for the Closure functor interface. The supplied closures are:
- Invoker - invokes a method on the input object
- For - repeatedly calls a closure for a fixed number of times
- While - repeatedly calls a closure while a predicate is true
- DoWhile - repeatedly calls a closure while a predicate is true
- Chained - chains two or more closures together
- Switch - calls one closure based on one or more predicates
- SwitchMap - calls one closure looked up from a Map
- Transformer - wraps a Transformer as a Closure
- NOP - does nothing
- Exception - always throws an exception
All the supplied closures are Serializable.
since: Commons Collections 3.0 version: $Revision: 375766 $ $Date: 2006-02-07 23:10:36 +0000 (Tue, 07 Feb 2006) $ author: Stephen Colebourne author: Matt Benson |
Constructor Summary | |
public | ClosureUtils() This class is not normally instantiated. |
Method Summary | |
public static Closure | asClosure(Transformer transformer) Creates a Closure that calls a Transformer each time it is called. | public static Closure | chainedClosure(Closure closure1, Closure closure2) Create a new Closure that calls two Closures, passing the result of
the first into the second. | public static Closure | chainedClosure(Closure[] closures) Create a new Closure that calls each closure in turn, passing the
result into the next closure. | public static Closure | chainedClosure(Collection closures) Create a new Closure that calls each closure in turn, passing the
result into the next closure. | public static Closure | doWhileClosure(Closure closure, Predicate predicate) Creates a Closure that will call the closure once and then repeatedly
until the predicate returns false. | public static Closure | exceptionClosure() Gets a Closure that always throws an exception. | public static Closure | forClosure(int count, Closure closure) Creates a Closure that will call the closure count times. | public static Closure | ifClosure(Predicate predicate, Closure trueClosure) Create a new Closure that calls another closure based on the
result of the specified predicate. | public static Closure | ifClosure(Predicate predicate, Closure trueClosure, Closure falseClosure) Create a new Closure that calls one of two closures depending
on the specified predicate. | public static Closure | invokerClosure(String methodName) Creates a Closure that will invoke a specific method on the closure's
input object by reflection. | public static Closure | invokerClosure(String methodName, Class[] paramTypes, Object[] args) Creates a Closure that will invoke a specific method on the closure's
input object by reflection. | public static Closure | nopClosure() Gets a Closure that will do nothing. | public static Closure | switchClosure(Predicate[] predicates, Closure[] closures) Create a new Closure that calls one of the closures depending
on the predicates.
The closure at array location 0 is called if the predicate at array
location 0 returned true. | public static Closure | switchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure) Create a new Closure that calls one of the closures depending
on the predicates.
The closure at array location 0 is called if the predicate at array
location 0 returned true. | public static Closure | switchClosure(Map predicatesAndClosures) Create a new Closure that calls one of the closures depending
on the predicates. | public static Closure | switchMapClosure(Map objectsAndClosures) Create a new Closure that uses the input object as a key to find the
closure to call. | public static Closure | whileClosure(Predicate predicate, Closure closure) Creates a Closure that will call the closure repeatedly until the
predicate returns false. |
ClosureUtils | public ClosureUtils()(Code) | | This class is not normally instantiated.
|
asClosure | public static Closure asClosure(Transformer transformer)(Code) | | Creates a Closure that calls a Transformer each time it is called.
The transformer will be called using the closure's input object.
The transformer's result will be ignored.
See Also: org.apache.commons.collections.functors.TransformerClosure Parameters: transformer - the transformer to run each time in the closure, null means nop the closure |
forClosure | public static Closure forClosure(int count, Closure closure)(Code) | | Creates a Closure that will call the closure count times.
A null closure or zero count returns the NOPClosure .
See Also: org.apache.commons.collections.functors.ForClosure Parameters: count - the number of times to loop Parameters: closure - the closure to call repeatedly the for closure |
switchClosure | public static Closure switchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure)(Code) | | Create a new Closure that calls one of the closures depending
on the predicates.
The closure at array location 0 is called if the predicate at array
location 0 returned true. Each predicate is evaluated
until one returns true. If no predicates evaluate to true, the default
closure is called.
See Also: org.apache.commons.collections.functors.SwitchClosure Parameters: predicates - an array of predicates to check, not null Parameters: closures - an array of closures to call, not null Parameters: defaultClosure - the default to call if no predicate matches the switch closure throws: IllegalArgumentException - if the either array is null throws: IllegalArgumentException - if any element in the arrays is null throws: IllegalArgumentException - if the arrays are different sizes |
switchClosure | public static Closure switchClosure(Map predicatesAndClosures)(Code) | | 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.
See Also: org.apache.commons.collections.functors.SwitchClosure Parameters: predicatesAndClosures - a map of predicates to closures the switch closure throws: IllegalArgumentException - if the map is null throws: IllegalArgumentException - if the map is empty throws: IllegalArgumentException - if any closure in the map is null throws: ClassCastException - if the map elements are of the wrong type |
|
|