Top Description Constructors Methods
org.apache.commons.collections4

public Class ClosureUtils

extends Object
Class Inheritance
Imports
java.util.Collection, .Map, org.apache.commons.collections4.functors.ChainedClosure, .EqualPredicate, .ExceptionClosure, .ForClosure, .IfClosure, .InvokerTransformer, .NOPClosure, .SwitchClosure, .TransformerClosure, .WhileClosure

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 safe are Serializable. Closures considered to be unsafe for serialization are:

Since
3.0

Constructor Summary

AccessConstructor and Description
private
ClosureUtils()

This class is not normally instantiated.

Method Summary

Modifier and TypeMethod and Description
public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the closure
asClosure
(final Transformer<? super E, ?>
the transformer to run each time in the closure, null means nop
transformer
)

Creates a Closure that calls a Transformer each time it is called.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the chained closure
chainedClosure
(final Closure<? super E>...
an array of closures to chain
closures
)

Create a new Closure that calls each closure in turn, passing the result into the next closure.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the chained closure
chainedClosure
(final Collection<? extends Closure<? super E>>
a collection of closures to chain
closures
)

Create a new Closure that calls each closure in turn, passing the result into the next closure.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the do-while closure
doWhileClosure
(final Closure<? super E>
the closure to call repeatedly, not null
closure
,
final Predicate<? super E>
the predicate to use as an end of loop test, not null
predicate
)

Creates a Closure that will call the closure once and then repeatedly until the predicate returns false.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the closure
exceptionClosure
()

Gets a Closure that always throws an exception.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the for closure
forClosure
(final int
the number of times to loop
count
,
final Closure<? super E>
the closure to call repeatedly
closure
)

Creates a Closure that will call the closure count times.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the if closure
ifClosure
(final Predicate<? super E>
the validating predicate
predicate
,
final Closure<? super E>
the closure called if the predicate is true
trueClosure
)

Create a new Closure that calls another closure based on the result of the specified predicate.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the switch closure
ifClosure
(final Predicate<? super E>
the predicate to switch on
predicate
,
final Closure<? super E>
the closure called if the predicate is true
trueClosure
,
final Closure<? super E>
the closure called if the predicate is false
falseClosure
)

Create a new Closure that calls one of two closures depending on the specified predicate.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the invoker closure
invokerClosure
(final String
the name of the method
methodName
)

Creates a Closure that will invoke a specific method on the closure's input object by reflection.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the invoker closure
invokerClosure
(final String
the name of the method
methodName
,
final Class<?>[]
the parameter types
paramTypes
,
final Object[]
the arguments
args
)

Creates a Closure that will invoke a specific method on the closure's input object by reflection.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the closure
nopClosure
()

Gets a Closure that will do nothing.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the switch closure
switchClosure
(final Predicate<? super E>[]
an array of predicates to check, not null
predicates
,
final Closure<? super E>[]
an array of closures to call, not null
closures
)

Create a new Closure that calls one of the closures depending on the predicates.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the switch closure
switchClosure
(final Predicate<? super E>[]
an array of predicates to check, not null
predicates
,
final Closure<? super E>[]
an array of closures to call, not null
closures
,
final Closure<? super E>
the default to call if no predicate matches
defaultClosure
)

Create a new Closure that calls one of the closures depending on the predicates.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the switch closure
switchClosure
(final Map<Predicate<E>, Closure<E>>
a map of predicates to closures
predicatesAndClosures
)

Create a new Closure that calls one of the closures depending on the predicates.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the closure
switchMapClosure
(final Map<? extends E, Closure<E>>
a map of objects to closures
objectsAndClosures
)

Create a new Closure that uses the input object as a key to find the closure to call.

public static <
the type that the closure acts on
E
>
Closure<E>

Returns:

the while closure
whileClosure
(final Predicate<? super E>
the predicate to use as an end of loop test, not null
predicate
,
final Closure<? super E>
the closure to call repeatedly, not null
closure
)

