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