Class DefaultedMap<K,V>
- java.lang.Object
-
- org.apache.commons.collections4.map.AbstractIterableMap<K,V>
-
- org.apache.commons.collections4.map.AbstractMapDecorator<K,V>
-
- org.apache.commons.collections4.map.DefaultedMap<K,V>
-
- Type Parameters:
K- the type of the keys in this mapV- the type of the values in this map
- All Implemented Interfaces:
java.io.Serializable,java.util.Map<K,V>,Get<K,V>,IterableGet<K,V>,IterableMap<K,V>,Put<K,V>
public class DefaultedMap<K,V> extends AbstractMapDecorator<K,V> implements java.io.Serializable
Decorates anotherMapreturning a default value if the map does not contain the requested key.When the
get(Object)method is called with a key that does not exist in the map, this map will return the default value specified in the constructor/factory. Only the get method is altered, so theMap.containsKey(Object)can be used to determine if a key really is in the map or not.The defaulted value is not added to the map. Compare this behaviour with
LazyMap, which does add the value to the map (via a Transformer).For instance:
Map map = new DefaultedMap("NULL"); Object obj = map.get("Surname"); // obj == "NULL"After the above code is executed the map is still empty.
Note that DefaultedMap 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
Collections.synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.- Since:
- 3.2
- See Also:
LazyMap, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static longserialVersionUIDSerialization versionprivate Transformer<? super K,? extends V>valueThe transformer to use if the map does not contain a key-
Fields inherited from class org.apache.commons.collections4.map.AbstractMapDecorator
map
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedDefaultedMap(java.util.Map<K,V> map, Transformer<? super K,? extends V> defaultValueTransformer)Constructor that wraps (not copies).DefaultedMap(Transformer<? super K,? extends V> defaultValueTransformer)Constructs a new emptyDefaultedMapthat decorates aHashMap.DefaultedMap(V defaultValue)Constructs a new emptyDefaultedMapthat decorates aHashMap.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <K,V>
DefaultedMap<K,V>defaultedMap(java.util.Map<K,V> map, Factory<? extends V> factory)Factory method to create a defaulting map.static <K,V>
java.util.Map<K,V>defaultedMap(java.util.Map<K,V> map, Transformer<? super K,? extends V> transformer)Factory method to create a defaulting map.static <K,V>
DefaultedMap<K,V>defaultedMap(java.util.Map<K,V> map, V defaultValue)Factory method to create a defaulting map.Vget(java.lang.Object key)private voidreadObject(java.io.ObjectInputStream in)Read the map in using a custom routine.private voidwriteObject(java.io.ObjectOutputStream out)Write the map out using a custom routine.-
Methods inherited from class org.apache.commons.collections4.map.AbstractMapDecorator
clear, containsKey, containsValue, decorated, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values
-
Methods inherited from class org.apache.commons.collections4.map.AbstractIterableMap
mapIterator
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialization version- See Also:
- Constant Field Values
-
value
private final Transformer<? super K,? extends V> value
The transformer to use if the map does not contain a key
-
-
Constructor Detail
-
DefaultedMap
public DefaultedMap(V defaultValue)
Constructs a new emptyDefaultedMapthat decorates aHashMap.The object passed in will be returned by the map whenever an unknown key is requested.
- Parameters:
defaultValue- the default value to return when the key is not found
-
DefaultedMap
public DefaultedMap(Transformer<? super K,? extends V> defaultValueTransformer)
Constructs a new emptyDefaultedMapthat decorates aHashMap.- Parameters:
defaultValueTransformer- transformer to use to generate missing values.
-
DefaultedMap
protected DefaultedMap(java.util.Map<K,V> map, Transformer<? super K,? extends V> defaultValueTransformer)
Constructor that wraps (not copies).- Parameters:
map- the map to decorate, must not be nulldefaultValueTransformer- the value transformer to use- Throws:
java.lang.NullPointerException- if map or transformer is null
-
-
Method Detail
-
defaultedMap
public static <K,V> DefaultedMap<K,V> defaultedMap(java.util.Map<K,V> map, V defaultValue)
Factory method to create a defaulting map.The value specified is returned when a missing key is found.
- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the map to decorate, must not be nulldefaultValue- the default value to return when the key is not found- Returns:
- a new defaulting map
- Throws:
java.lang.NullPointerException- if map is null- Since:
- 4.0
-
defaultedMap
public static <K,V> DefaultedMap<K,V> defaultedMap(java.util.Map<K,V> map, Factory<? extends V> factory)
Factory method to create a defaulting map.The factory specified is called when a missing key is found. The result will be returned as the result of the map get(key) method.
- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the map to decorate, must not be nullfactory- the factory to use to create entries, must not be null- Returns:
- a new defaulting map
- Throws:
java.lang.NullPointerException- if map or factory is null- Since:
- 4.0
-
defaultedMap
public static <K,V> java.util.Map<K,V> defaultedMap(java.util.Map<K,V> map, Transformer<? super K,? extends V> transformer)Factory method to create a defaulting map.The transformer specified is called when a missing key is found. The key is passed to the transformer as the input, and the result will be returned as the result of the map get(key) method.
- Type Parameters:
K- the key typeV- the value type- Parameters:
map- the map to decorate, must not be nulltransformer- the transformer to use as a factory to create entries, must not be null- Returns:
- a new defaulting map
- Throws:
java.lang.NullPointerException- if map or factory is null- Since:
- 4.0
-
writeObject
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOExceptionWrite the map out using a custom routine.- Parameters:
out- the output stream- Throws:
java.io.IOException- if an error occurs while writing to the stream
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundExceptionRead the map in using a custom routine.- Parameters:
in- the input stream- Throws:
java.io.IOException- if an error occurs while reading from the streamjava.lang.ClassNotFoundException- if an object read from the stream can not be loaded
-
get
public V get(java.lang.Object key)
- Specified by:
getin interfaceGet<K,V>- Specified by:
getin interfacejava.util.Map<K,V>- Overrides:
getin classAbstractMapDecorator<K,V>- Parameters:
key- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
nullif this map contains no mapping for the key - See Also:
Map.get(Object)
-
-