Creates a Closure that will call the closure repeatedly until the predicate returns false.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ClosureUtilsback to summary
private ClosureUtils()

This class is not normally instantiated.

Method Detail

asClosureback to summary
public static <E> Closure<E> asClosure(final Transformer<? super E, ?> transformer)

Creates 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.

Parameters
<E>
the type that the closure acts on
transformer:Transformer<? super E, ?>

the transformer to run each time in the closure, null means nop

Returns:Closure<E>

the closure

See Also
org.apache.commons.collections4.functors.TransformerClosure
chainedClosureback to summary
public static <E> Closure<E> chainedClosure(final Closure<? super E>... closures)

Create a new Closure that calls each closure in turn, passing the result into the next closure.

Parameters
<E>
the type that the closure acts on
closures:Closure<? super E>[]

an array of closures to chain

Returns:Closure<E>

the chained closure

Exceptions
NullPointerException:
  • if the closures array is null
  • if any closure in the array is null
See Also
org.apache.commons.collections4.functors.ChainedClosure
chainedClosureback to summary
public static <E> Closure<E> chainedClosure(final Collection<? extends Closure<? super E>> closures)

Create 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.

Parameters
<E>
the type that the closure acts on
closures:Collection<? extends Closure<? super E>>

a collection of closures to chain

Returns:Closure<E>

the chained closure

Exceptions
NullPointerException:
  • if the closures collection is null
  • if any closure in the collection is null
IllegalArgumentException:
if the closures collection is empty
See Also
org.apache.commons.collections4.functors.ChainedClosure
doWhileClosureback to summary
public static <E> Closure<E> doWhileClosure(final Closure<? super E> closure, final Predicate<? super E> predicate)

Creates a Closure that will call the closure once and then repeatedly until the predicate returns false.

Parameters
<E>
the type that the closure acts on
closure:Closure<? super E>

the closure to call repeatedly, not null

predicate:Predicate<? super E>

the predicate to use as an end of loop test, not null

Returns:Closure<E>

the do-while closure

Exceptions
NullPointerException:
if either argument is null
See Also
org.apache.commons.collections4.functors.WhileClosure
exceptionClosureback to summary
public static <E> Closure<E> exceptionClosure()

Gets a Closure that always throws an exception. This could be useful during testing as a placeholder.

Parameters
<E>
the type that the closure acts on
Returns:Closure<E>

the closure

See Also
org.apache.commons.collections4.functors.ExceptionClosure
forClosureback to summary
public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure)

Creates a Closure that will call the closure count times.

A null closure or zero count returns the NOPClosure.

Parameters
<E>
the type that the closure acts on
count:int

the number of times to loop

closure:Closure<? super E>

the closure to call repeatedly

Returns:Closure<E>

the for closure

See Also
org.apache.commons.collections4.functors.ForClosure
ifClosureback to summary
public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure)

Create a new Closure that calls another closure based on the result of the specified predicate.

Parameters
<E>
the type that the closure acts on
predicate:Predicate<? super E>

the validating predicate

trueClosure:Closure<? super E>

the closure called if the predicate is true

Returns:Closure<E>

the if closure

Exceptions
NullPointerException:
if the predicate or closure is null
Since
3.2
See Also
org.apache.commons.collections4.functors.IfClosure
ifClosureback to summary
public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure, final Closure<? super E> falseClosure)

Create a new Closure that calls one of two closures depending on the specified predicate.

Parameters
<E>
the type that the closure acts on
predicate:Predicate<? super E>

the predicate to switch on

trueClosure:Closure<? super E>

the closure called if the predicate is true

falseClosure:Closure<? super E>

the closure called if the predicate is false

Returns:Closure<E>

the switch closure

Exceptions
NullPointerException:
if the predicate or either closure is null
See Also
org.apache.commons.collections4.functors.IfClosure
invokerClosureback to summary
public static <E> Closure<E> invokerClosure(final String methodName)

