Class MapBinder<K,V>
- java.lang.Object
-
- org.elasticsearch.common.inject.multibindings.MapBinder<K,V>
-
- Direct Known Subclasses:
MapBinder.RealMapBinder
public abstract class MapBinder<K,V> extends java.lang.Object
An API to bind multiple map entries separately, only to later inject them as a complete map. MapBinder is intended for use in your application's module: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 bindMap<K, Provider<V>>
for lazy value provision:class SnackMachine { @Inject public SnackMachine(Map<String, Provider<Snack>> snackProviders) { ... } }
Creating mapbindings from different modules is supported. For example, it is okay to have both
CandyModule
andChipsModule
both create their ownMapBinder<String, Snack>
, and to each contribute bindings to the snacks map. When that map is injected, it will contain entries from both modules.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.
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).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MapBinder.RealMapBinder<K,V>
The actual mapbinder plays several roles:
-
Method Summary
Modifier and Type Method Description abstract LinkedBindingBuilder<V>
addBinding(K key)
Returns a binding builder used to add a new entry in the map.static <K,V>
MapBinder<K,V>newMapBinder(Binder binder, java.lang.Class<K> keyType, java.lang.Class<V> valueType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound with no binding annotation.static <K,V>
MapBinder<K,V>newMapBinder(Binder binder, java.lang.Class<K> keyType, java.lang.Class<V> valueType, java.lang.annotation.Annotation annotation)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotation
.static <K,V>
MapBinder<K,V>newMapBinder(Binder binder, java.lang.Class<K> keyType, java.lang.Class<V> valueType, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotationType
.static <K,V>
MapBinder<K,V>newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound with no binding annotation.static <K,V>
MapBinder<K,V>newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, java.lang.annotation.Annotation annotation)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotation
.static <K,V>
MapBinder<K,V>newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotationType
.
-
-
-
Method Detail
-
newMapBinder
public static <K,V> MapBinder<K,V> newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound with no binding annotation.
-
newMapBinder
public static <K,V> MapBinder<K,V> newMapBinder(Binder binder, java.lang.Class<K> keyType, java.lang.Class<V> valueType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound with no binding annotation.
-
newMapBinder
public static <K,V> MapBinder<K,V> newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, java.lang.annotation.Annotation annotation)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotation
.
-
newMapBinder
public static <K,V> MapBinder<K,V> newMapBinder(Binder binder, java.lang.Class<K> keyType, java.lang.Class<V> valueType, java.lang.annotation.Annotation annotation)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotation
.
-
newMapBinder
public static <K,V> MapBinder<K,V> newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotationType
.
-
newMapBinder
public static <K,V> MapBinder<K,V> newMapBinder(Binder binder, java.lang.Class<K> keyType, java.lang.Class<V> valueType, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Returns a new mapbinder that collects entries ofkeyType
/valueType
in aMap
that is itself bound withannotationType
.
-
addBinding
public abstract LinkedBindingBuilder<V> addBinding(K key)
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
to
methods on the returned binding builder.Scoping elements independently is supported. Use the
in
method to specify a binding scope.
-
-