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
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()
boolean
containsKey(@Nullable Object key)
boolean
containsValue(@Nullable Object value)
protected abstract Map<K,V>
delegate()
Returns the backing delegate instance that methods are forwarded to.Set<Map.Entry<K,V>>
entrySet()
boolean
equals(@Nullable Object object)
@Nullable V
get(@Nullable Object key)
int
hashCode()
boolean
isEmpty()
Set<K>
keySet()
@Nullable V
put(K key, V value)
void
putAll(Map<? extends K,? extends V> map)
@Nullable V
remove(@Nullable Object key)
int
size()
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 @Nullable 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()
-
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()
-
isEmpty
public boolean isEmpty()
-
remove
@CanIgnoreReturnValue public @Nullable V remove(@Nullable Object key)
-
clear
public void clear()
-
containsKey
public boolean containsKey(@Nullable Object key)
-
containsValue
public boolean containsValue(@Nullable Object value)
-
put
@CanIgnoreReturnValue public @Nullable V put(K key, V value)
-
values
public Collection<V> values()
-
hashCode
public int hashCode()
-
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
protected @Nullable 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
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
-
-