com.google.gwt.inject.client.multibindings
Class GinMapBinder<K,V>

java.lang.Object
  extended by com.google.gwt.inject.client.multibindings.GinMapBinder<K,V>
Type Parameters:
K - type of key for map
V - type of value for map

public final class GinMapBinder<K,V>
extends java.lang.Object

A utility that mimics the behavior and API of Guice MapBinder for GIN.

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);
 


Method Summary
 GinLinkedBindingBuilder<V> addBinding(java.lang.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, java.lang.Class<K> keyType, java.lang.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, java.lang.Class<K> keyType, java.lang.Class<V> valueType, java.lang.annotation.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, 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 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, java.lang.annotation.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, java.lang.Class<? extends java.lang.annotation.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newMapBinder

public 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.


newMapBinder

public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder,
                                                   java.lang.Class<K> keyType,
                                                   java.lang.Class<V> valueType)
Returns a new mapbinder that collects entries of keyType/valueType in a Map that is itself bound with no binding annotation.


newMapBinder

public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder,
                                                   TypeLiteral<K> keyType,
                                                   TypeLiteral<V> valueType,
                                                   java.lang.annotation.Annotation annotation)
Returns a new mapbinder that collects entries of keyType/valueType in a Map that is itself bound with annotation.


newMapBinder

public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder,
                                                   java.lang.Class<K> keyType,
                                                   java.lang.Class<V> valueType,
                                                   java.lang.annotation.Annotation annotation)
Returns a new mapbinder that collects entries of keyType/valueType in a Map that is itself bound with annotation.


newMapBinder

public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder binder,
                                                   TypeLiteral<K> keyType,
                                                   TypeLiteral<V> valueType,
                                                   java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Returns a new mapbinder that collects entries of keyType/valueType in a Map that is itself bound with annotationType.


newMapBinder

public static <K,V> GinMapBinder<K,V> newMapBinder(GinBinder 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 of keyType/valueType in a Map that is itself bound with annotationType.


permitDuplicates

public GinMapBinder<K,V> permitDuplicates()
Configures the 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.

Returns:
this map binder

addBinding

public GinLinkedBindingBuilder<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.


addBinding

public GinLinkedBindingBuilder<V> addBinding(java.lang.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.

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.

See Also:
addBinding(Object)

addBinding

public 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.

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.

See Also:
addBinding(Object)