K
- type of key for mapV
- type of value for mappublic final class GinMapBinder<K,V> extends Object
Example usage:
interface X {}; class X1Impl implements X {}; class X2Impl implements X {}; class X3Provider implements Provider<X> {}; GinMapBinder<String, X> mapBinder = GinMapBinder.newMapBinder(binder(), String.class, X.class); mapBinder.addBinding("id1").to(X1Impl.class); mapBinder.addBinding("id2").to(X2Impl.class); mapBinder.addBinding("id3").toProvider(X3Provider.class);
GIN supports instance binding for only limited set of types. To overcome this limitation,
GinMapBinder provides addBinding(Class)
method so bindings can be added via a key
provider class that will instantiate the actual key during runtime. This alternative approach
is needed to used for all key types that cannot be bound via
GinConstantBindingBuilder
:
class Place { public Place(String key) { ... } } class HomePlaceProvider implements Provider<Place> { public Place get() { return new Place("home"); } } class AboutPlaceProvider implements Provider<Place> { public Place get() { return new Place("about"); } } GinMapBinder<Place, X> mapBinder = GinMapBinder.newMapBinder(binder(), Place.class, X.class); mapBinder.addBinding(HomePlaceProvider.class).to(XImpl1.class); mapBinder.addBinding(AboutPlaceProvider.class).to(XImpl2.class);
Modifier and Type | Method and Description |
---|---|
GinLinkedBindingBuilder<V> |
addBinding(Class<? extends javax.inject.Provider<? extends K>> keyProvider)
Returns a binding builder used to add a new entry in the map using a key provider.
|
GinLinkedBindingBuilder<V> |
addBinding(K key)
Returns a binding builder used to add a new entry in the map.
|
GinLinkedBindingBuilder<V> |
addBinding(TypeLiteral<? extends javax.inject.Provider<? extends K>> keyProvider)
Returns a binding builder used to add a new entry in the map using a key provider.
|
static <K,V> GinMapBinder<K,V> |
newMapBinder(GinBinder binder,
Class<K> keyType,
Class<V> valueType)
Returns a new mapbinder that collects entries of
keyType /valueType in a
Map that is itself bound with no binding annotation. |
static <K,V> GinMapBinder<K,V> |
newMapBinder(GinBinder binder,
Class<K> keyType,
Class<V> valueType,
Annotation annotation)
Returns a new mapbinder that collects entries of
keyType /valueType in a
Map that is itself bound with annotation . |
static <K,V> GinMapBinder<K,V> |
newMapBinder(GinBinder binder,
Class<K> keyType,
Class<V> valueType,
Class<? extends Annotation> annotationType)
Returns a new mapbinder that collects entries of
keyType /valueType in a
Map that is itself bound with annotationType . |
static <K,V> GinMapBinder<K,V> |
newMapBinder(GinBinder binder,
TypeLiteral<K> keyType,
TypeLiteral<V> valueType)
Returns a new mapbinder that collects entries of
keyType /valueType in a
Map that is itself bound with no binding annotation. |
static <K,V> GinMapBinder<K,V> |
newMapBinder(GinBinder binder,
TypeLiteral<K> keyType,
TypeLiteral<V> valueType,
Annotation annotation)
Returns a new mapbinder that collects entries of
keyType /valueType in a
Map that is itself bound with annotation . |
static <K,V> GinMapBinder<K,V> |
newMapBinder(GinBinder binder,
TypeLiteral<K> keyType,
TypeLiteral<V> valueType,
Class<? extends Annotation> annotationType)
Returns a new mapbinder that collects entries of
keyType /valueType in a
Map that is itself bound with annotationType . |
GinMapBinder<K,V> |
permitDuplicates()
Configures the
MapBinder to handle duplicate entries. |
public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType)
keyType
/valueType
in a
Map
that is itself bound with no binding annotation.public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder, Class<K> keyType, Class<V> valueType)
keyType
/valueType
in a
Map
that is itself bound with no binding annotation.public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, Annotation annotation)
keyType
/valueType
in a
Map
that is itself bound with annotation
.public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder, Class<K> keyType, Class<V> valueType, Annotation annotation)
keyType
/valueType
in a
Map
that is itself bound with annotation
.public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType, Class<? extends Annotation> annotationType)
keyType
/valueType
in a
Map
that is itself bound with annotationType
.public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder, Class<K> keyType, Class<V> valueType, Class<? extends Annotation> annotationType)
keyType
/valueType
in a
Map
that is itself bound with annotationType
.public GinMapBinder<K,V> permitDuplicates()
MapBinder
to handle duplicate entries.
When multiple equal keys are bound, the value that gets included in the map is arbitrary.
In addition to the Map<K, V>
and Map<K, Provider<V>>
maps that are normally
bound, a Map<K, Set<V>>
and Map<K, Set<Provider<V>>>
are also bound,
which contain all values bound to each key.
When multiple modules contribute elements to the map, this configuration option impacts all of them.
public GinLinkedBindingBuilder<V> addBinding(K key)
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.
public GinLinkedBindingBuilder<V> addBinding(Class<? extends javax.inject.Provider<? extends K>> keyProvider)
This API is not compatible with Guice however it is provided as GIN has limitation to bind
'instances'. For that reason for all key types that are not defined in
GinConstantBindingBuilder
needs to use a provider
class for each key together with this method.
addBinding(Object)
public GinLinkedBindingBuilder<V> addBinding(TypeLiteral<? extends javax.inject.Provider<? extends K>> keyProvider)
This API is not compatible with Guice however it is provided as GIN has limitation to bind
'instances'. For that reason for all key types that are not defined in
GinConstantBindingBuilder
needs to use a provider
class for each key together with this method.
addBinding(Object)
Copyright © 2008–2018. All rights reserved.