Package com.google.common.collect
Class MutableClassToInstanceMap<B>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.collect.ForwardingMap<Class<? extends B>,B>
-
- com.google.common.collect.MutableClassToInstanceMap<B>
-
- All Implemented Interfaces:
ClassToInstanceMap<B>
,Serializable
,Map<Class<? extends B>,B>
@GwtIncompatible public final class MutableClassToInstanceMap<B> extends ForwardingMap<Class<? extends B>,B> implements ClassToInstanceMap<B>, Serializable
A mutable class-to-instance map backed by an arbitrary user-provided map. See alsoImmutableClassToInstanceMap
.See the Guava User Guide article on
ClassToInstanceMap
.This implementation does support null values, despite how it is annotated; see discussion at
ClassToInstanceMap
.- Since:
- 2.0
- Author:
- Kevin Bourrillion
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.common.collect.ForwardingMap
ForwardingMap.StandardEntrySet, ForwardingMap.StandardKeySet, ForwardingMap.StandardValues
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <B> MutableClassToInstanceMap<B>
create()
Returns a newMutableClassToInstanceMap
instance backed by aHashMap
using the default initial capacity and load factor.static <B> MutableClassToInstanceMap<B>
create(Map<Class<? extends B>,B> backingMap)
Returns a newMutableClassToInstanceMap
instance backed by a given emptybackingMap
.protected Map<Class<? extends B>,B>
delegate()
Returns the backing delegate instance that methods are forwarded to.Set<Map.Entry<Class<? extends B>,B>>
entrySet()
Returns aSet
view of the mappings contained in this map.<T extends B>
TgetInstance(Class<T> type)
Returns the value the specified class is mapped to, ornull
if no entry for this class is present.B
put(Class<? extends B> key, B value)
Associates the specified value with the specified key in this map (optional operation).void
putAll(Map<? extends Class<? extends B>,? extends B> map)
Copies all of the mappings from the specified map to this map (optional operation).<T extends B>
TputInstance(Class<T> type, T value)
Maps the specified class to the specified value.-
Methods inherited from class com.google.common.collect.ForwardingMap
clear, containsKey, containsValue, equals, get, hashCode, isEmpty, keySet, remove, size, standardClear, standardContainsKey, standardContainsValue, standardEquals, standardHashCode, standardIsEmpty, standardPutAll, standardRemove, standardToString, 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
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
-
-
-
Method Detail
-
create
public static <B> MutableClassToInstanceMap<B> create()
Returns a newMutableClassToInstanceMap
instance backed by aHashMap
using the default initial capacity and load factor.
-
create
public static <B> MutableClassToInstanceMap<B> create(Map<Class<? extends B>,B> backingMap)
Returns a newMutableClassToInstanceMap
instance backed by a given emptybackingMap
. The caller surrenders control of the backing map, and thus should not allow any direct references to it to remain accessible.
-
delegate
protected Map<Class<? extends B>,B> 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 classForwardingMap<Class<? extends B>,B>
-
entrySet
public Set<Map.Entry<Class<? extends B>,B>> 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.
-
put
@CanIgnoreReturnValue @CheckForNull public B put(Class<? extends B> key, B 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<Class<? extends B>,B>
- Overrides:
put
in classForwardingMap<Class<? extends B>,B>
- 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 Class<? extends B>,? extends B> 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.
-
putInstance
@CanIgnoreReturnValue @CheckForNull public <T extends B> T putInstance(Class<T> type, T value)
Description copied from interface:ClassToInstanceMap
Maps the specified class to the specified value. Does not associate this value with any of the class's supertypes.- Specified by:
putInstance
in interfaceClassToInstanceMap<B>
- Returns:
- the value previously associated with this class (possibly
null
), ornull
if there was no previous entry.
-
getInstance
@CheckForNull public <T extends B> T getInstance(Class<T> type)
Description copied from interface:ClassToInstanceMap
Returns the value the specified class is mapped to, ornull
if no entry for this class is present. This will only return a value that was bound to this specific class, not a value that may have been bound to a subtype.- Specified by:
getInstance
in interfaceClassToInstanceMap<B>
-
-