public class DefaultMap<K,V> extends Object implements Map<K,V>, Serializable
Map
decorator that will return a default value from get(java.lang.Object)
if there is no mapping for the key. There are two ways to use this class: static
and dynamic, and they're very different in how they interact with the underlying
map.
The static usage returns a single constant value instance, and does not update the underlying map. This is useful to avoid null checks, for example when examining a list of parameters.
The dynamic usage invokes a factory to create new values, and stores those values
in the underlying map. One example of this is a multi-map, where the factory will
create new List
or Set
instances.
You can also construct an instance that reverses these usage patterns, and either returns a static value while updating the underlying map, or returns a dynamic value and leaves the delegate untouched.
Note that get(java.lang.Object)
is the only method overridden by this class. The
"contains" methods would be mostly useless if they considered default values, and
the behavior of iterators would be difficult to define.
Modifier and Type | Class and Description |
---|---|
static class |
DefaultMap.StaticValueFactory<T>
An implementation of the
DefaultMap.ValueFactory interface for
static values. |
static interface |
DefaultMap.ValueFactory<T>
This interface defines a factory for default values.
|
Constructor and Description |
---|
DefaultMap(Map<K,V> delegate,
ObjectFactory<V> factory)
Constructs an instance that returns a dynamic value and will update
the underlying map.
|
DefaultMap(Map<K,V> delegate,
ObjectFactory<V> factory,
boolean update)
Base constructor, allowing full configuration.
|
DefaultMap(Map<K,V> delegate,
V value)
Constructs an instance that returns a static value and does not update
the underlying map.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<K,V>> |
entrySet() |
boolean |
equals(Object obj)
Two instances are equal if their delegate maps and value factories are equal.
|
V |
get(Object key)
Retrieves an existing mapping, or returns the default value if no
such mapping exists.
|
int |
hashCode()
Returns the hashcode of the delegate map.
|
boolean |
isEmpty() |
Set<K> |
keySet() |
V |
put(K key,
V value)
Stores a new value in the map, replacing any existing value.
|
void |
putAll(Map<? extends K,? extends V> m) |
V |
remove(Object key)
Removes an existing mapping, if there was one, and returns the value of
that mapping.
|
int |
size() |
Collection<V> |
values() |
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public DefaultMap(Map<K,V> delegate, ObjectFactory<V> factory, boolean update)
delegate
- The underlying Map
.factory
- A factory for new values.update
- Pass true
to update the map when a default
value is returned, false
to return the value
and leave the mapping missing.public DefaultMap(Map<K,V> delegate, V value)
delegate
- The underlying Map
.value
- The value to return for missing mappings.public DefaultMap(Map<K,V> delegate, ObjectFactory<V> factory)
delegate
- The underlying Map
.factory
- A factory for new values.public V get(Object key)
This operation is not atomic. You will need external synchronization to make it so (or subclass and synchronize).
public final boolean equals(Object obj)
public final int hashCode()
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
public V put(K key, V value)
public V remove(Object key)