Top Description Methods
com.google.inject.multibindings

public Interface MapBinderBinding<T>

Known Direct Implementers
com.google.inject.internal.RealMapBinder.ExtensionRealMapProvider
Type Parameters
<T>
The fully qualified type of the map, including Map. For example: MapBinderBinding<Map<String, Snack>>
Imports
com.google.inject.Binding, .Key, .TypeLiteral, com.google.inject.spi.Element, .Elements, java.util.List, .Map, .Set

A binding for a MapBinder.

Although MapBinders may be injected through a variety of generic types (Map<K, V>, Map<K, ? extends V>, Map<K, Provider<V>>, Map<K, Set<V>>, Map<K, Set<Provider<V>>, and even Set<Map.Entry<K, Provider<V>>), a MapBinderBinding exists only on the Binding associated with the Map<K, V> key. Injectable map types can be discovered using getMapKey (which will return the Map<K, V> key), orgetAlternateMapKeys (which will return the other keys that can inject this data). Other bindings can be validated to be derived from this MapBinderBinding using containsElement(Element).

Author
[email protected] (Sam Berlin)
Since
3.0

Method Summary

Modifier and TypeMethod and Description
public boolean
containsElement(Element element)

Returns true if this MapBinder contains the given Element in order to build the map or uses the given Element in order to support building and injecting the map.

public Set<Key<?>>
getAlternateMapKeys()

Returns the keys of other bindings that represent this map.

public List<Map.Entry<?, Binding<?>>>
getEntries()

Returns all entries in the Map.

public List<Map.Entry<?, Binding<?>>>
getEntries(Iterable<? extends Element> elements)

Similar to getEntries(), but can be used on a MapBinderBinding retrieved from Elements#getElements.

public TypeLiteral<?>
getKeyTypeLiteral()

Returns the TypeLiteral describing the keys of the map.

public Key<T>
getMapKey()

Returns the Key for the map.

public TypeLiteral<?>
getValueTypeLiteral()

Returns the TypeLiteral describing the values of the map.

public boolean
permitsDuplicates()

Returns true if the MapBinder permits duplicates.

Method Detail

containsElementback to summary
public boolean containsElement(Element element)

Returns true if this MapBinder contains the given Element in order to build the map or uses the given Element in order to support building and injecting the map. This will work for MapBinderBindings retrieved from an injector and Elements#getElements. Usually this is only necessary if you are working with elements retrieved from modules (without an Injector), otherwise getEntries and permitsDuplicates are better options.

If you need to introspect the details of the map, such as the keys, values or if it permits duplicates, it is necessary to pass the elements through an Injector and use getEntries() and permitsDuplicates().

getAlternateMapKeysback to summary
public Set<Key<?>> getAlternateMapKeys()

Returns the keys of other bindings that represent this map. This will return an entry for Map<K, com.google.inject.Provider<V>>, Map<K, jakarta.inject.Provider<V>>, Map<K, Set<com.google.inject.Provider<V>>>, Map<K, Set<jakarta.inject.Provider<V>>>, Map<K, Collection<com.google.inject.Provider<V>>>, Map<K, Collection<jakarta.inject.Provider<V>>>, Map<K, Set<V>, and Map<K, ? extends V>.

Since
4.2.3
getEntriesback to summary
public List<Map.Entry<?, Binding<?>>> getEntries()

Returns all entries in the Map. The returned list of Map.Entries contains the key and a binding to the value. Duplicate keys or values will exist as separate Map.Entries in the returned list. This is only supported on bindings returned from an injector. This will throw UnsupportedOperationException if it is called on an element retrieved from Elements#getElements.

The elements will always match the type Map's generic type. For example, if getMapKey returns a key of Map<String, Snack>, then this will always return a list of type List<Map.Entry<String, Binding<Snack>>>.

getEntriesback to summary
public List<Map.Entry<?, Binding<?>>> getEntries(Iterable<? extends Element> elements)

Similar to getEntries(), but can be used on a MapBinderBinding retrieved from Elements#getElements.

One way to use this is to pass in the results of Elements#getElements as the elements parameter.

This differs from getEntries() in that it will return duplicates if they are present in the elements passed in. This does not run the normal Guice de-duplication that getEntries() does.

Exceptions
IllegalArgumentException:
if the provided elements contain partial map entries. If the elements come from Elements#getElements on a module with a MapBinder, there will be a 1:1 relationship and no exception will be thrown.
Since
4.2
getKeyTypeLiteralback to summary
public TypeLiteral<?> getKeyTypeLiteral()

Returns the TypeLiteral describing the keys of the map.

The TypeLiteral will always match the type Map's generic type. For example, if getMapKey returns a key of Map<String, Snack>, then this will always return a TypeLiteral<String>.

getMapKeyback to summary
public Key<T> getMapKey()

Returns the Key for the map.

getValueTypeLiteralback to summary
public TypeLiteral<?> getValueTypeLiteral()

Returns the TypeLiteral describing the values of the map.

The TypeLiteral will always match the type Map's generic type. For example, if getMapKey returns a key of Map<String, Snack>, then this will always return a TypeLiteral<Snack>.

permitsDuplicatesback to summary
public boolean permitsDuplicates()

Returns true if the MapBinder permits duplicates. This is only supported on bindings returned from an injector. This will throw UnsupportedOperationException if it is called on a MapBinderBinding retrieved from Elements#getElements.