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

public Class TransformedMap<K, V>

extends AbstractInputCheckedMapDecorator<K, V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
org.apache.commons.collections4.map.TransformedSortedMap
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.Map, org.apache.commons.collections4.Transformer

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.

This class is Serializable from Commons Collections 3.1.

Since
3.0
See Also
org.apache.commons.collections4.splitmap.TransformedSplitMap

Field Summary

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

The transformer to use for the key

private static final long
serialVersionUID

Serialization version

protected final Transformer<? super V, ? extends V>
valueTransformer

The transformer to use for the value

Constructor Summary

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

Constructor that wraps (not copies).

Method Summary

Modifier and TypeMethod and Description
protected V

Returns:

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

Implements abstract org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.checkSetValue.

Override to transform the value when using setValue.
protected boolean

Returns:

true if a value transformer is in use
isSetValueChecking
()

Overrides org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.isSetValueChecking.

Override to only return true when there is a value transformer.
public V
put(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.put.

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Associates the specified value with the specified key in this map (optional operation).
public void
putAll(Map<? extends K, ? extends V>
mappings to be stored in this map
mapToCopy
)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.putAll.

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Copies all of the mappings from the specified map to this map (optional operation).
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

public static <
the key type
K
,
the value type
V
>
TransformedMap<K, V>

Returns:

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

Factory method to create a transforming map that will transform existing contents of the specified map.

public static <
the key type
K
,
the value type
V
>
TransformedMap<K, V>

Returns:

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

Factory method to create a transforming map.

protected K

Returns:

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

Transforms a key.

protected Map<K, V>

Returns:

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

Transforms a map.

protected V

Returns:

the transformed object
transformValue
(final V
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.map.AbstractInputCheckedMapDecorator:
entrySet

Field Detail

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

The transformer to use for the key

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

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

The transformer to use for the value

Constructor Detail

TransformedMapback to summary
protected TransformedMap(final Map<K, V> map, final Transformer<? super K, ? extends K> keyTransformer, final Transformer<? super V, ? 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 K, ? extends K>

the transformer to use for key conversion, null means no conversion

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

the transformer to use for value conversion, null means no conversion

Exceptions
NullPointerException:
if map is null

Method Detail

checkSetValueback to summary
protected V checkSetValue(final V value)

Implements abstract org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.checkSetValue.

Override to transform the value when using setValue.

Parameters
value:V

the value to transform

Returns:V

the transformed value

Annotations
@Override
Since
3.1
isSetValueCheckingback to summary
protected boolean isSetValueChecking()

Overrides org.apache.commons.collections4.map.AbstractInputCheckedMapDecorator.isSetValueChecking.

Override to only return true when there is a value transformer.

Returns:boolean

true if a value transformer is in use

Annotations
@Override
Since
3.1
putback to summary
public V put(K key, V value)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.put.

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Doc from java.util.Map.put.

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Parameters
key:K

key with which the specified value is to be associated

value:V

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(Map<? extends K, ? extends V> mapToCopy)

Overrides org.apache.commons.collections4.map.AbstractMapDecorator.putAll.

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Doc from java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress. If the specified map has a defined encounter order, processing of its mappings generally occurs in that order.

Parameters
mapToCopy:Map<? extends K, ? extends V>

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
transformedMapback to summary
public static <K, V> TransformedMap<K, V> transformedMap(final Map<K, V> map, final Transformer<? super K, ? extends K> keyTransformer, final Transformer<? super V, ? extends V> valueTransformer)

Factory method to create a transforming map that will transform existing contents of the specified map.

If there are any elements already in the map being decorated, they will be transformed by this method. Contrast this with transformingMap(Map, Transformer, Transformer).

Parameters
<K>
the key type
<V>
the value type
map:Map<K, V>

the map to decorate, must not be null

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

the transformer to use for key conversion, null means no transformation

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

the transformer to use for value conversion, null means no transformation

Returns:TransformedMap<K, V>

a new transformed map

Exceptions
NullPointerException:
if map is null
Since
4.0
transformingMapback to summary
public static <K, V> TransformedMap<K, V> transformingMap(final Map<K, V> map, final Transformer<? super K, ? extends K> keyTransformer, final Transformer<? super V, ? 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. Contrast this with transformedMap(Map, Transformer, Transformer).

Parameters
<K>
the key type
<V>
the value type
map:Map<K, V>

the map to decorate, must not be null

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

the transformer to use for key conversion, null means no transformation

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

the transformer to use for value conversion, null means no transformation

Returns:TransformedMap<K, V>

a new transformed map

Exceptions
NullPointerException:
if map is null
Since
4.0
transformKeyback to summary
protected K transformKey(final K object)

Transforms a key.

The transformer itself may throw an exception if necessary.

Parameters
object:K

the object to transform

Returns:K

the transformed object

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

Transforms a map.

The transformer itself may throw an exception if necessary.

Parameters
map:Map<? extends K, ? extends V>

the map to transform

Returns:Map<K, V>

the transformed object

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

Transforms a value.

The transformer itself may throw an exception if necessary.

Parameters
object:V

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
Since
3.1