• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.spi.Element;
22 import com.google.inject.spi.Elements;
23 
24 /**
25  * A binding for a OptionalBinder.
26  *
27  * <p>Although OptionalBinders may be injected through a variety of types {@code T}, {@code
28  * Optional<T>}, {@code Optional<Provider<T>>}, etc..), an OptionalBinderBinding exists only on the
29  * Binding associated with the {@code Optional<T>} key. Other bindings can be validated to be
30  * derived from this OptionalBinderBinding using {@link #containsElement}.
31  *
32  * @param <T> The fully qualified type of the optional binding, including Optional. For example:
33  *     {@code Optional<String>}.
34  * @since 4.0
35  * @author sameb@google.com (Sam Berlin)
36  */
37 public interface OptionalBinderBinding<T> {
38 
39   /** Returns the {@link Key} for this binding. */
getKey()40   Key<T> getKey();
41 
42   /**
43    * Returns the default binding (set by {@link OptionalBinder#setDefault}) if one exists or null if
44    * no default binding is set. This will throw {@link UnsupportedOperationException} if it is
45    * called on an element retrieved from {@link Elements#getElements}.
46    *
47    * <p>The Binding's type will always match the type Optional's generic type. For example, if
48    * getKey returns a key of <code>Optional&lt;String></code>, then this will always return a <code>
49    * Binding&lt;String></code>.
50    */
getDefaultBinding()51   Binding<?> getDefaultBinding();
52 
53   /**
54    * Returns the actual binding (set by {@link OptionalBinder#setBinding}) or null if not set. This
55    * will throw {@link UnsupportedOperationException} if it is called on an element retrieved from
56    * {@link Elements#getElements}.
57    *
58    * <p>The Binding's type will always match the type Optional's generic type. For example, if
59    * getKey returns a key of <code>Optional&lt;String></code>, then this will always return a <code>
60    * Binding&lt;String></code>.
61    */
getActualBinding()62   Binding<?> getActualBinding();
63 
64   /**
65    * Returns true if this OptionalBinder contains the given Element in order to build the optional
66    * binding or uses the given Element in order to support building and injecting its data. This
67    * will work for OptionalBinderBinding retrieved from an injector and {@link
68    * Elements#getElements}. Usually this is only necessary if you are working with elements
69    * retrieved from modules (without an Injector), otherwise {@link #getDefaultBinding} and {@link
70    * #getActualBinding} are better options.
71    */
containsElement(Element element)72   boolean containsElement(Element element);
73 }
74