Top Description Inners Fields Constructors Methods
com.google.common.collect

public final Class ImmutableClassToInstanceMap<B>

extends ForwardingMap<Class<? extends @NonNull B>, B>
implements ClassToInstanceMap<B>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.collect.ClassToInstanceMap, java.util.Map
Annotations
@Immutable
containerOf:B
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

A ClassToInstanceMap whose contents will never change, with many other important properties detailed at ImmutableCollection.
Author
Kevin Bourrillion
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ImmutableClassToInstanceMap.Builder<B>

A builder for creating immutable class-to-instance maps.

Field Summary

Modifier and TypeField and Description
private final ImmutableMap<Class<? extends B>, B>
private static final ImmutableClassToInstanceMap<Object>

Constructor Summary

AccessConstructor and Description
private
ImmutableClassToInstanceMap(ImmutableMap<Class<? extends B>, B> delegate)

Method Summary

Modifier and TypeMethod and Description
public static <B> ImmutableClassToInstanceMap.Builder<B>
builder()

Returns a new builder.

public static <B, S extends B> ImmutableClassToInstanceMap<B>
copyOf(Map<? extends Class<? extends S>, ? extends S>
a Map from which entries are drawn, must be non-null
map
)

Hides java.util.Map.copyOf.

Returns an immutable map containing the same entries as map.
protected Map<Class<? extends B>, B>
delegate()

Implements abstract com.google.common.collect.ForwardingMap.delegate.

Returns the backing delegate instance that methods are forwarded to.
public <T extends B> T
getInstance(Class<T> type)

Implements com.google.common.collect.ClassToInstanceMap.getInstance.

Returns the value the specified class is mapped to, or null if no entry for this class is present.
public static <B> ImmutableClassToInstanceMap<B>
of()

Hides java.util.Map.of.

Returns an empty ImmutableClassToInstanceMap.
public static <B, T extends B> ImmutableClassToInstanceMap<B>
of(Class<T>
the mapping's key
type
,
T
the mapping's value
value
)

Hides java.util.Map.of.

Returns an ImmutableClassToInstanceMap containing a single entry.
public <T extends B> T
putInstance(Class<T> type, T value)

Implements com.google.common.collect.ClassToInstanceMap.putInstance.

Deprecated Unsupported operation.
Guaranteed to throw an exception and leave the map unmodified.
pack-priv Object
Inherited from com.google.common.collect.ForwardingMap:
clearcontainsKeycontainsValueentrySetequalsgethashCodeisEmptykeySetputputAllremovesizestandardClearstandardContainsKeystandardContainsValuestandardEqualsstandardHashCodestandardIsEmptystandardPutAllstandardRemovestandardToStringvalues

Field Detail

delegateback to summary
private final ImmutableMap<Class<? extends B>, B> delegate
EMPTYback to summary
private static final ImmutableClassToInstanceMap<Object> EMPTY

Constructor Detail

ImmutableClassToInstanceMapback to summary
private ImmutableClassToInstanceMap(ImmutableMap<Class<? extends B>, B> delegate)

Method Detail

builderback to summary
public static <B> ImmutableClassToInstanceMap.Builder<B> builder()

Returns a new builder. The generated builder is equivalent to the builder created by the Builder constructor.

copyOfback to summary
public static <B, S extends B> ImmutableClassToInstanceMap<B> copyOf(Map<? extends Class<? extends S>, ? extends S> map)

Hides java.util.Map.copyOf.

Returns an immutable map containing the same entries as map. If map somehow contains entries with duplicate keys (for example, if it is a SortedMap whose comparator is not consistent with equals), the results of this method are undefined.

Note

Despite what the method name suggests, if map is an ImmutableClassToInstanceMap, no copy will actually be performed.

Parameters
<B>

Doc from java.util.Map.copyOf. the Map's key type

<S>

Doc from java.util.Map.copyOf. the Map's value type

map:Map<? extends Class<? extends S>, ? extends S>

Doc from java.util.Map.copyOf.

a Map from which entries are drawn, must be non-null

Returns:ImmutableClassToInstanceMap<B>

Doc from java.util.Map.copyOf.

a Map containing the entries of the given Map

Exceptions
NullPointerException:
if any key or value in map is null
ClassCastException:
if any value is not an instance of the type specified by its key
delegateback to summary
protected Map<Class<? extends B>, B> delegate()

Implements abstract com.google.common.collect.ForwardingMap.delegate.

Doc from com.google.common.collect.ForwardingObject.delegate.

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 as ForwardingSet#delegate. Concrete subclasses override this method to supply the instance being decorated.

