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

public Class TransformedSplitMap<J, K, U, V>

extends AbstractIterableGetMapDecorator<K, V>
implements Put<J, U>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.collections4.Put
Type Parameters
<J>
the type of the keys to put in this map
<K>
the type of the keys to get in this map
<U>
the type of the values to put in this map
<V>
the type of the values to get in this map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.Map, org.apache.commons.collections4.Put, .Transformer, org.apache.commons.collections4.map.LinkedMap

Decorates another Map to transform objects that are added.

The Map put methods and Map.Entry setValue method are affected by this class. Thus objects must be removed or searched for using their transformed form. For example, if the transformation converts Strings to Integers, you must use the Integer form to remove objects.

Note that TransformedMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using java.util.Collections#synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

The "put" and "get" type constraints of this class are mutually independent; contrast with org.apache.commons.collections4.map.TransformedMap which, by virtue of its implementing Map<K, V>, must be constructed in such a way that its read and write parameters are generalized to a common (super-)type. In practice this would often mean >Object, Object>, defeating much of the usefulness of having parameterized types.

On the downside, this class is not drop-in compatible with java.util.Map but is intended to be worked with either directly or by Put and Get generalizations.

Since
4.0
See Also
org.apache.commons.collections4.SplitMapUtils#readableMap(org.apache.commons.collections4.Get), org.apache.commons.collections4.SplitMapUtils#writableMap(Put)

Field Summary

Modifier and TypeField and Description
private final Transformer<? super J, ? extends K>
keyTransformer

The transformer to use for the key

private static final long
serialVersionUID

Serialization version

private final Transformer<? super U, ? extends V>
valueTransformer

The transformer to use for the value

Inherited from org.apache.commons.collections4.splitmap.AbstractIterableGetMapDecorator:
map

Constructor Summary

AccessConstructor and Description
protected
TransformedSplitMap(final Map<K, V>
the map to decorate, must not be null
map
,
final Transformer<? super J, ? extends K>
the transformer to use for key conversion, must not be null
keyTransformer
,
final Transformer<? super U, ? extends V>
the transformer to use for value conversion, must not be null
valueTransformer
)

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
protected V

Returns:

the transformed value
checkSetValue
(final U
the value to transform
value
)

Override to transform the value when using setValue.

public void
public V
put(final J
key with which the specified value is to be associated
key
,
final U
value to be associated with the specified key
value
)

Implements org.apache.commons.collections4.Put.put.

Note that the return type is Object, rather than V as in the Map interface.
public void
putAll(final Map<? extends J, ? extends U>
mappings to be stored in this map
mapToCopy
)

Implements org.apache.commons.collections4.Put.putAll.

private void
readObject(final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

public static <
the input key type
J
,
the output key type
K
,
the input value type
U
,
the output value type
V
>
TransformedSplitMap<J, K, U, V>

Returns:

a new transformed map
transformingMap
(final Map<K, V>
the map to decorate, must not be null
map
,
final Transformer<? super J, ? extends K>
the transformer to use for key conversion, must not be null
keyTransformer
,
final Transformer<? super U, ? extends V>
the transformer to use for value conversion, must not be null
valueTransformer
)

Factory method to create a transforming map.

protected K

Returns:

the transformed object
transformKey
(final J
the object to transform
object
)

Transforms a key.

protected Map<K, V>

Returns:

the transformed object
transformMap
(final Map<? extends J, ? extends U>
the map to transform
map
)

Transforms a map.

protected V

Returns:

the transformed object
transformValue
(final U
the object to transform
object
)

Transforms a value.

private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the map out using a custom routine.

Inherited from org.apache.commons.collections4.splitmap.AbstractIterableGetMapDecorator:
containsKeycontainsValuedecoratedentrySetequalsgethashCodeisEmptykeySetmapIteratorremovesizetoStringvalues

Field Detail

keyTransformerback to summary
private final Transformer<? super J, ? extends K> keyTransformer

The transformer to use for the key

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

valueTransformerback to summary
private final Transformer<? super U, ? extends V> valueTransformer

The transformer to use for the value

Constructor Detail

TransformedSplitMapback to summary
protected TransformedSplitMap(final Map<K, V> map, final Transformer<? super J, ? extends K> keyTransformer, final Transformer<? super U, ? extends V> valueTransformer)

Constructor that wraps (not copies).

If there are any elements already in the collection being decorated, they are NOT transformed.

Parameters
map:Map<K, V>

the map to decorate, must not be null

keyTransformer:Transformer<? super J, ? extends K>

the transformer to use for key conversion, must not be null

valueTransformer:Transformer<? super U, ? extends V>

the transformer to use for value conversion, must not be null

Exceptions
NullPointerException:
if map or either of the transformers is null

Method Detail

checkSetValueback to summary
protected V checkSetValue(final U value)

Override to transform the value when using setValue.

Parameters
value:U

the value to transform

Returns:V

the transformed value

clearback to summary
public void clear()

Implements org.apache.commons.collections4.Put.clear.

Annotations
@Override
putback to summary
public V put(final J key, final U value)

Implements org.apache.commons.collections4.Put.put.

Doc from org.apache.commons.collections4.Put.put.

Note that the return type is Object, rather than V as in the Map interface. See the class Javadoc for further info.

Parameters
key:J

key with which the specified value is to be associated

value:U

value to be associated with the specified key

Returns:V

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

Annotations
@Override
putAllback to summary
public void putAll(final Map<? extends J, ? extends U> mapToCopy)

Implements org.apache.commons.collections4.Put.putAll.

Parameters
mapToCopy:Map<? extends J, ? extends U>

Doc from org.apache.commons.collections4.Put.putAll.

mappings to be stored in this map

Annotations
@Override
readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the map in using a custom routine.

Parameters
in:ObjectInputStream

the input stream

Annotations
@SuppressWarnings:unchecked
Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
Since
3.1
transformingMapback to summary
public static <J, K, U, V> TransformedSplitMap<J, K, U, V> transformingMap(final Map<K, V> map, final Transformer<? super J, ? extends K> keyTransformer, final Transformer<? super U, ? extends V> valueTransformer)

Factory method to create a transforming map.

If there are any elements already in the map being decorated, they are NOT transformed.

Parameters
<J>
the input key type
<K>
the output key type
<U>
the input value type
<V>
the output value type
map:Map<K, V>

the map to decorate, must not be null

keyTransformer:Transformer<? super J, ? extends K>

the transformer to use for key conversion, must not be null

valueTransformer:Transformer<? super U, ? extends V>

the transformer to use for value conversion, must not be null

Returns:TransformedSplitMap<J, K, U, V>

a new transformed map

Exceptions
NullPointerException:
if map or either of the transformers is null
transformKeyback to summary
protected K transformKey(final J object)

Transforms a key.

The transformer itself may throw an exception if necessary.

Parameters
object:J

the object to transform

Returns:K

the transformed object

transformMapback to summary
protected Map<K, V> transformMap(final Map<? extends J, ? extends U> map)

Transforms a map.

The transformer itself may throw an exception if necessary.

Parameters
map:Map<? extends J, ? extends U>

the map to transform

Returns:Map<K, V>

the transformed object

Annotations
@SuppressWarnings:unchecked
transformValueback to summary
protected V transformValue(final U object)

Transforms a value.

The transformer itself may throw an exception if necessary.

Parameters
object:U

the object to transform

Returns:V

the transformed object

writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the map out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream