public class DefaultedMap extends AbstractMapDecorator implements java.util.Map, java.io.Serializable
Map
returning 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 the
Map.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.
LazyMap
,
Serialized FormModifier and Type | Field and Description |
---|---|
private static long |
serialVersionUID
Serialization version
|
protected java.lang.Object |
value
The transformer to use if the map does not contain a key
|
map
Modifier | Constructor and Description |
---|---|
protected |
DefaultedMap(java.util.Map map,
java.lang.Object value)
Constructor that wraps (not copies).
|
|
DefaultedMap(java.lang.Object defaultValue)
Constructs a new empty
DefaultedMap that decorates
a HashMap . |
Modifier and Type | Method and Description |
---|---|
static java.util.Map |
decorate(java.util.Map map,
Factory factory)
Factory method to create a defaulting map.
|
static java.util.Map |
decorate(java.util.Map map,
java.lang.Object defaultValue)
Factory method to create a defaulting map.
|
static java.util.Map |
decorate(java.util.Map map,
Transformer factory)
Factory method to create a defaulting map.
|
java.lang.Object |
get(java.lang.Object key) |
private void |
readObject(java.io.ObjectInputStream in)
Read the map in using a custom routine.
|
private void |
writeObject(java.io.ObjectOutputStream out)
Write the map out using a custom routine.
|
clear, containsKey, containsValue, entrySet, equals, getMap, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values
private static final long serialVersionUID
protected final java.lang.Object value
public DefaultedMap(java.lang.Object defaultValue)
DefaultedMap
that decorates
a HashMap
.
The object passed in will be returned by the map whenever an unknown key is requested.
defaultValue
- the default value to return when the key is not foundprotected DefaultedMap(java.util.Map map, java.lang.Object value)
map
- the map to decorate, must not be nullvalue
- the value to usejava.lang.IllegalArgumentException
- if map or transformer is nullpublic static java.util.Map decorate(java.util.Map map, java.lang.Object defaultValue)
The value specified is returned when a missing key is found.
map
- the map to decorate, must not be nulldefaultValue
- the default value to return when the key is not foundjava.lang.IllegalArgumentException
- if map is nullpublic static java.util.Map decorate(java.util.Map map, Factory factory)
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.
map
- the map to decorate, must not be nullfactory
- the factory to use, must not be nulljava.lang.IllegalArgumentException
- if map or factory is nullpublic static java.util.Map decorate(java.util.Map map, Transformer factory)
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.
map
- the map to decorate, must not be nullfactory
- the factory to use, must not be nulljava.lang.IllegalArgumentException
- if map or factory is nullprivate void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
out
- the output streamjava.io.IOException
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
in
- the input streamjava.io.IOException
java.lang.ClassNotFoundException
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
get
in class AbstractMapDecorator