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