public class ClosureUtils extends Object
ClosureUtils provides reference implementations and utilities for the Closure functor interface. The supplied closures are:
Since v4.1 only closures which are considered to be unsafe are Serializable. Closures considered to be unsafe for serialization are:
| Modifier and Type | Method and Description |
|---|---|
static <E> Closure |
asClosure(Transformer
Creates a Closure that calls a Transformer each time it is called.
|
static <E> Closure |
chainedClosure(Closure
Create a new Closure that calls each closure in turn, passing the result into the next closure.
|
static <E> Closure |
chainedClosure(Collection
Create a new Closure that calls each closure in turn, passing the result into the next closure.
|
static <E> Closure |
doWhileClosure(Closure
Creates a Closure that will call the closure once and then repeatedly until the predicate returns false.
|
static <E> Closure |
exceptionClosure()
Gets a Closure that always throws an exception.
|
static <E> Closure |
forClosure(int count, Closure
Creates a Closure that will call the closure
count times.
|
static <E> Closure |
ifClosure(Predicate
Create a new Closure that calls another closure based on the result of the specified predicate.
|
static <E> Closure |
ifClosure(Predicate
Create a new Closure that calls one of two closures depending on the specified predicate.
|
static <E> Closure |
invokerClosure(String
Creates a Closure that will invoke a specific method on the closure's input object by reflection.
|
static <E> Closure |
invokerClosure(String
Creates a Closure that will invoke a specific method on the closure's input object by reflection.
|
static <E> Closure |
nopClosure()
Gets a Closure that will do nothing.
|
static <E> Closure |
switchClosure(Map
Create a new Closure that calls one of the closures depending on the predicates.
|
static <E> Closure |
switchClosure(Predicate
Create a new Closure that calls one of the closures depending on the predicates.
|
static <E> Closure |
switchClosure(Predicate
Create a new Closure that calls one of the closures depending on the predicates.
|
static <E> Closure |
switchMapClosure(Map
Create a new Closure that uses the input object as a key to find the closure to call.
|
static <E> Closure |
whileClosure(Predicate
Creates a Closure that will call the closure repeatedly until the predicate returns false.
|
public static <E> Closure<E> exceptionClosure()
E - the type that the closure acts on
ExceptionClosure
public static <E> Closure<E> nopClosure()
E - the type that the closure acts on
NOPClosure
public static <E> Closure<E> asClosure(Transformer <? super E ,?> transformer)
E - the type that the closure acts on
transformer - the transformer to run each time in the closure, null means nop
TransformerClosure
public static <E> Closure<E> forClosure(int count, Closure <? super E> closure)
count times.
A null closure or zero count returns the NOPClosure.
E - the type that the closure acts on
count - the number of times to loop
closure - the closure to call repeatedly
for closure
ForClosure
public static <E> Closure<E> whileClosure(Predicate <? super E> predicate, Closure <? super E> closure)
E - the type that the closure acts on
predicate - the predicate to use as an end of loop test, not null
closure - the closure to call repeatedly, not null
while closure
NullPointerException - if either argument is null
WhileClosure
public static <E> Closure<E> doWhileClosure(Closure <? super E> closure, Predicate <? super E> predicate)
E - the type that the closure acts on
closure - the closure to call repeatedly, not null
predicate - the predicate to use as an end of loop test, not null
do-while closure
NullPointerException - if either argument is null
WhileClosure
public static <E> Closure<E> invokerClosure(String methodName)
E - the type that the closure acts on
methodName - the name of the method
invoker closure
NullPointerException - if the method name is null
InvokerTransformer,
TransformerClosure
public static <E> Closure<E> invokerClosure(String methodName, Class <?>[] paramTypes, Object [] args)
E - the type that the closure acts on
methodName - the name of the method
paramTypes - the parameter types
args - the arguments
invoker closure
NullPointerException - if the method name is null
IllegalArgumentException - if the paramTypes and args don't match
InvokerTransformer,
TransformerClosure
public static <E> Closure<E> chainedClosure(Closure <? super E>... closures)
E - the type that the closure acts on
closures - an array of closures to chain
chained closure
NullPointerException - if the closures array is null
NullPointerException - if any closure in the array is null
ChainedClosure
public static <E> Closure<E> chainedClosure(Collection <? extends Closure <? super E>> closures)
E - the type that the closure acts on
closures - a collection of closures to chain
chained closure
NullPointerException - if the closures collection is null
NullPointerException - if any closure in the collection is null
IllegalArgumentException - if the closures collection is empty
ChainedClosure
public static <E> Closure<E> ifClosure(Predicate <? super E> predicate, Closure <? super E> trueClosure)
E - the type that the closure acts on
predicate - the validating predicate
trueClosure - the closure called if the predicate is true
if closure
NullPointerException - if the predicate or closure is null
IfClosure
public static <E> Closure<E> ifClosure(Predicate <? super E> predicate, Closure <? super E> trueClosure, Closure <? super E> falseClosure)
E - the type that the closure acts on
predicate - the predicate to switch on
trueClosure - the closure called if the predicate is true
falseClosure - the closure called if the predicate is false
switch closure
NullPointerException - if the predicate or either closure is null
IfClosure
public static <E> Closure<E> switchClosure(Predicate <? super E>[] predicates, Closure <? super E>[] closures)
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.
E - the type that the closure acts on
predicates - an array of predicates to check, not null
closures - an array of closures to call, not null
switch closure
NullPointerException - if the either array is null
NullPointerException - if any element in the arrays is null
IllegalArgumentException - if the arrays have different sizes
SwitchClosure
public static <E> Closure<E> switchClosure(Predicate <? super E>[] predicates, Closure <? super E>[] closures, Closure <? super E> defaultClosure)
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.
E - the type that the closure acts on
predicates - an array of predicates to check, not null
closures - an array of closures to call, not null
defaultClosure - the default to call if no predicate matches
switch closure
NullPointerException - if the either array is null
NullPointerException - if any element in the arrays is null
IllegalArgumentException - if the arrays are different sizes
SwitchClosure
public static <E> Closure<E> switchClosure(Map <Predicate <E> ,Closure <E>> predicatesAndClosures)
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.
E - the type that the closure acts on
predicatesAndClosures - a map of predicates to closures
switch closure
NullPointerException - if the map is null
NullPointerException - if any closure in the map is null
IllegalArgumentException - if the map is empty
ClassCastException - if the map elements are of the wrong type
SwitchClosure
public static <E> Closure<E> switchMapClosure(Map <? extends E ,Closure <E>> objectsAndClosures)
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.
E - the type that the closure acts on
objectsAndClosures - a map of objects to closures
NullPointerException - if the map is null
NullPointerException - if any closure in the map is null
IllegalArgumentException - if the map is empty
SwitchClosure