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 java.. This class may throw
exceptions when accessed by concurrent threads without synchronization.
LazyMap
| Modifier and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
Serialization version |
| private final Transformer | value
The transformer to use if the map does not contain a key |
| Access | Constructor and Description |
|---|---|
| public | DefaultedMap(final V
the default value to return when the key is not found defaultValue)Constructs a new empty |
| public | DefaultedMap(final Transformer<? super K, ? extends V>
transformer to use to generate missing values. defaultValueTransformer)Constructs a new empty |
| protected | DefaultedMap(final Map<K, V>
the map to decorate, must not be null map, final Transformer<? super K, ? extends V> the value transformer to use defaultValueTransformer)Constructor that wraps (not copies). |
| Modifier and Type | Method and Description |
|---|---|
| public static < the key type K, the value type V> DefaultedMap | Returns: a new defaulting mapthe map to decorate, must not be null map, final V the default value to return when the key is not found defaultValue)Factory method to create a defaulting map. |
| public static < the key type K, the value type V> DefaultedMap | Returns: a new defaulting mapthe map to decorate, must not be null map, final Factory<? extends V> the factory to use to create entries, must not be null factory)Factory method to create a defaulting map. |
| public static < the key type K, the value type V> Map | Returns: a new defaulting mapthe map to decorate, must not be null map, final Transformer<? super K, ? extends V> the transformer to use as a factory to create entries, must not be null transformer)Factory method to create a defaulting map. |
| public V | get(final Object
the key whose associated value is to be returned key)Overrides org. Implements java. null if this map contains no mapping for the key.
|
| private void | |
| private void |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version | |
| value | back to summary |
|---|---|
| private final Transformer<? super K, ? extends V> value The transformer to use if the map does not contain a key | |
| DefaultedMap | back to summary |
|---|---|
| public DefaultedMap(final V defaultValue) Constructs a new empty The object passed in will be returned by the map whenever an unknown key is requested.
| |
| DefaultedMap | back to summary |
|---|---|
| public DefaultedMap(final Transformer<? super K, ? extends V> defaultValueTransformer) Constructs a new empty
| |
| DefaultedMap | back to summary |
|---|---|
| protected DefaultedMap(final Map<K, V> map, final Transformer<? super K, ? extends V> defaultValueTransformer) Constructor that wraps (not copies).
| |
| defaultedMap | back to summary |
|---|---|
| public static <K, V> DefaultedMap Factory method to create a defaulting map. The value specified is returned when a missing key is found.
| |
| defaultedMap | back to summary |
|---|---|
| public static <K, V> DefaultedMap 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.
| |
| defaultedMap | back to summary |
|---|---|
| public static <K, V> Map 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.
| |
| get | back to summary |
|---|---|
| public V get(final Object key) Overrides org. Implements java. Doc from java. Returns the value to which the specified key is mapped,
or More formally, if this map contains a mapping from a key
If this map permits null values, then a return value of
| |
| readObject | back to summary |
|---|---|
| private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException Read the map in using a custom routine.
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(final ObjectOutputStream out) throws IOException Write the map out using a custom routine.
| |