Class ForwardingMap<K extends @Nullable Object,V extends @Nullable Object>
- 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 extends @Nullable Object,V extends @Nullable Object> 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
ForwardingMapforward 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 overrideputAllas well, either providing your own implementation, or delegating to the providedstandardPutAllmethod.defaultmethod warning: This class does not forward calls todefaultmethods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on theForwardingMap.Each of the
standardmethods, 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 aSortedMapwhose comparator is not consistent withequals.The
standardmethods 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 classForwardingMap.StandardEntrySetA 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 classForwardingMap.StandardKeySetA 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 classForwardingMap.StandardValuesA 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 protectedForwardingMap()Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all of the mappings from this map (optional operation).booleancontainsKey(Object key)Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value)Returnstrueif 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 aSetview of the mappings contained in this map.booleanequals(Object object)Indicates whether some other object is "equal to" this one.Vget(Object key)Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.inthashCode()Returns a hash code value for the object.booleanisEmpty()Returnstrueif this map contains no key-value mappings.Set<K>keySet()Returns aSetview of the keys contained in this map.Vput(K key, V value)Associates the specified value with the specified key in this map (optional operation).voidputAll(Map<? extends K,? extends V> map)Copies all of the mappings from the specified map to this map (optional operation).Vremove(Object key)Removes the mapping for a key from this map if it is present (optional operation).intsize()Returns the number of key-value mappings in this map.protected voidstandardClear()protected booleanstandardContainsKey(Object key)A sensible, albeit inefficient, definition ofcontainsKey(java.lang.Object)in terms of theiteratormethod ofentrySet().protected booleanstandardContainsValue(Object value)A sensible definition ofcontainsValue(java.lang.Object)in terms of theiteratormethod ofentrySet().protected booleanstandardEquals(Object object)protected intstandardHashCode()protected booleanstandardIsEmpty()protected voidstandardPutAll(Map<? extends K,? extends V> map)A sensible definition ofputAll(Map)in terms ofput(Object, Object).protected VstandardRemove(Object key)A sensible, albeit inefficient, definition ofremove(java.lang.Object)in terms of theiteratormethod ofentrySet().protected StringstandardToString()Collection<V>values()Returns aCollectionview 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:ForwardingObjectReturns 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:
delegatein classForwardingObject
-
size
public int size()
Description copied from interface:java.util.MapReturns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.
-
isEmpty
public boolean isEmpty()
Description copied from interface:java.util.MapReturnstrueif this map contains no key-value mappings.
-
remove
@CanIgnoreReturnValue @CheckForNull public V remove(@CheckForNull Object key)
Description copied from interface:java.util.MapRemoves the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from keykto valuevsuch 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
nullif the map contained no mapping for the key.If this map permits null values, then a return value of
nulldoes 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.MapRemoves all of the mappings from this map (optional operation). The map will be empty after this call returns.
-
containsKey
public boolean containsKey(@CheckForNull Object key)
Description copied from interface:java.util.MapReturnstrueif this map contains a mapping for the specified key. More formally, returnstrueif and only if this map contains a mapping for a keyksuch thatObjects.equals(key, k). (There can be at most one such mapping.)
-
containsValue
public boolean containsValue(@CheckForNull Object value)
Description copied from interface:java.util.MapReturnstrueif this map maps one or more keys to the specified value. More formally, returnstrueif and only if this map contains at least one mapping to a valuevsuch thatObjects.equals(value, v). This operation will probably require time linear in the map size for most implementations of theMapinterface.
-
get
@CheckForNull public V get(@CheckForNull Object key)
Description copied from interface:java.util.MapReturns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.More formally, if this map contains a mapping from a key
kto a valuevsuch 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
nulldoes not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull. ThecontainsKeyoperation may be used to distinguish these two cases.
-
put
@CanIgnoreReturnValue @CheckForNull public V put(K key, V value)
Description copied from interface:java.util.MapAssociates 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 mapmis said to contain a mapping for a keykif and only ifm.containsKey(k)would returntrue.)- Specified by:
putin interfaceMap<K extends @Nullable Object,V extends @Nullable Object>- 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, ornullif there was no mapping forkey. (Anullreturn can also indicate that the map previously associatednullwithkey, if the implementation supportsnullvalues.)
-
putAll
public void putAll(Map<? extends K,? extends V> map)
Description copied from interface:java.util.MapCopies 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 keykto valuevin 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.MapReturns aSetview 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 ownremoveoperation), 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, andclearoperations. It does not support theaddoraddAlloperations.
-
values
public Collection<V> values()
Description copied from interface:java.util.MapReturns aCollectionview 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 ownremoveoperation), 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,retainAllandclearoperations. It does not support theaddoraddAlloperations.
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Description copied from interface:java.util.MapReturns aSetview 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 ownremoveoperation, or through thesetValueoperation 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,retainAllandclearoperations. It does not support theaddoraddAlloperations.
-
equals
public boolean equals(@CheckForNull Object object)
Description copied from class:java.lang.ObjectIndicates whether some other object is "equal to" this one.The
equalsmethod 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
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
The
equalsmethod for classObjectimplements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesxandy, this method returnstrueif and only ifxandyrefer to the same object (x == yhas the valuetrue).Note that it is generally necessary to override the
hashCodemethod whenever this method is overridden, so as to maintain the general contract for thehashCodemethod, which states that equal objects must have equal hash codes.- Specified by:
equalsin interfaceMap<K extends @Nullable Object,V extends @Nullable Object>- Overrides:
equalsin classObject- Parameters:
object- the reference object with which to compare.- Returns:
trueif this object is the same as the obj argument;falseotherwise.- See Also:
Object.hashCode(),HashMap
- It is reflexive: for any non-null reference value
-
hashCode
public int hashCode()
Description copied from class:java.lang.ObjectReturns 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
hashCodeis:- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons 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 thehashCodemethod 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 thehashCodemethod 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
Objectdoes 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.) - 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 @CheckForNull protected V standardRemove(@CheckForNull Object key)
A sensible, albeit inefficient, definition ofremove(java.lang.Object)in terms of theiteratormethod 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 theiteratormethod ofentrySet(). In many cases, you may wish to overrideclear()to forward to this implementation.- Since:
- 7.0
-
standardContainsKey
@Beta protected boolean standardContainsKey(@CheckForNull Object key)
A sensible, albeit inefficient, definition ofcontainsKey(java.lang.Object)in terms of theiteratormethod ofentrySet(). If you overrideentrySet(), you may wish to overridecontainsKey(java.lang.Object)to forward to this implementation.- Since:
- 7.0
-
standardContainsValue
protected boolean standardContainsValue(@CheckForNull Object value)
A sensible definition ofcontainsValue(java.lang.Object)in terms of theiteratormethod 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 theiteratormethod ofentrySet(). If you overrideentrySet(), you may wish to overrideisEmpty()to forward to this implementation.- Since:
- 7.0
-
standardEquals
protected boolean standardEquals(@CheckForNull Object object)
A sensible definition ofequals(java.lang.Object)in terms of theequalsmethod 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 theiteratormethod 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 theiteratormethod ofentrySet(). If you overrideentrySet(), you may wish to overrideForwardingObject.toString()to forward to this implementation.- Since:
- 7.0
-
-