Top Description Fields Constructors Methods
org.apache.commons.collections4.functors

public Class SwitchClosure<E>

extends Object
implements Closure<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.collections4.Closure
Imports
java.io.Serializable, java.util.Map, org.apache.commons.collections4.Closure, .Predicate

Closure implementation calls the closure whose predicate returns true, like a switch statement.
Since
3.0

Field Summary

Modifier and TypeField and Description
private final Closure<? super E>[]
iClosures

The matching closures to call

private final Closure<? super E>
iDefault

The default closure to call if no tests match

private final Predicate<? super E>[]
iPredicates

The tests to consider

private static final long
serialVersionUID

Serial version UID

Constructor Summary

AccessConstructor and Description
private
SwitchClosure(final boolean
if true the input arguments will be cloned
clone
,
final Predicate<? super E>[]
array of predicates, no nulls
predicates
,
final Closure<? super E>[]
matching array of closures, no nulls
closures
,
final Closure<? super E>
the closure to use if no match, null means nop
defaultClosure
)

Hidden constructor for the use by the static factory methods.

public
SwitchClosure(final Predicate<? super E>[]
array of predicates, cloned, no nulls
predicates
,
final Closure<? super E>[]
matching array of closures, cloned, no nulls
closures
,
final Closure<? super E>
the closure to use if no match, null means nop
defaultClosure
)

Constructor that performs no validation.

Method Summary

Modifier and TypeMethod and Description
public void
execute(final E
the input object
input
)

Implements org.apache.commons.collections4.Closure.execute.

Executes the closure whose matching predicate returns true
public Closure<? super E>[]

Returns:

a copy of the closures
getClosures
()

Gets the closures.

public Closure<? super E>

Returns:

the default closure
getDefaultClosure
()

Gets the default closure.

public Predicate<? super E>[]

Returns:

a copy of the predicates
getPredicates
()

Gets the predicates.

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

Returns:

the chained closure
switchClosure
(final Predicate<? super E>[]
array of predicates, cloned, no nulls
predicates
,
final Closure<? super E>[]
matching array of closures, cloned, no nulls
closures
,
final Closure<? super E>
the closure to use if no match, null means nop
defaultClosure
)

Factory method that performs validation and copies the parameter arrays.

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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iClosuresback to summary
private final Closure<? super E>[] iClosures

The matching closures to call

iDefaultback to summary
private final Closure<? super E> iDefault

The default closure to call if no tests match

iPredicatesback to summary
private final Predicate<? super E>[] iPredicates

The tests to consider

serialVersionUIDback to summary
private static final long serialVersionUID

Serial version UID

Constructor Detail

SwitchClosureback to summary
private SwitchClosure(final boolean clone, final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, final Closure<? super E> defaultClosure)

Hidden constructor for the use by the static factory methods.

Parameters
clone:boolean

if true the input arguments will be cloned

predicates:Predicate<? super E>[]

array of predicates, no nulls

closures:Closure<? super E>[]

matching array of closures, no nulls

defaultClosure:Closure<? super E>

the closure to use if no match, null means nop

Annotations
@SuppressWarnings:unchecked
SwitchClosureback to summary
public SwitchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, final Closure<? super E> defaultClosure)

Constructor that performs no validation. Use switchClosure if you want that.

Parameters
predicates:Predicate<? super E>[]

array of predicates, cloned, no nulls

closures:Closure<? super E>[]

matching array of closures, cloned, no nulls

defaultClosure:Closure<? super E>

the closure to use if no match, null means nop

Method Detail

executeback to summary
public void execute(final E input)

Implements org.apache.commons.collections4.Closure.execute.

Executes the closure whose matching predicate returns true

Parameters
input:E

the input object

Annotations
@Override
getClosuresback to summary
public Closure<? super E>[] getClosures()

Gets the closures.

Returns:Closure<? super E>[]

a copy of the closures

Since
3.1
getDefaultClosureback to summary
public Closure<? super E> getDefaultClosure()

Gets the default closure.

Returns:Closure<? super E>

the default closure

Since
3.1
getPredicatesback to summary
public Predicate<? super E>[] getPredicates()

Gets the predicates.

Returns:Predicate<? super E>[]

a copy of the predicates

Since
3.1
switchClosureback to summary
public static <E> Closure<E> switchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, final Closure<? super E> defaultClosure)

Factory method that performs validation and copies the parameter arrays.

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

array of predicates, cloned, no nulls

closures:Closure<? super E>[]

matching array of closures, cloned, no nulls

defaultClosure:Closure<? super E>

the closure to use if no match, null means nop

Returns:Closure<E>

the chained closure

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
  • if array is null
  • if any element in the array is null
IllegalArgumentException:
if the array lengths of predicates and closures do not match
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

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
  • if the map is null
  • if any closure in the map is null
ClassCastException:
if the map elements are of the wrong type