static final class Multibinder.RealMultibinder<T> extends Multibinder<T> implements Module, ProviderWithExtensionVisitor<java.util.Set<T>>, HasDependencies, MultibinderBinding<java.util.Set<T>>
As a Multibinder, it acts as a factory for LinkedBindingBuilders for each of the set's elements. Each binding is given an annotation that identifies it as a part of this set.
As a Module, it installs the binding to the set itself. As a module, this implements equals() and hashcode() in order to trick Guice into executing its configure() method only once. That makes it so that multiple multibinders can be created for the same target collection, but only one is bound. Since the list of bindings is retrieved from the injector itself (and not the multibinder), each multibinder has access to all contributions from all multibinders.
As a Provider, this constructs the set instances.
We use a subclass to hide 'implements Module, Provider' from the public API.
Modifier and Type | Class and Description |
---|---|
(package private) class |
Multibinder.RealMultibinder.RealMultibinderCollectionOfProvidersProvider |
Multibinder.RealMultibinder<T>
Modifier and Type | Field and Description |
---|---|
private Binder |
binder |
private com.google.common.collect.ImmutableList<Binding<T>> |
bindings |
private Key<java.util.Collection<javax.inject.Provider<T>>> |
collectionOfJavaxProvidersKey |
private Key<java.util.Collection<Provider<T>>> |
collectionOfProvidersKey |
private java.util.Set<Dependency<?>> |
dependencies |
private TypeLiteral<T> |
elementType |
private boolean |
permitDuplicates
whether duplicates are allowed.
|
private Key<java.lang.Boolean> |
permitDuplicatesKey |
private Key<java.util.Set<T>> |
setKey |
private java.lang.String |
setName |
Modifier | Constructor and Description |
---|---|
private |
RealMultibinder(Binder binder,
TypeLiteral<T> elementType,
Key<java.util.Set<T>> setKey) |
Modifier and Type | Method and Description |
---|---|
<B,V> V |
acceptExtensionVisitor(BindingTargetVisitor<B,V> visitor,
ProviderInstanceBinding<? extends B> binding)
Instructs the extension determine if the visitor is an instance of a custom
extension visitor, and if so, visit it using that method.
|
LinkedBindingBuilder<T> |
addBinding()
Returns a binding builder used to add a new element in the set.
|
void |
configure(Binder binder)
Contributes bindings and other configurations for this module to
binder . |
boolean |
containsElement(Element element)
Returns true if this Multibinder uses the given Element.
|
boolean |
equals(java.lang.Object o) |
java.util.Set<T> |
get()
Provides an instance of
T . |
java.util.Set<Dependency<?>> |
getDependencies()
Returns the known dependencies for this type.
|
java.util.List<Binding<?>> |
getElements()
Returns all bindings that make up the set.
|
TypeLiteral<?> |
getElementTypeLiteral()
Returns the TypeLiteral that describes the type of elements in the set.
|
(package private) Key<T> |
getKeyForNewItem() |
Key<java.util.Set<T>> |
getSetKey()
Returns the key for the set.
|
(package private) java.lang.String |
getSetName() |
int |
hashCode() |
(package private) void |
initialize(Injector injector)
Invoked by Guice at Injector-creation time to prepare providers for each
element in this set.
|
private boolean |
isInitialized() |
private boolean |
keyMatches(Key<?> key) |
private static int |
mapCapacity(int numBindings) |
Multibinder<T> |
permitDuplicates()
Configures the bound set to silently discard duplicate elements.
|
boolean |
permitsDuplicates()
Returns true if the multibinder permits duplicates.
|
(package private) boolean |
permitsDuplicates(Injector injector) |
java.lang.String |
toString() |
checkConfiguration, checkNotNull, collectionOfJavaxProvidersOf, collectionOfProvidersOf, newRealSetBinder, newSetBinder, newSetBinder, newSetBinder, newSetBinder, newSetBinder, newSetBinder, newSetBinder, setOf
private final TypeLiteral<T> elementType
private final java.lang.String setName
private final Key<java.util.Collection<Provider<T>>> collectionOfProvidersKey
private final Key<java.util.Collection<javax.inject.Provider<T>>> collectionOfJavaxProvidersKey
private final Key<java.lang.Boolean> permitDuplicatesKey
private Binder binder
private java.util.Set<Dependency<?>> dependencies
private boolean permitDuplicates
private RealMultibinder(Binder binder, TypeLiteral<T> elementType, Key<java.util.Set<T>> setKey)
public void configure(Binder binder)
Module
binder
.
Do not invoke this method directly to install submodules. Instead use
Binder.install(Module)
, which ensures that provider methods
are
discovered.
public Multibinder<T> permitDuplicates()
Multibinder
permitDuplicates
in class Multibinder<T>
public LinkedBindingBuilder<T> addBinding()
Multibinder
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
in class Multibinder<T>
@Toolable @Inject void initialize(Injector injector)
private static int mapCapacity(int numBindings)
boolean permitsDuplicates(Injector injector)
private boolean keyMatches(Key<?> key)
private boolean isInitialized()
public java.util.Set<T> get()
Provider
T
. Must never return null
.get
in interface javax.inject.Provider<java.util.Set<T>>
public <B,V> V acceptExtensionVisitor(BindingTargetVisitor<B,V> visitor, ProviderInstanceBinding<? extends B> binding)
ProviderWithExtensionVisitor
Due to issues with generics, the type parameters of this method do not relate to the type of the provider. In practice, the 'B' type will always be a supertype of 'T'.
acceptExtensionVisitor
in interface ProviderWithExtensionVisitor<java.util.Set<T>>
java.lang.String getSetName()
public TypeLiteral<?> getElementTypeLiteral()
MultibinderBinding
The elements will always match the type Set's generic type. For example, if getSetKey returns a
key of Set<String>
, then this will always return a
TypeLiteral<String>
.
getElementTypeLiteral
in interface MultibinderBinding<java.util.Set<T>>
public Key<java.util.Set<T>> getSetKey()
MultibinderBinding
getSetKey
in interface MultibinderBinding<java.util.Set<T>>
public java.util.List<Binding<?>> getElements()
MultibinderBinding
UnsupportedOperationException
if it is called on an element
retrieved from Elements.getElements(com.google.inject.Module...)
.
The elements will always match the type Set's generic type. For example, if getSetKey returns a
key of Set<String>
, then this will always return a list of type
List<Binding<String>>
.
getElements
in interface MultibinderBinding<java.util.Set<T>>
public boolean permitsDuplicates()
MultibinderBinding
UnsupportedOperationException
if it is called on a
MultibinderBinding retrieved from Elements.getElements(com.google.inject.Module...)
.permitsDuplicates
in interface MultibinderBinding<java.util.Set<T>>
public boolean containsElement(Element element)
MultibinderBinding
Elements.getElements(com.google.inject.Module...)
.
Usually this is only necessary if you are working with elements retrieved from modules (without
an Injector), otherwise MultibinderBinding.getElements()
and MultibinderBinding.permitsDuplicates()
are better options.
If you need to introspect the details of the set, such as the values or if it permits
duplicates, it is necessary to pass the elements through an Injector and use
MultibinderBinding.getElements()
and MultibinderBinding.permitsDuplicates()
.
containsElement
in interface MultibinderBinding<java.util.Set<T>>
public java.util.Set<Dependency<?>> getDependencies()
HasDependencies
Injector
will be
included in the returned set.getDependencies
in interface HasDependencies
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object