public class SnacksModule extends AbstractModule {
protected void configure() {
MapBinder<String, Snack> mapbinder
= MapBinder.newMapBinder(binder(), String.class, Snack.class);
mapbinder.addBinding("twix").toInstance(new Twix());
mapbinder.addBinding("snickers").toProvider(SnickersProvider.class);
mapbinder.addBinding("skittles").to(Skittles.class);
}
}
With this binding, a Map<String, Snack> can now be injected:
class SnackMachine {
@Inject
public SnackMachine(Map<String, Snack> snacks) { ... }
}
In addition to binding Map<K, V>, a mapbinder will also bind Map<K,
Provider<V>> for lazy value provision:
class SnackMachine {
@Inject
public SnackMachine(Map<String, Provider<Snack>> snackProviders) { ... }
}
Contributing mapbindings from different modules is supported. For example, it is okay to have
both CandyModule and ChipsModule both create their own MapBinder<String,
Snack>, and to each contribute bindings to the snacks map. When that map is injected, it will
contain entries from both modules.
The map's iteration order is consistent with the binding order. This is convenient when multiple elements are contributed by the same module because that module can order its bindings appropriately. Avoid relying on the iteration order of elements contributed by different modules, since there is no equivalent mechanism to order modules.
The map is unmodifiable. Elements can only be added to the map by configuring the MapBinder. Elements can never be removed from the map.
Values are resolved at map injection time. If a value is bound to a provider, that provider's get method will be called each time the map is injected (unless the binding is also scoped, or a map of providers is injected).
Annotations are used to create different maps of the same key/value type. Each distinct annotation gets its own independent map.
Keys must be distinct. If the same key is bound more than once, map injection
will fail. However, use permitDuplicates() in order to allow duplicate keys; extra
bindings to Map<K, Set<V>> and Map<K, Set<Provider<V>> will be added.
Keys must be non-null. addBinding(null) will throw an unchecked
exception.
Values must be non-null to use map injection. If any value is null, map injection will fail (although injecting a map of providers will not).
| Modifier and Type | Field and Description |
|---|---|
| private final RealMapBinder |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public LinkedBindingBuilder | |
| public boolean | equals(Object
the reference object with which to compare. obj)Overrides java. |
| public int | |
| public static <K, V> MapBinder | newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType)
Returns a new mapbinder that collects entries of |
| public static <K, V> MapBinder | newMapBinder(Binder binder, Class<K> keyType, Class<V> valueType)
Returns a new mapbinder that collects entries of |
| public static <K, V> MapBinder | newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, Annotation annotation)
Returns a new mapbinder that collects entries of |
| public static <K, V> MapBinder | newMapBinder(Binder binder, Class<K> keyType, Class<V> valueType, Annotation annotation)
Returns a new mapbinder that collects entries of |
| public static <K, V> MapBinder | newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, Class<? extends Annotation> annotationType)
Returns a new mapbinder that collects entries of |
| public static <K, V> MapBinder | newMapBinder(Binder binder, Class<K> keyType, Class<V> valueType, Class<? extends Annotation> annotationType)
Returns a new mapbinder that collects entries of |
| public MapBinder |
| delegate | back to summary |
|---|---|
| private final RealMapBinder<K, V> delegate | |
| MapBinder | back to summary |
|---|---|
| private MapBinder(RealMapBinder<K, V> delegate) | |
| addBinding | back to summary |
|---|---|
| public LinkedBindingBuilder Returns a binding builder used to add a new entry in the map. Each key must be distinct (and non-null). Bound providers will be evaluated each time the map is injected. It is an error to call this method without also calling one of the Scoping elements independently is supported. Use the | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| newMapBinder | back to summary |
|---|---|
| public static <K, V> MapBinder Returns a new mapbinder that collects entries of | |
| newMapBinder | back to summary |
|---|---|
| public static <K, V> MapBinder Returns a new mapbinder that collects entries of | |
| newMapBinder | back to summary |
|---|---|
| public static <K, V> MapBinder Returns a new mapbinder that collects entries of | |
| newMapBinder | back to summary |
|---|---|
| public static <K, V> MapBinder Returns a new mapbinder that collects entries of | |
| newMapBinder | back to summary |
|---|---|
| public static <K, V> MapBinder Returns a new mapbinder that collects entries of | |
| newMapBinder | back to summary |
|---|---|
| public static <K, V> MapBinder Returns a new mapbinder that collects entries of | |
| permitDuplicates | back to summary |
|---|---|
| public MapBinder Configures the When multiple equal keys are bound, the value that gets included in the map is arbitrary. In addition to the When multiple modules contribute elements to the map, this configuration option impacts all of them.
| |