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.
asClosure
public static Closure asClosure(Transformer transformer)
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.
transformer
- the transformer to run each time in the closure, null means nop
chainedClosure
public static Closure chainedClosure(Collection closures)
Create a new Closure that calls each closure in turn, passing the
result into the next closure. The ordering is that of the iterator()
method on the collection.
closures
- a collection of closures to chain
chainedClosure
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.
closure1
- the first closureclosure2
- the second closure
chainedClosure
public static Closure chainedClosure(Closure[] closures)
Create a new Closure that calls each closure in turn, passing the
result into the next closure.
closures
- an array of closures to chain
doWhileClosure
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.
closure
- the closure to call repeatedly, not nullpredicate
- the predicate to use as an end of loop test, not null
exceptionClosure
public static Closure exceptionClosure()
Gets a Closure that always throws an exception.
This could be useful during testing as a placeholder.
forClosure
public static Closure forClosure(int count,
Closure closure)
Creates a Closure that will call the closure
count
times.
A null closure or zero count returns the
NOPClosure
.
count
- the number of times to loopclosure
- the closure to call repeatedly
ifClosure
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.
predicate
- the predicate to switch ontrueClosure
- the closure called if the predicate is truefalseClosure
- the closure called if the predicate is false
invokerClosure
public static Closure invokerClosure(String methodName)
Creates a Closure that will invoke a specific method on the closure's
input object by reflection.
methodName
- the name of the method
invokerClosure
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.
methodName
- the name of the methodparamTypes
- the parameter typesargs
- the arguments
nopClosure
public static Closure nopClosure()
Gets a Closure that will do nothing.
This could be useful during testing as a placeholder.
switchClosure
public static Closure switchClosure(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
switchClosure
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. Each predicate is evaluated
until one returns true.
predicates
- an array of predicates to check, not nullclosures
- an array of closures to call, not null
switchClosure
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. Each predicate is evaluated
until one returns true. If no predicates evaluate to true, the default
closure is called.
predicates
- an array of predicates to check, not nullclosures
- an array of closures to call, not nulldefaultClosure
- the default to call if no predicate matches
switchMapClosure
public static Closure switchMapClosure(Map objectsAndClosures)
Create a new Closure that uses the input object as a key to find the
closure to call.
The Map consists of object keys and Closure values. A closure
is called if the input object equals the key. If there is no match, the
default closure is called. The default closure is set in the map
using a null key.
objectsAndClosures
- a map of objects to closures
whileClosure
public static Closure whileClosure(Predicate predicate,
Closure closure)
Creates a Closure that will call the closure repeatedly until the
predicate returns false.
predicate
- the predicate to use as an end of loop test, not nullclosure
- the closure to call repeatedly, not null