• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.inject.multibindings;
18 
19 import com.google.inject.Binding;
20 import com.google.inject.Key;
21 import com.google.inject.TypeLiteral;
22 import com.google.inject.spi.Element;
23 import com.google.inject.spi.Elements;
24 
25 import java.util.List;
26 
27 /**
28  * A binding for a Multibinder.
29  *
30  * @param <T> The fully qualified type of the set, including Set. For example:
31  *          <code>MultibinderBinding&lt;Set&lt;Boolean>></code>
32  *
33  * @since 3.0
34  * @author sameb@google.com (Sam Berlin)
35  */
36 public interface MultibinderBinding<T> {
37 
38   /** Returns the key for the set. */
getSetKey()39   Key<T> getSetKey();
40 
41   /**
42    * Returns the TypeLiteral that describes the type of elements in the set.
43    * <p>
44    * The elements will always match the type Set's generic type. For example, if getSetKey returns a
45    * key of <code>Set&lt;String></code>, then this will always return a
46    * <code>TypeLiteral&lt;String></code>.
47    */
getElementTypeLiteral()48   TypeLiteral<?> getElementTypeLiteral();
49 
50   /**
51    * Returns all bindings that make up the set. This is only supported on bindings returned from an
52    * injector. This will throw {@link UnsupportedOperationException} if it is called on an element
53    * retrieved from {@link Elements#getElements}.
54    * <p>
55    * The elements will always match the type Set's generic type. For example, if getSetKey returns a
56    * key of <code>Set&lt;String></code>, then this will always return a list of type
57    * <code>List&lt;Binding&lt;String>></code>.
58    */
getElements()59   List<Binding<?>> getElements();
60 
61   /**
62    * Returns true if the multibinder permits duplicates. This is only supported on bindings returned
63    * from an injector. This will throw {@link UnsupportedOperationException} if it is called on a
64    * MultibinderBinding retrieved from {@link Elements#getElements}.
65    */
permitsDuplicates()66   boolean permitsDuplicates();
67 
68   /**
69    * Returns true if this Multibinder uses the given Element. This will be true for bindings that
70    * derive the elements of the set and other bindings that Multibinder uses internally. This will
71    * work for MultibinderBindings retrieved from an injector and {@link Elements#getElements}.
72    * Usually this is only necessary if you are working with elements retrieved from modules (without
73    * an Injector), otherwise {@link #getElements} and {@link #permitsDuplicates} are better options.
74    * <p>
75    * If you need to introspect the details of the set, such as the values or if it permits
76    * duplicates, it is necessary to pass the elements through an Injector and use
77    * {@link #getElements()} and {@link #permitsDuplicates()}.
78    */
containsElement(Element element)79   boolean containsElement(Element element);
80 }
81