Annotations
@Override
getInstanceback to summary
public <T extends B> T getInstance(Class<T> type)

Implements com.google.common.collect.ClassToInstanceMap.getInstance.

Doc from com.google.common.collect.ClassToInstanceMap.getInstance.

Returns the value the specified class is mapped to, or null 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.

Annotations
@Override
@SuppressWarnings:unchecked
@CheckForNull
ofback to summary
public static <B> ImmutableClassToInstanceMap<B> of()

Hides java.util.Map.of.

Returns an empty ImmutableClassToInstanceMap.

Performance note: the instance returned is a singleton.

Parameters
<B>

Doc from java.util.Map.of. the Map's key type

Returns:ImmutableClassToInstanceMap<B>

Doc from java.util.Map.of.

an empty Map

Annotations
@SuppressWarnings:unchecked
Since
19.0
ofback to summary
public static <B, T extends B> ImmutableClassToInstanceMap<B> of(Class<T> type, T value)

Hides java.util.Map.of.

Returns an ImmutableClassToInstanceMap containing a single entry.

Parameters
<B>

Doc from java.util.Map.of. the Map's key type

<T>

Doc from java.util.Map.of. the Map's value type

type:Class<T>

Doc from java.util.Map.of.

the mapping's key

value:T

Doc from java.util.Map.of.

the mapping's value

Returns:ImmutableClassToInstanceMap<B>

Doc from java.util.Map.of.

a Map containing the specified mapping

Since
19.0
putInstanceback to summary
public <T extends B> T putInstance(Class<T> type, T value)

Implements com.google.common.collect.ClassToInstanceMap.putInstance.

Deprecated

Unsupported operation.

Guaranteed to throw an exception and leave the map unmodified.

Returns:T

Doc from com.google.common.collect.ClassToInstanceMap.putInstance.

the value previously associated with this class (possibly null), or null if there was no previous entry.

Annotations
@CanIgnoreReturnValue
@Deprecated
@Override
@DoNotCall:Always throws UnsupportedOperationException
@CheckForNull
Exceptions
UnsupportedOperationException:
always
readResolveback to summary
pack-priv Object readResolve()
com.google.common.collect back to summary

public final Class ImmutableClassToInstanceMap.Builder<B>

extends Object
Class Inheritance
  • java.lang.Object
  • com.google.common.collect.ImmutableClassToInstanceMap.Builder

A builder for creating immutable class-to-instance maps. Example:
static final ImmutableClassToInstanceMap<Handler> HANDLERS =
    new ImmutableClassToInstanceMap.Builder<Handler>()
        .put(FooHandler.class, new FooHandler())
        .put(BarHandler.class, new SubBarHandler())
        .put(Handler.class, new QuuxHandler())
        .build();

After invoking build() it is still possible to add more entries and build again. Thus each map generated by this builder will be a superset of any map generated before it.

Since
2.0

Field Summary

Modifier and TypeField and Description
private final ImmutableMap.Builder<Class<? extends B>, B>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public ImmutableClassToInstanceMap<B>
build()

Returns a new immutable class-to-instance map containing the entries provided to this builder.

private static <T> T
cast(Class<T> type, Object value)

public <T extends B> ImmutableClassToInstanceMap.Builder<B>
put(Class<T> key, T value)

Associates key with value in the built map.

public <T extends B> ImmutableClassToInstanceMap.Builder<B>
putAll(Map<? extends Class<? extends T>, ? extends T> map)

Associates all of map's keys and values in the built map.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mapBuilderback to summary
private final ImmutableMap.Builder<Class<? extends B>, B> mapBuilder

Constructor Detail

Builderback to summary
public Builder()

Method Detail

buildback to summary
public ImmutableClassToInstanceMap<B> build()

Returns a new immutable class-to-instance map containing the entries provided to this builder.

Exceptions
IllegalArgumentException:
if duplicate keys were added
castback to summary
private static <T> T cast(Class<T> type, Object value)
putback to summary
public <T extends B> ImmutableClassToInstanceMap.Builder<B> put(Class<T> key, T value)

Associates key with value in the built map. Duplicate keys are not allowed, and will cause build to fail.

Annotations
@CanIgnoreReturnValue
putAllback to summary
public <T extends B> ImmutableClassToInstanceMap.Builder<B> putAll(Map<? extends Class<? extends T>, ? extends T> map)

Associates all of map's keys and values in the built map. Duplicate keys are not allowed, and will cause build to fail.

Annotations
@CanIgnoreReturnValue
Exceptions
NullPointerException:
if any key or value in map is null
ClassCastException:
if any value is not an instance of the type specified by its key