Creates a Closure that will invoke a specific method on the closure's input object by reflection.

Parameters
<E>
the type that the closure acts on
methodName:String

the name of the method

Returns:Closure<E>

the invoker closure

Exceptions
NullPointerException:
if the method name is null
See Also
org.apache.commons.collections4.functors.InvokerTransformer, org.apache.commons.collections4.functors.TransformerClosure
invokerClosureback to summary
public static <E> Closure<E> invokerClosure(final String methodName, final Class<?>[] paramTypes, final Object[] args)

Creates a Closure that will invoke a specific method on the closure's input object by reflection.

Parameters
<E>
the type that the closure acts on
methodName:String

the name of the method

paramTypes:Class<?>[]

the parameter types

args:Object[]

the arguments

Returns:Closure<E>

the invoker closure

Exceptions
NullPointerException:
if the method name is null
IllegalArgumentException:
if the paramTypes and args don't match
See Also
org.apache.commons.collections4.functors.InvokerTransformer, org.apache.commons.collections4.functors.TransformerClosure
nopClosureback to summary
public static <E> Closure<E> nopClosure()

Gets a Closure that will do nothing. This could be useful during testing as a placeholder.

Parameters
<E>
the type that the closure acts on
Returns:Closure<E>

the closure

See Also
org.apache.commons.collections4.functors.NOPClosure
switchClosureback to summary
public static <E> Closure<E> switchClosure(final Predicate<? super E>[] predicates, final 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.

Parameters
<E>
the type that the closure acts on
predicates:Predicate<? super E>[]

an array of predicates to check, not null

closures:Closure<? super E>[]

an array of closures to call, not null

Returns:Closure<E>

the switch closure

Exceptions
NullPointerException:
  • if the either array is null
  • if any element in the arrays is null
IllegalArgumentException:
if the arrays have different sizes
See Also
org.apache.commons.collections4.functors.SwitchClosure
switchClosureback to summary
public static <E> Closure<E> switchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, final 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.

Parameters
<E>
the type that the closure acts on
predicates:Predicate<? super E>[]

an array of predicates to check, not null

closures:Closure<? super E>[]

an array of closures to call, not null

defaultClosure:Closure<? super E>

the default to call if no predicate matches

Returns:Closure<E>

the switch closure

Exceptions
NullPointerException:
  • if the either array is null
  • if any element in the arrays is null
IllegalArgumentException:
if the arrays are different sizes
See Also
org.apache.commons.collections4.functors.SwitchClosure
switchClosureback to summary
public static <E> Closure<E> switchClosure(final Map<Predicate<E>, Closure<E>> predicatesAndClosures)

Create 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.

Parameters
<E>
the type that the closure acts on
predicatesAndClosures:Map<Predicate<E>, Closure<E>>

a map of predicates to closures

Returns:Closure<E>

the switch closure

Exceptions
NullPointerException:
  • if the map is null
  • if any closure in the map is null
IllegalArgumentException:
if the map is empty
ClassCastException:
if the map elements are of the wrong type
See Also
org.apache.commons.collections4.functors.SwitchClosure
switchMapClosureback to summary
public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures)

Create 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.

Parameters
<E>
the type that the closure acts on
objectsAndClosures:Map<? extends E, Closure<E>>

a map of objects to closures

Returns:Closure<E>

the closure

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
  • if the map is null
  • if any closure in the map is null
IllegalArgumentException:
if the map is empty
See Also
org.apache.commons.collections4.functors.SwitchClosure
whileClosureback to summary
public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure)

Creates a Closure that will call the closure repeatedly until the predicate returns false.

Parameters
<E>
the type that the closure acts on
predicate:Predicate<? super E>

the predicate to use as an end of loop test, not null

closure:Closure<? super E>

the closure to call repeatedly, not null

Returns:Closure<E>

the while closure

Exceptions
NullPointerException:
if either argument is null
See Also
org.apache.commons.collections4.functors.WhileClosure