public class ClosureUtils extends Object
ClosureUtils provides reference implementations and utilities
for the Closure functor interface. The supplied closures are:
| Modifier and Type | Method and Description |
|---|---|
static <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(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(Collection<Closure<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> |
exceptionClosure()
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 closure
count times. |
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.
|
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> |
nopClosure()
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.
|
public static <E> Closure<E> exceptionClosure()
E - the type that the closure acts onExceptionClosurepublic static <E> Closure<E> nopClosure()
E - the type that the closure acts onNOPClosurepublic static <E> Closure<E> asClosure(Transformer<? super E,?> transformer)
E - the type that the closure acts ontransformer - the transformer to run each time in the closure, null means nopTransformerClosurepublic 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 oncount - the number of times to loopclosure - the closure to call repeatedlyfor closureForClosurepublic static <E> Closure<E> whileClosure(Predicate<? super E> predicate, Closure<? super E> closure)
E - the type that the closure acts onpredicate - the predicate to use as an end of loop test, not nullclosure - the closure to call repeatedly, not nullwhile closureIllegalArgumentException - if either argument is nullWhileClosurepublic static <E> Closure<E> doWhileClosure(Closure<? super E> closure, Predicate<? super E> predicate)
E - the type that the closure acts onclosure - the closure to call repeatedly, not nullpredicate - the predicate to use as an end of loop test, not nulldo-while closureIllegalArgumentException - if either argument is nullWhileClosurepublic static <E> Closure<E> invokerClosure(String methodName)
E - the type that the closure acts onmethodName - the name of the methodinvoker closureIllegalArgumentException - if the method name is nullInvokerTransformer,
TransformerClosurepublic static <E> Closure<E> invokerClosure(String methodName, Class<?>[] paramTypes, Object[] args)
E - the type that the closure acts onmethodName - the name of the methodparamTypes - the parameter typesargs - the argumentsinvoker closureIllegalArgumentException - if the method name is nullIllegalArgumentException - if the paramTypes and args don't matchInvokerTransformer,
TransformerClosurepublic static <E> Closure<E> chainedClosure(Closure<? super E>... closures)
E - the type that the closure acts onclosures - an array of closures to chainchained closureIllegalArgumentException - if the closures array is nullIllegalArgumentException - if any closure in the array is nullChainedClosurepublic static <E> Closure<E> chainedClosure(Collection<Closure<E>> closures)
E - the type that the closure acts onclosures - a collection of closures to chainchained closureIllegalArgumentException - if the closures collection is nullIllegalArgumentException - if the closures collection is emptyIllegalArgumentException - if any closure in the collection is nullChainedClosurepublic static <E> Closure<E> ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure)
E - the type that the closure acts onpredicate - the validating predicatetrueClosure - the closure called if the predicate is trueif closureIllegalArgumentException - if the predicate is nullIllegalArgumentException - if the closure is nullIfClosurepublic static <E> Closure<E> ifClosure(Predicate<? super E> predicate, Closure<? super E> trueClosure, Closure<? super E> falseClosure)
E - the type that the closure acts onpredicate - the predicate to switch ontrueClosure - the closure called if the predicate is truefalseClosure - the closure called if the predicate is falseswitch closureIllegalArgumentException - if the predicate is nullIllegalArgumentException - if either closure is nullIfClosurepublic 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 onpredicates - an array of predicates to check, not nullclosures - an array of closures to call, not nullswitch closureIllegalArgumentException - if the either array is nullIllegalArgumentException - if any element in the arrays is nullIllegalArgumentException - if the arrays are different sizesSwitchClosurepublic 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 onpredicates - an array of predicates to check, not nullclosures - an array of closures to call, not nulldefaultClosure - the default to call if no predicate matchesswitch closureIllegalArgumentException - if the either array is nullIllegalArgumentException - if any element in the arrays is nullIllegalArgumentException - if the arrays are different sizesSwitchClosurepublic 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 onpredicatesAndClosures - a map of predicates to closuresswitch closureIllegalArgumentException - if the map is nullIllegalArgumentException - if the map is emptyIllegalArgumentException - if any closure in the map is nullClassCastException - if the map elements are of the wrong typeSwitchClosurepublic 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 onobjectsAndClosures - a map of objects to closuresIllegalArgumentException - if the map is nullIllegalArgumentException - if the map is emptyIllegalArgumentException - if any closure in the map is nullSwitchClosureCopyright © 2001–2013 The Apache Software Foundation. All rights reserved.