Class ClosureUtils
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
- Chained - chains two or more closures together
- If - calls one closure or another based on a predicate
- 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
Since v4.1 only closures which are considered to be safe are Serializable. Closures considered to be unsafe for serialization are:
- Invoker
- For
- While
- Since:
- 3.0
- 
Method SummaryModifier and TypeMethodDescriptionstatic <E> Closure<E> asClosure(Transformer<? super E, ?> transformer) Creates a Closure that calls a Transformer each time it is called.static <E> Closure<E> chainedClosure(Collection<? extends Closure<? super E>> closures) Create a new Closure that calls each closure in turn, passing the result into the next closure.static <E> Closure<E> chainedClosure(Closure<? super E>... closures) Create a new Closure that calls each closure in turn, passing the result into the next closure.static <E> Closure<E> doWhileClosure(Closure<? super E> closure, Predicate<? super E> predicate) Creates a Closure that will call the closure once and then repeatedly until the predicate returns false.static <E> Closure<E> Gets a Closure that always throws an exception.static <E> Closure<E> forClosure(int count, Closure<? super E> closure) Creates a Closure that will call the closurecounttimes.static <E> Closure<E> Create a new Closure that calls another closure based on the result of the specified predicate.static <E> Closure<E> ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure, Closure<? super E> falseClosure) Create a new Closure that calls one of two closures depending on the specified predicate.static <E> Closure<E> invokerClosure(String methodName) Creates a Closure that will invoke a specific method on the closure's input object by reflection.static <E> Closure<E> invokerClosure(String methodName, Class<?>[] paramTypes, Object[] args) Creates a Closure that will invoke a specific method on the closure's input object by reflection.static <E> Closure<E> Gets a Closure that will do nothing.static <E> Closure<E> switchClosure(Map<Predicate<E>, Closure<E>> predicatesAndClosures) Create a new Closure that calls one of the closures depending on the predicates.static <E> Closure<E> switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures) Create a new Closure that calls one of the closures depending on the predicates.static <E> Closure<E> switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> defaultClosure) Create a new Closure that calls one of the closures depending on the predicates.static <E> Closure<E> switchMapClosure(Map<? extends E, Closure<E>> objectsAndClosures) Create a new Closure that uses the input object as a key to find the closure to call.static <E> Closure<E> whileClosure(Predicate<? super E> predicate, Closure<? super E> closure) Creates a Closure that will call the closure repeatedly until the predicate returns false.
- 
Method Details- 
asClosureCreates 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.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- transformer- the transformer to run each time in the closure, null means nop
- Returns:
- the closure
- See Also:
 
- 
chainedClosureCreate a new Closure that calls each closure in turn, passing the result into the next closure.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- closures- an array of closures to chain
- Returns:
- the chainedclosure
- Throws:
- NullPointerException- if the closures array is null
- NullPointerException- if any closure in the array is null
- See Also:
 
- 
chainedClosureCreate 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.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- closures- a collection of closures to chain
- Returns:
- the chainedclosure
- Throws:
- NullPointerException- if the closures collection is null
- NullPointerException- if any closure in the collection is null
- See Also:
 
- 
doWhileClosurepublic static <E> Closure<E> doWhileClosure(Closure<? super E> closure, Predicate<? super E> predicate) Creates a Closure that will call the closure once and then repeatedly until the predicate returns false.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- closure- the closure to call repeatedly, not null
- predicate- the predicate to use as an end of loop test, not null
- Returns:
- the do-whileclosure
- Throws:
- NullPointerException- if either argument is null
- See Also:
 
- 
exceptionClosureGets a Closure that always throws an exception. This could be useful during testing as a placeholder.- Type Parameters:
- E- the type that the closure acts on
- Returns:
- the closure
- See Also:
 
- 
forClosureCreates a Closure that will call the closurecounttimes.A null closure or zero count returns the NOPClosure.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- count- the number of times to loop
- closure- the closure to call repeatedly
- Returns:
- the forclosure
- See Also:
 
- 
ifClosurepublic static <E> Closure<E> ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure) Create a new Closure that calls another closure based on the result of the specified predicate.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- predicate- the validating predicate
- trueClosure- the closure called if the predicate is true
- Returns:
- the ifclosure
- Throws:
- NullPointerException- if the predicate or closure is null
- Since:
- 3.2
- See Also:
 
- 
ifClosurepublic static <E> Closure<E> ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure, Closure<? super E> falseClosure) Create a new Closure that calls one of two closures depending on the specified predicate.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- predicate- the predicate to switch on
- trueClosure- the closure called if the predicate is true
- falseClosure- the closure called if the predicate is false
- Returns:
- the switchclosure
- Throws:
- NullPointerException- if the predicate or either closure is null
- See Also:
 
- 
invokerClosureCreates a Closure that will invoke a specific method on the closure's input object by reflection.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- methodName- the name of the method
- Returns:
- the invokerclosure
- Throws:
- NullPointerException- if the method name is null
- See Also:
 
- 
invokerClosurepublic static <E> Closure<E> invokerClosure(String methodName, Class<?>[] paramTypes, Object[] args) Creates a Closure that will invoke a specific method on the closure's input object by reflection.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- methodName- the name of the method
- paramTypes- the parameter types
- args- the arguments
- Returns:
- the invokerclosure
- Throws:
- NullPointerException- if the method name is null
- IllegalArgumentException- if the paramTypes and args don't match
- See Also:
 
- 
nopClosureGets a Closure that will do nothing. This could be useful during testing as a placeholder.- Type Parameters:
- E- the type that the closure acts on
- Returns:
- the closure
- See Also:
 
- 
switchClosureCreate 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. - Type Parameters:
- E- the type that the closure acts on
- Parameters:
- predicatesAndClosures- a map of predicates to closures
- Returns:
- the switchclosure
- Throws:
- NullPointerException- if the map is null
- NullPointerException- if any closure in the map is null
- ClassCastException- if the map elements are of the wrong type
- See Also:
 
- 
switchClosurepublic static <E> Closure<E> switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] 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. - Type Parameters:
- E- the type that the closure acts on
- Parameters:
- predicates- an array of predicates to check, not null
- closures- an array of closures to call, not null
- Returns:
- the switchclosure
- Throws:
- NullPointerException- if either array is null
- NullPointerException- if any element in the arrays is null
- IllegalArgumentException- if the arrays have different sizes
- See Also:
 
- 
switchClosurepublic static <E> Closure<E> switchClosure(Predicate<? super E>[] predicates, Closure<? super E>[] closures, Closure<? super E> 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. - Type Parameters:
- E- the type that the closure acts on
- Parameters:
- 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
- Returns:
- the switchclosure
- Throws:
- NullPointerException- if either array is null
- NullPointerException- if any element in the arrays is null
- IllegalArgumentException- if the arrays are different sizes
- See Also:
 
- 
switchMapClosureCreate 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. - Type Parameters:
- E- the type that the closure acts on
- Parameters:
- objectsAndClosures- a map of objects to closures
- Returns:
- the closure
- Throws:
- NullPointerException- if the map is null
- NullPointerException- if any closure in the map is null
- See Also:
 
- 
whileClosurepublic static <E> Closure<E> whileClosure(Predicate<? super E> predicate, Closure<? super E> closure) Creates a Closure that will call the closure repeatedly until the predicate returns false.- Type Parameters:
- E- the type that the closure acts on
- Parameters:
- predicate- the predicate to use as an end of loop test, not null
- closure- the closure to call repeatedly, not null
- Returns:
- the whileclosure
- Throws:
- NullPointerException- if either argument is null
- See Also:
 
 
-