Home | History | Annotate | Download | only in spi
      1 /**
      2  * Copyright (C) 2008 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.spi;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 import static com.google.common.base.Preconditions.checkState;
     21 
     22 import com.google.common.collect.ImmutableSet;
     23 import com.google.inject.Binder;
     24 import com.google.inject.Key;
     25 import com.google.inject.Provider;
     26 import com.google.inject.util.Types;
     27 
     28 import java.util.Set;
     29 
     30 /**
     31  * A lookup of the provider for a type. Lookups are created explicitly in a module using
     32  * {@link com.google.inject.Binder#getProvider(Class) getProvider()} statements:
     33  * <pre>
     34  *     Provider&lt;PaymentService&gt; paymentServiceProvider
     35  *         = getProvider(PaymentService.class);</pre>
     36  *
     37  * @author jessewilson (at) google.com (Jesse Wilson)
     38  * @since 2.0
     39  */
     40 public final class ProviderLookup<T> implements Element {
     41   private final Object source;
     42   private final Dependency<T> dependency;
     43   private Provider<T> delegate;
     44 
     45   public ProviderLookup(Object source, Key<T> key) {
     46     this(source, Dependency.get(checkNotNull(key, "key")));
     47   }
     48 
     49   /** @since 4.0 */
     50   public ProviderLookup(Object source, Dependency<T> dependency) {
     51     this.source = checkNotNull(source, "source");
     52     this.dependency = checkNotNull(dependency, "dependency");
     53   }
     54 
     55   public Object getSource() {
     56     return source;
     57   }
     58 
     59   public Key<T> getKey() {
     60     return dependency.getKey();
     61   }
     62 
     63   /** @since 4.0 */
     64   public Dependency<T> getDependency() {
     65     return dependency;
     66   }
     67 
     68   public <T> T acceptVisitor(ElementVisitor<T> visitor) {
     69     return visitor.visit(this);
     70   }
     71 
     72   /**
     73    * Sets the actual provider.
     74    *
     75    * @throws IllegalStateException if the delegate is already set
     76    */
     77   public void initializeDelegate(Provider<T> delegate) {
     78     checkState(this.delegate == null, "delegate already initialized");
     79     this.delegate = checkNotNull(delegate, "delegate");
     80   }
     81 
     82   public void applyTo(Binder binder) {
     83     initializeDelegate(binder.withSource(getSource()).getProvider(dependency));
     84   }
     85 
     86   /**
     87    * Returns the delegate provider, or {@code null} if it has not yet been initialized. The delegate
     88    * will be initialized when this element is processed, or otherwise used to create an injector.
     89    */
     90   public Provider<T> getDelegate() {
     91     return delegate;
     92   }
     93 
     94   /**
     95    * Returns the looked up provider. The result is not valid until this lookup has been initialized,
     96    * which usually happens when the injector is created. The provider will throw an {@code
     97    * IllegalStateException} if you try to use it beforehand.
     98    */
     99   public Provider<T> getProvider() {
    100     return new ProviderWithDependencies<T>() {
    101       public T get() {
    102         checkState(delegate != null,
    103             "This Provider cannot be used until the Injector has been created.");
    104         return delegate.get();
    105       }
    106 
    107       public Set<Dependency<?>> getDependencies() {
    108         // We depend on Provider<T>, not T directly.  This is an important distinction
    109         // for dependency analysis tools that short-circuit on providers.
    110         Key<?> providerKey = getKey().ofType(Types.providerOf(getKey().getTypeLiteral().getType()));
    111         return ImmutableSet.<Dependency<?>>of(Dependency.get(providerKey));
    112       }
    113 
    114       @Override public String toString() {
    115         return "Provider<" + getKey().getTypeLiteral() + ">";
    116       }
    117     };
    118   }
    119 }
    120