public class ClosureUtils
extends java.lang.Object
ClosureUtils provides reference implementations and utilities
for the Closure functor interface. The supplied closures are:
| Constructor and Description |
|---|
ClosureUtils()
This class is not normally instantiated.
|
| Modifier and Type | Method and Description |
|---|---|
static Closure |
asClosure(Transformer transformer)
Creates a Closure that calls a Transformer each time it is called.
|
static Closure |
chainedClosure(Closure[] closures)
Create a new Closure that calls each closure in turn, passing the
result into the next closure.
|
static Closure |
chainedClosure(Closure closure1,
Closure closure2)
Create a new Closure that calls two Closures, passing the result of
the first into the second.
|
static Closure |
chainedClosure(java.util.Collection closures)
Create a new Closure that calls each closure in turn, passing the
result into the next closure.
|
static Closure |
doWhileClosure(Closure closure,
Predicate predicate)
Creates a Closure that will call the closure once and then repeatedly
until the predicate returns false.
|
static Closure |
exceptionClosure()
Gets a Closure that always throws an exception.
|
static Closure |
forClosure(int count,
Closure closure)
Creates a Closure that will call the closure
count times. |
static Closure |
ifClosure(Predicate predicate,
Closure trueClosure)
Create a new Closure that calls another closure based on the
result of the specified predicate.
|
static Closure |
ifClosure(Predicate predicate,
Closure trueClosure,
Closure falseClosure)
Create a new Closure that calls one of two closures depending
on the specified predicate.
|
static Closure |
invokerClosure(java.lang.String methodName)
Creates a Closure that will invoke a specific method on the closure's
input object by reflection.
|
static Closure |
invokerClosure(java.lang.String methodName,
java.lang.Class[] paramTypes,
java.lang.Object[] args)
Creates a Closure that will invoke a specific method on the closure's
input object by reflection.
|
static Closure |
nopClosure()
Gets a Closure that will do nothing.
|
static Closure |
switchClosure(java.util.Map predicatesAndClosures)
Create a new Closure that calls one of the closures depending
on the predicates.
|
static Closure |
switchClosure(Predicate[] predicates,
Closure[] closures)
Create a new Closure that calls one of the closures depending
on the predicates.
|
static Closure |
switchClosure(Predicate[] predicates,
Closure[] closures,
Closure defaultClosure)
Create a new Closure that calls one of the closures depending
on the predicates.
|
static Closure |
switchMapClosure(java.util.Map objectsAndClosures)
Create a new Closure that uses the input object as a key to find the
closure to call.
|
static Closure |
whileClosure(Predicate predicate,
Closure closure)
Creates a Closure that will call the closure repeatedly until the
predicate returns false.
|
public static Closure exceptionClosure()
ExceptionClosurepublic static Closure nopClosure()
NOPClosurepublic static Closure asClosure(Transformer transformer)
transformer - the transformer to run each time in the closure, null means nopTransformerClosurepublic static Closure forClosure(int count, Closure closure)
count times.
A null closure or zero count returns the NOPClosure.
count - the number of times to loopclosure - the closure to call repeatedlyfor closureForClosurepublic static Closure whileClosure(Predicate predicate, Closure closure)
predicate - the predicate to use as an end of loop test, not nullclosure - the closure to call repeatedly, not nullwhile closurejava.lang.IllegalArgumentException - if either argument is nullWhileClosurepublic static Closure doWhileClosure(Closure closure, Predicate predicate)
closure - the closure to call repeatedly, not nullpredicate - the predicate to use as an end of loop test, not nulldo-while closurejava.lang.IllegalArgumentException - if either argument is nullWhileClosurepublic static Closure invokerClosure(java.lang.String methodName)
methodName - the name of the methodinvoker closurejava.lang.IllegalArgumentException - if the method name is nullInvokerTransformer,
TransformerClosurepublic static Closure invokerClosure(java.lang.String methodName, java.lang.Class[] paramTypes, java.lang.Object[] args)
methodName - the name of the methodparamTypes - the parameter typesargs - the argumentsinvoker closurejava.lang.IllegalArgumentException - if the method name is nulljava.lang.IllegalArgumentException - if the paramTypes and args don't matchInvokerTransformer,
TransformerClosurepublic static Closure chainedClosure(Closure closure1, Closure closure2)
closure1 - the first closureclosure2 - the second closurechained closurejava.lang.IllegalArgumentException - if either closure is nullChainedClosurepublic static Closure chainedClosure(Closure[] closures)
closures - an array of closures to chainchained closurejava.lang.IllegalArgumentException - if the closures array is nulljava.lang.IllegalArgumentException - if any closure in the array is nullChainedClosurepublic static Closure chainedClosure(java.util.Collection closures)
closures - a collection of closures to chainchained closurejava.lang.IllegalArgumentException - if the closures collection is nulljava.lang.IllegalArgumentException - if the closures collection is emptyjava.lang.IllegalArgumentException - if any closure in the collection is nullChainedClosurepublic static Closure ifClosure(Predicate predicate, Closure trueClosure)
predicate - the validating predicatetrueClosure - the closure called if the predicate is trueif closurejava.lang.IllegalArgumentException - if the predicate is nulljava.lang.IllegalArgumentException - if the closure is nullIfClosurepublic static Closure ifClosure(Predicate predicate, Closure trueClosure, Closure falseClosure)
predicate - the predicate to switch ontrueClosure - the closure called if the predicate is truefalseClosure - the closure called if the predicate is falseswitch closurejava.lang.IllegalArgumentException - if the predicate is nulljava.lang.IllegalArgumentException - if either closure is nullIfClosurepublic static Closure switchClosure(Predicate[] predicates, Closure[] 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.
predicates - an array of predicates to check, not nullclosures - an array of closures to call, not nullswitch closurejava.lang.IllegalArgumentException - if the either array is nulljava.lang.IllegalArgumentException - if any element in the arrays is nulljava.lang.IllegalArgumentException - if the arrays are different sizesSwitchClosurepublic static Closure switchClosure(Predicate[] predicates, Closure[] closures, Closure 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.
predicates - an array of predicates to check, not nullclosures - an array of closures to call, not nulldefaultClosure - the default to call if no predicate matchesswitch closurejava.lang.IllegalArgumentException - if the either array is nulljava.lang.IllegalArgumentException - if any element in the arrays is nulljava.lang.IllegalArgumentException - if the arrays are different sizesSwitchClosurepublic static Closure switchClosure(java.util.Map 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.
predicatesAndClosures - a map of predicates to closuresswitch closurejava.lang.IllegalArgumentException - if the map is nulljava.lang.IllegalArgumentException - if the map is emptyjava.lang.IllegalArgumentException - if any closure in the map is nulljava.lang.ClassCastException - if the map elements are of the wrong typeSwitchClosurepublic static Closure switchMapClosure(java.util.Map 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.
objectsAndClosures - a map of objects to closuresjava.lang.IllegalArgumentException - if the map is nulljava.lang.IllegalArgumentException - if the map is emptyjava.lang.IllegalArgumentException - if any closure in the map is nullSwitchClosureCopyright © 2010 - 2023 Adobe. All Rights Reserved