|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.collections.ClosureUtils
public class ClosureUtils
ClosureUtils
provides reference implementations and utilities
for the Closure functor interface. The supplied closures are:
Constructor Summary | |
---|---|
ClosureUtils()
This class is not normally instantiated. |
Method Summary | ||
---|---|---|
static
|
asClosure(Transformer<I,O> transformer)
Creates a Closure that calls a Transformer each time it is called. |
|
static
|
chainedClosure(Closure<T>[] closures)
Create a new Closure that calls each closure in turn, passing the result into the next closure. |
|
static
|
chainedClosure(Closure<T> closure1,
Closure<T> closure2)
Create a new Closure that calls two Closures, passing the result of the first into the second. |
|
static
|
chainedClosure(Collection<T> closures)
Create a new Closure that calls each closure in turn, passing the result into the next closure. |
|
static
|
doWhileClosure(Closure<? super T> closure,
Predicate<? super T> 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
|
forClosure(int count,
Closure<T> closure)
Creates a Closure that will call the closure count times. |
|
static
|
ifClosure(Predicate<? super T> predicate,
Closure<? super T> trueClosure,
Closure<? super T> falseClosure)
Create a new Closure that calls one of two closures depending on the specified predicate. |
|
static Closure |
invokerClosure(String methodName)
Creates a Closure that will invoke a specific method on the closure's input object by reflection. |
|
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. |
|
static Closure |
nopClosure()
Gets a Closure that will do nothing. |
|
static
|
switchClosure(Map<Predicate<? super T>,Closure<? super T>> predicatesAndClosures)
Create a new Closure that calls one of the closures depending on the predicates. |
|
static
|
switchClosure(Predicate<? super T>[] predicates,
Closure<? super T>[] closures)
Create a new Closure that calls one of the closures depending on the predicates. |
|
static
|
switchClosure(Predicate<? super T>[] predicates,
Closure<? super T>[] closures,
Closure<? super T> defaultClosure)
Create a new Closure that calls one of the closures depending on the predicates. |
|
static
|
switchMapClosure(Map<T,Closure<T>> objectsAndClosures)
Create a new Closure that uses the input object as a key to find the closure to call. |
|
static
|
whileClosure(Predicate<? super T> predicate,
Closure<? super T> closure)
Creates a Closure that will call the closure repeatedly until the predicate returns false. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ClosureUtils()
Method Detail |
---|
public static Closure exceptionClosure()
ExceptionClosure
public static Closure nopClosure()
NOPClosure
public static <I,O> Closure<I> asClosure(Transformer<I,O> transformer)
transformer
- the transformer to run each time in the closure, null means nop
TransformerClosure
public static <T> Closure<T> forClosure(int count, Closure<T> closure)
count
times.
A null closure or zero count returns the NOPClosure
.
count
- the number of times to loopclosure
- the closure to call repeatedly
for
closureForClosure
public static <T> Closure<T> whileClosure(Predicate<? super T> predicate, Closure<? super T> closure)
predicate
- the predicate to use as an end of loop test, not nullclosure
- the closure to call repeatedly, not null
while
closure
IllegalArgumentException
- if either argument is nullWhileClosure
public static <T> Closure<T> doWhileClosure(Closure<? super T> closure, Predicate<? super T> predicate)
closure
- the closure to call repeatedly, not nullpredicate
- the predicate to use as an end of loop test, not null
do-while
closure
IllegalArgumentException
- if either argument is nullWhileClosure
public static Closure invokerClosure(String methodName)
methodName
- the name of the method
invoker
closure
IllegalArgumentException
- if the method name is nullInvokerTransformer
,
TransformerClosure
public static Closure invokerClosure(String methodName, Class[] paramTypes, Object[] args)
methodName
- the name of the methodparamTypes
- the parameter typesargs
- the arguments
invoker
closure
IllegalArgumentException
- if the method name is null
IllegalArgumentException
- if the paramTypes and args don't matchInvokerTransformer
,
TransformerClosure
public static <T> Closure<T> chainedClosure(Closure<T> closure1, Closure<T> closure2)
closure1
- the first closureclosure2
- the second closure
chained
closure
IllegalArgumentException
- if either closure is nullChainedClosure
public static <T> Closure<T> chainedClosure(Closure<T>[] closures)
closures
- an array of closures to chain
chained
closure
IllegalArgumentException
- if the closures array is null
IllegalArgumentException
- if any closure in the array is nullChainedClosure
public static <T> Closure<T> chainedClosure(Collection<T> closures)
closures
- a collection of closures to chain
chained
closure
IllegalArgumentException
- if the closures collection is null
IllegalArgumentException
- if the closures collection is empty
IllegalArgumentException
- if any closure in the collection is nullChainedClosure
public static <T> Closure<T> ifClosure(Predicate<? super T> predicate, Closure<? super T> trueClosure, Closure<? super T> falseClosure)
predicate
- the predicate to switch ontrueClosure
- the closure called if the predicate is truefalseClosure
- the closure called if the predicate is false
switch
closure
IllegalArgumentException
- if the predicate is null
IllegalArgumentException
- if either closure is nullIfClosure
public static <T> Closure<T> switchClosure(Predicate<? super T>[] predicates, Closure<? super T>[] 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 null
switch
closure
IllegalArgumentException
- if the either array is null
IllegalArgumentException
- if any element in the arrays is null
IllegalArgumentException
- if the arrays are different sizesSwitchClosure
public static <T> Closure<T> switchClosure(Predicate<? super T>[] predicates, Closure<? super T>[] closures, Closure<? super T> 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 matches
switch
closure
IllegalArgumentException
- if the either array is null
IllegalArgumentException
- if any element in the arrays is null
IllegalArgumentException
- if the arrays are different sizesSwitchClosure
public static <T> Closure<T> switchClosure(Map<Predicate<? super T>,Closure<? super T>> 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 closures
switch
closure
IllegalArgumentException
- if the map is null
IllegalArgumentException
- if the map is empty
IllegalArgumentException
- if any closure in the map is null
ClassCastException
- if the map elements are of the wrong typeSwitchClosure
public static <T> Closure<T> switchMapClosure(Map<T,Closure<T>> 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 closures
IllegalArgumentException
- if the map is null
IllegalArgumentException
- if the map is empty
IllegalArgumentException
- if any closure in the map is nullSwitchClosure
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |