Top Description Fields Constructors Methods
com.google.inject.multibindings

public Class Multibinder<T>

extends Object
Class Inheritance
Static Imports
com.google.inject.internal.RealMultibinder.newRealSetBinder

An API to bind multiple values separately, only to later inject them as a complete collection. Multibinder is intended for use in your application's module:

public class SnacksModule extends AbstractModule {
  protected void configure() {
    Multibinder<Snack> multibinder
        = Multibinder.newSetBinder(binder(), Snack.class);
    multibinder.addBinding().toInstance(new Twix());
    multibinder.addBinding().toProvider(SnickersProvider.class);
    multibinder.addBinding().to(Skittles.class);
  }
}

With this binding, a Set<Snack> can now be injected:


class SnackMachine {
  @Inject
  public SnackMachine(Set<Snack> snacks) { ... }
}
If desired, Collection<Provider<Snack>> can also be injected.

Contributing multibindings from different modules is supported. For example, it is okay for both CandyModule and ChipsModule to create their own Multibinder<Snack>, and to each contribute bindings to the set of snacks. When that set is injected, it will contain elements from both modules.

The set'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 set is unmodifiable. Elements can only be added to the set by configuring the multibinder. Elements can never be removed from the set.

Elements are resolved at set injection time. If an element is bound to a provider, that provider's get method will be called each time the set is injected (unless the binding is also scoped).

Annotations are used to create different sets of the same element type. Each distinct annotation gets its own independent collection of elements.

Elements must be distinct unless permitDuplicates() is specified. Without permitDuplicates(), set injection will fail if multiple bound elements have the same value.

Elements must be non-null. If any set element is null, set injection will fail.

Author
[email protected] (Jesse Wilson)

Field Summary

Modifier and TypeField and Description
private final RealMultibinder<T>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public LinkedBindingBuilder<T>
addBinding()

Returns a binding builder used to add a new element in the set.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public static <T> Multibinder<T>
newSetBinder(Binder binder, TypeLiteral<T> type)

Returns a new multibinder that collects instances of type in a Set that is itself bound with no binding annotation.

public static <T> Multibinder<T>
newSetBinder(Binder binder, Class<T> type)

Returns a new multibinder that collects instances of type in a Set that is itself bound with no binding annotation.

public static <T> Multibinder<T>
newSetBinder(Binder binder, TypeLiteral<T> type, Annotation annotation)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotation.

public static <T> Multibinder<T>
newSetBinder(Binder binder, Class<T> type, Annotation annotation)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotation.

public static <T> Multibinder<T>
newSetBinder(Binder binder, TypeLiteral<T> type, Class<? extends Annotation> annotationType)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotationType.

public static <T> Multibinder<T>
newSetBinder(Binder binder, Key<T> key)

Returns a new multibinder that collects instances of the key's type in a Set that is itself bound with the annotation (if any) of the key.

public static <T> Multibinder<T>
newSetBinder(Binder binder, Class<T> type, Class<? extends Annotation> annotationType)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotationType.

public Multibinder<T>

Returns:

this multibinder
permitDuplicates
()

Configures the bound set to silently discard duplicate elements.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateback to summary
private final RealMultibinder<T> delegate

Constructor Detail

Multibinderback to summary
private Multibinder(RealMultibinder<T> delegate)

Method Detail

addBindingback to summary
public LinkedBindingBuilder<T> addBinding()

Returns a binding builder used to add a new element in the set. Each bound element must have a distinct value. Bound providers will be evaluated each time the set 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.

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

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.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
newSetBinderback to summary
public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type)

Returns a new multibinder that collects instances of type in a Set that is itself bound with no binding annotation.

newSetBinderback to summary
public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type)

Returns a new multibinder that collects instances of type in a Set that is itself bound with no binding annotation.

newSetBinderback to summary
public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type, Annotation annotation)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotation.

newSetBinderback to summary
public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type, Annotation annotation)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotation.

newSetBinderback to summary
public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type, Class<? extends Annotation> annotationType)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotationType.

newSetBinderback to summary
public static <T> Multibinder<T> newSetBinder(Binder binder, Key<T> key)

Returns a new multibinder that collects instances of the key's type in a Set that is itself bound with the annotation (if any) of the key.

Since
4.0
newSetBinderback to summary
public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type, Class<? extends Annotation> annotationType)

Returns a new multibinder that collects instances of type in a Set that is itself bound with annotationType.

permitDuplicatesback to summary
public Multibinder<T> permitDuplicates()

Configures the bound set to silently discard duplicate elements. When multiple equal values are bound, the one that gets included is arbitrary. When multiple modules contribute elements to the set, this configuration option impacts all of them.

Returns:Multibinder<T>

this multibinder

Since
3.0