Class ForwardingMap<K,V>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.collect.ForwardingMap<K,V>
-
- All Implemented Interfaces:
Map<K,V>
- Direct Known Subclasses:
EnumBiMap
,EnumHashBiMap
,ForwardingConcurrentMap
,ForwardingNavigableMap.StandardDescendingMap
,ForwardingSortedMap
,ImmutableClassToInstanceMap
,ImmutableTypeToInstanceMap
,MutableClassToInstanceMap
,MutableTypeToInstanceMap
@GwtCompatible public abstract class ForwardingMap<K,V> extends ForwardingObject implements Map<K,V>
A map which forwards all its method calls to another map. Subclasses should override one or more methods to modify the behavior of the backing map as desired per the decorator pattern.Warning: The methods of
ForwardingMap
forward indiscriminately to the methods of the delegate. For example, overridingput(K, V)
alone will not change the behavior ofputAll(java.util.Map<? extends K, ? extends V>)
, which can lead to unexpected behavior. In this case, you should overrideputAll
as well, either providing your own implementation, or delegating to the providedstandardPutAll
method.default
method warning: This class does not forward calls todefault
methods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on theForwardingMap
.Each of the
standard
methods, where appropriate, useObjects.equal(java.lang.Object, java.lang.Object)
to test equality for both keys and values. This may not be the desired behavior for map implementations that use non-standard notions of key equality, such as aSortedMap
whose comparator is not consistent withequals
.The
standard
methods and the collection views they return are not guaranteed to be thread-safe, even when all of the methods that they depend on are thread-safe.- Since:
- 2.0
- Author:
- Kevin Bourrillion, Jared Levy, Louis Wasserman
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
ForwardingMap.StandardEntrySet
A sensible implementation ofMap.entrySet()
in terms of the following methods:clear()
,containsKey(java.lang.Object)
,get(java.lang.Object)
,isEmpty()
,remove(java.lang.Object)
, andsize()
.protected class
ForwardingMap.StandardKeySet
A sensible implementation ofMap.keySet()
in terms of the following methods:clear()
,containsKey(java.lang.Object)
,isEmpty()
,remove(java.lang.Object)
,size()
, and theSet.iterator()
method ofentrySet()
.protected class
ForwardingMap.StandardValues
A sensible implementation ofMap.values()
in terms of the following methods:clear()
,containsValue(java.lang.Object)
,isEmpty()
,size()
, and theSet.iterator()
method ofentrySet()
.
-
Constructor Summary
Constructors Modifier Constructor Description protected
ForwardingMap()
Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all of the mappings from this map (optional operation).boolean
containsKey(@Nullable Object key)
Returnstrue
if this map contains a mapping for the specified key.boolean
containsValue(@Nullable Object value)
Returnstrue
if this map maps one or more keys to the specified value.protected abstract Map<K,V>
delegate()
Returns the backing delegate instance that methods are forwarded to.Set<Map.Entry<K,V>>
entrySet()
Returns aSet
view of the mappings contained in this map.boolean
equals(@Nullable Object object)
Indicates whether some other object is "equal to" this one.V
get(@Nullable Object key)
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.int
hashCode()
Returns a hash code value for the object.boolean
isEmpty()
Returnstrue
if this map contains no key-value mappings.Set<K>
keySet()
Returns aSet
view of the keys contained in this map.V
put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).void
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map (optional operation).V
remove(Object key)
Removes the mapping for a key from this map if it is present (optional operation).int
size()
Returns the number of key-value mappings in this map.protected void
standardClear()
protected boolean
standardContainsKey(@Nullable Object key)
A sensible, albeit inefficient, definition ofcontainsKey(java.lang.Object)
in terms of theiterator
method ofentrySet()
.protected boolean
standardContainsValue(@Nullable Object value)
A sensible definition ofcontainsValue(java.lang.Object)
in terms of theiterator
method ofentrySet()
.protected boolean
standardEquals(@Nullable Object object)
protected int
standardHashCode()
protected boolean
standardIsEmpty()
protected void
standardPutAll(Map<? extends K,? extends V> map)
A sensible definition ofputAll(Map)
in terms ofput(Object, Object)
.protected V
standardRemove(@Nullable Object key)
A sensible, albeit inefficient, definition ofremove(java.lang.Object)
in terms of theiterator
method ofentrySet()
.protected String
standardToString()
Collection<V>
values()
Returns aCollection
view of the values contained in this map.-
Methods inherited from class com.google.common.collect.ForwardingObject
toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
ForwardingMap
protected ForwardingMap()
Constructor for use by subclasses.
-
-
Method Detail
-
delegate
protected abstract Map<K,V> delegate()
Description copied from class:ForwardingObject
Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such asForwardingSet.delegate()
. Concrete subclasses override this method to supply the instance being decorated.- Specified by:
delegate
in classForwardingObject
-
size
public int size()
Description copied from interface:java.util.Map
Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.
-
isEmpty
public boolean isEmpty()
Description copied from interface:java.util.Map
Returnstrue
if this map contains no key-value mappings.
-
remove
@CanIgnoreReturnValue public V remove(Object key)
Description copied from interface:java.util.Map
Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from keyk
to valuev
such thatObjects.equals(key, k)
, that mapping is removed. (The map can contain at most one such mapping.)Returns the value to which this map previously associated the key, or
null
if the map contained no mapping for the key.If this map permits null values, then a return value of
null
does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key tonull
.The map will not contain a mapping for the specified key once the call returns.
-
clear
public void clear()
Description copied from interface:java.util.Map
Removes all of the mappings from this map (optional operation). The map will be empty after this call returns.
-
containsKey
public boolean containsKey(@Nullable Object key)
Description copied from interface:java.util.Map
Returnstrue
if this map contains a mapping for the specified key. More formally, returnstrue
if and only if this map contains a mapping for a keyk
such thatObjects.equals(key, k)
. (There can be at most one such mapping.)- Specified by:
containsKey
in interfaceMap<K,V>
- Parameters:
key
- key whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified key
-
containsValue
public boolean containsValue(@Nullable Object value)
Description copied from interface:java.util.Map
Returnstrue
if this map maps one or more keys to the specified value. More formally, returnstrue
if and only if this map contains at least one mapping to a valuev
such thatObjects.equals(value, v)
. This operation will probably require time linear in the map size for most implementations of theMap
interface.- Specified by:
containsValue
in interfaceMap<K,V>
- Parameters:
value
- value whose presence in this map is to be tested- Returns:
true
if this map maps one or more keys to the specified value
-
get
public V get(@Nullable Object key)
Description copied from interface:java.util.Map
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.More formally, if this map contains a mapping from a key
k
to a valuev
such thatObjects.equals(key, k)
, then this method returnsv
; otherwise it returnsnull
. (There can be at most one such mapping.)If this map permits null values, then a return value of
null
does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull
. ThecontainsKey
operation may be used to distinguish these two cases.
-
put
@CanIgnoreReturnValue public V put(K key, V value)
Description copied from interface:java.util.Map
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 mapm
is said to contain a mapping for a keyk
if and only ifm.containsKey(k)
would returntrue
.)- Specified by:
put
in interfaceMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
. (Anull
return can also indicate that the map previously associatednull
withkey
, if the implementation supportsnull
values.)
-
putAll
public void putAll(Map<? extends K,? extends V> map)
Description copied from interface:java.util.Map
Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of callingput(k, v)
on this map once for each mapping from keyk
to valuev
in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
-
keySet
public Set<K> keySet()
Description copied from interface:java.util.Map
Returns aSet
view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's ownremove
operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove
,Set.remove
,removeAll
,retainAll
, andclear
operations. It does not support theadd
oraddAll
operations.
-
values
public Collection<V> values()
Description copied from interface:java.util.Map
Returns aCollection
view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's ownremove
operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via theIterator.remove
,Collection.remove
,removeAll
,retainAll
andclear
operations. It does not support theadd
oraddAll
operations.
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Description copied from interface:java.util.Map
Returns aSet
view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's ownremove
operation, or through thesetValue
operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via theIterator.remove
,Set.remove
,removeAll
,retainAll
andclear
operations. It does not support theadd
oraddAll
operations.
-
equals
public boolean equals(@Nullable Object object)
Description copied from class:java.lang.Object
Indicates whether some other object is "equal to" this one.The
equals
method implements an equivalence relation on non-null object references:- It is reflexive: for any non-null reference value
x
,x.equals(x)
should returntrue
. - It is symmetric: for any non-null reference values
x
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
The
equals
method for classObject
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returnstrue
if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).Note that it is generally necessary to override the
hashCode
method whenever this method is overridden, so as to maintain the general contract for thehashCode
method, which states that equal objects must have equal hash codes. - It is reflexive: for any non-null reference value
-
hashCode
public int hashCode()
Description copied from class:java.lang.Object
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided byHashMap
.The general contract of
hashCode
is:- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used inequals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals(Object)
method, then calling thehashCode
method on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
Object.equals(java.lang.Object)
method, then calling thehashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class
Object
does return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)- Specified by:
hashCode
in interfaceMap<K,V>
- Overrides:
hashCode
in classObject
- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object)
,System.identityHashCode(java.lang.Object)
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
-
standardPutAll
protected void standardPutAll(Map<? extends K,? extends V> map)
A sensible definition ofputAll(Map)
in terms ofput(Object, Object)
. If you overrideput(Object, Object)
, you may wish to overrideputAll(Map)
to forward to this implementation.- Since:
- 7.0
-
standardRemove
@Beta protected V standardRemove(@Nullable Object key)
A sensible, albeit inefficient, definition ofremove(java.lang.Object)
in terms of theiterator
method ofentrySet()
. If you overrideentrySet()
, you may wish to overrideremove(java.lang.Object)
to forward to this implementation.Alternately, you may wish to override
remove(java.lang.Object)
withkeySet().remove
, assuming that approach would not lead to an infinite loop.- Since:
- 7.0
-
standardClear
protected void standardClear()
A sensible definition ofclear()
in terms of theiterator
method ofentrySet()
. In many cases, you may wish to overrideclear()
to forward to this implementation.- Since:
- 7.0
-
standardContainsKey
@Beta protected boolean standardContainsKey(@Nullable Object key)
A sensible, albeit inefficient, definition ofcontainsKey(java.lang.Object)
in terms of theiterator
method ofentrySet()
. If you overrideentrySet()
, you may wish to overridecontainsKey(java.lang.Object)
to forward to this implementation.- Since:
- 7.0
-
standardContainsValue
protected boolean standardContainsValue(@Nullable Object value)
A sensible definition ofcontainsValue(java.lang.Object)
in terms of theiterator
method ofentrySet()
. If you overrideentrySet()
, you may wish to overridecontainsValue(java.lang.Object)
to forward to this implementation.- Since:
- 7.0
-
standardIsEmpty
protected boolean standardIsEmpty()
A sensible definition ofisEmpty()
in terms of theiterator
method ofentrySet()
. If you overrideentrySet()
, you may wish to overrideisEmpty()
to forward to this implementation.- Since:
- 7.0
-
standardEquals
protected boolean standardEquals(@Nullable Object object)
A sensible definition ofequals(java.lang.Object)
in terms of theequals
method ofentrySet()
. If you overrideentrySet()
, you may wish to overrideequals(java.lang.Object)
to forward to this implementation.- Since:
- 7.0
-
standardHashCode
protected int standardHashCode()
A sensible definition ofhashCode()
in terms of theiterator
method ofentrySet()
. If you overrideentrySet()
, you may wish to overridehashCode()
to forward to this implementation.- Since:
- 7.0
-
standardToString
protected String standardToString()
A sensible definition ofForwardingObject.toString()
in terms of theiterator
method ofentrySet()
. If you overrideentrySet()
, you may wish to overrideForwardingObject.toString()
to forward to this implementation.- Since:
- 7.0
-
-