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

public Class SwitchTransformer<I, O>

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

Transformer implementation calls the transformer whose predicate returns true, like a switch statement.
Since
3.0

Field Summary

Modifier and TypeField and Description
private final Transformer<? super I, ? extends O>
iDefault

The default transformer to call if no tests match

private final Predicate<? super I>[]
iPredicates

The tests to consider

private final Transformer<? super I, ? extends O>[]
iTransformers

The matching transformers to call

private static final long
serialVersionUID

Serial version UID

Constructor Summary

AccessConstructor and Description
private
SwitchTransformer(final boolean
if true the input arguments will be cloned
clone
,
final Predicate<? super I>[]
array of predicates, no nulls
predicates
,
final Transformer<? super I, ? extends O>[]
matching array of transformers, no nulls
transformers
,
final Transformer<? super I, ? extends O>
the transformer to use if no match, null means return null
defaultTransformer
)

Hidden constructor for the use by the static factory methods.

public
SwitchTransformer(final Predicate<? super I>[]
array of predicates, cloned, no nulls
predicates
,
final Transformer<? super I, ? extends O>[]
matching array of transformers, cloned, no nulls
transformers
,
final Transformer<? super I, ? extends O>
the transformer to use if no match, null means return null
defaultTransformer
)

Constructor that performs no validation.

Method Summary

Modifier and TypeMethod and Description
public Transformer<? super I, ? extends O>

Returns:

the default transformer
getDefaultTransformer
()

Gets the default transformer.

public Predicate<? super I>[]

Returns:

a copy of the predicates
getPredicates
()

Gets the predicates.

public Transformer<? super I, ? extends O>[]

Returns:

a copy of the transformers
getTransformers
()

Gets the transformers.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the chained transformer
switchTransformer
(final Predicate<? super I>[]
array of predicates, cloned, no nulls
predicates
,
final Transformer<? super I, ? extends O>[]
matching array of transformers, cloned, no nulls
transformers
,
final Transformer<? super I, ? extends O>
the transformer to use if no match, null means return null
defaultTransformer
)

Factory method that performs validation and copies the parameter arrays.

public static <
the input type
I
,
the output type
O
>
Transformer<I, O>

Returns:

the switch transformer
switchTransformer
(final Map<? extends Predicate<? super I>, ? extends Transformer<? super I, ? extends O>>
a map of predicates to transformers
map
)

Create a new Transformer that calls one of the transformers depending on the predicates.

public O

Returns:

the transformed result
transform
(final I
the input object to transform
input
)

Implements org.apache.commons.collections4.Transformer.transform.

Transforms the input to result by calling the transformer whose matching predicate returns true.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iDefaultback to summary
private final Transformer<? super I, ? extends O> iDefault

The default transformer to call if no tests match

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

The tests to consider

iTransformersback to summary
private final Transformer<? super I, ? extends O>[] iTransformers

The matching transformers to call

serialVersionUIDback to summary
private static final long serialVersionUID

Serial version UID

Constructor Detail

SwitchTransformerback to summary
private SwitchTransformer(final boolean clone, final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O> defaultTransformer)

Hidden constructor for the use by the static factory methods.

Parameters
clone:boolean

if true the input arguments will be cloned

predicates:Predicate<? super I>[]

array of predicates, no nulls

transformers:Transformer<? super I, ? extends O>[]

matching array of transformers, no nulls

defaultTransformer:Transformer<? super I, ? extends O>

the transformer to use if no match, null means return null

Annotations
@SuppressWarnings:unchecked
SwitchTransformerback to summary
public SwitchTransformer(final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O> defaultTransformer)

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

Parameters
predicates:Predicate<? super I>[]

array of predicates, cloned, no nulls

transformers:Transformer<? super I, ? extends O>[]

matching array of transformers, cloned, no nulls

defaultTransformer:Transformer<? super I, ? extends O>

the transformer to use if no match, null means return null

Method Detail

getDefaultTransformerback to summary
public Transformer<? super I, ? extends O> getDefaultTransformer()

Gets the default transformer.

Returns:Transformer<? super I, ? extends O>

the default transformer

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

Gets the predicates.

Returns:Predicate<? super I>[]

a copy of the predicates

Since
3.1
getTransformersback to summary
public Transformer<? super I, ? extends O>[] getTransformers()

Gets the transformers.

Returns:Transformer<? super I, ? extends O>[]

a copy of the transformers

Since
3.1
switchTransformerback to summary
public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O> defaultTransformer)

Factory method that performs validation and copies the parameter arrays.

Parameters
<I>
the input type
<O>
the output type
predicates:Predicate<? super I>[]

array of predicates, cloned, no nulls

transformers:Transformer<? super I, ? extends O>[]

matching array of transformers, cloned, no nulls

defaultTransformer:Transformer<? super I, ? extends O>

the transformer to use if no match, null means return null

Returns:Transformer<I, O>

the chained transformer

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
  • if array is null
  • if any element in the array is null
switchTransformerback to summary
public static <I, O> Transformer<I, O> switchTransformer(final Map<? extends Predicate<? super I>, ? extends Transformer<? super I, ? extends O>> map)

Create a new Transformer that calls one of the transformers depending on the predicates.

The Map consists of Predicate keys and Transformer values. A transformer is called if its matching predicate returns true. Each predicate is evaluated until one returns true. If no predicates evaluate to true, the default transformer is called. The default transformer 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
<I>
the input type
<O>
the output type
map:Map<? extends Predicate<? super I>, ? extends Transformer<? super I, ? extends O>>

a map of predicates to transformers

Returns:Transformer<I, O>

the switch transformer

Annotations
@SuppressWarnings:unchecked
Exceptions
NullPointerException:
  • if the map is null
  • if any transformer in the map is null
ClassCastException:
if the map elements are of the wrong type
transformback to summary
public O transform(final I input)

Implements org.apache.commons.collections4.Transformer.transform.

Transforms the input to result by calling the transformer whose matching predicate returns true.

Parameters
input:I

the input object to transform

Returns:O

the transformed result

Annotations
@Override