blob: 3cc2d050371774c6f035d05dfa26e3ab499e739c [file] [log] [blame]
limpbizkit00ca9f72008-08-02 17:56:17 +00001/**
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
17package com.google.inject.spi;
18
sberlinb7a02b02011-07-08 00:34:16 +000019import static com.google.common.base.Preconditions.checkNotNull;
20import static com.google.common.base.Preconditions.checkState;
21
Christian Edward Grubercade8972014-04-01 15:07:02 -070022import com.google.common.collect.ImmutableSet;
limpbizkit@gmail.com9a227be2010-07-03 15:51:31 +000023import com.google.inject.Binder;
limpbizkit00ca9f72008-08-02 17:56:17 +000024import com.google.inject.Key;
25import com.google.inject.Provider;
Christian Edward Grubercade8972014-04-01 15:07:02 -070026import com.google.inject.util.Types;
27
28import java.util.Set;
limpbizkit00ca9f72008-08-02 17:56:17 +000029
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@google.com (Jesse Wilson)
limpbizkitc489adf2008-11-18 07:01:33 +000038 * @since 2.0
limpbizkit00ca9f72008-08-02 17:56:17 +000039 */
40public final class ProviderLookup<T> implements Element {
41 private final Object source;
sameb9867f9c2015-02-02 12:45:25 -080042 private final Dependency<T> dependency;
limpbizkit00ca9f72008-08-02 17:56:17 +000043 private Provider<T> delegate;
44
limpbizkit8d620752009-03-31 22:37:26 +000045 public ProviderLookup(Object source, Key<T> key) {
sameb9867f9c2015-02-02 12:45:25 -080046 this(source, Dependency.get(checkNotNull(key, "key")));
47 }
48
49 /** @since 4.0 */
50 public ProviderLookup(Object source, Dependency<T> dependency) {
limpbizkit00ca9f72008-08-02 17:56:17 +000051 this.source = checkNotNull(source, "source");
sameb9867f9c2015-02-02 12:45:25 -080052 this.dependency = checkNotNull(dependency, "dependency");
limpbizkit00ca9f72008-08-02 17:56:17 +000053 }
54
55 public Object getSource() {
56 return source;
57 }
58
59 public Key<T> getKey() {
sameb9867f9c2015-02-02 12:45:25 -080060 return dependency.getKey();
61 }
62
cgdecker646e1742015-04-24 12:41:43 -070063 /** @since 4.0 */
sameb9867f9c2015-02-02 12:45:25 -080064 public Dependency<T> getDependency() {
65 return dependency;
limpbizkit00ca9f72008-08-02 17:56:17 +000066 }
67
limpbizkitafa4b5d2008-08-02 18:40:47 +000068 public <T> T acceptVisitor(ElementVisitor<T> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000069 return visitor.visit(this);
limpbizkit00ca9f72008-08-02 17:56:17 +000070 }
71
limpbizkit03b81a62009-03-18 05:34:39 +000072 /**
73 * Sets the actual provider.
74 *
limpbizkit03b81a62009-03-18 05:34:39 +000075 * @throws IllegalStateException if the delegate is already set
limpbizkit03b81a62009-03-18 05:34:39 +000076 */
77 public void initializeDelegate(Provider<T> delegate) {
limpbizkit00ca9f72008-08-02 17:56:17 +000078 checkState(this.delegate == null, "delegate already initialized");
limpbizkit97eac0f2009-03-28 18:25:35 +000079 this.delegate = checkNotNull(delegate, "delegate");
limpbizkit00ca9f72008-08-02 17:56:17 +000080 }
81
limpbizkit03b81a62009-03-18 05:34:39 +000082 public void applyTo(Binder binder) {
sameb9867f9c2015-02-02 12:45:25 -080083 initializeDelegate(binder.withSource(getSource()).getProvider(dependency));
limpbizkit03b81a62009-03-18 05:34:39 +000084 }
85
limpbizkit00ca9f72008-08-02 17:56:17 +000086 /**
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 }
limpbizkit8d620752009-03-31 22:37:26 +000093
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() {
Christian Edward Grubercade8972014-04-01 15:07:02 -0700100 return new ProviderWithDependencies<T>() {
limpbizkit8d620752009-03-31 22:37:26 +0000101 public T get() {
102 checkState(delegate != null,
103 "This Provider cannot be used until the Injector has been created.");
104 return delegate.get();
105 }
sameb9867f9c2015-02-02 12:45:25 -0800106
Christian Edward Grubercade8972014-04-01 15:07:02 -0700107 public Set<Dependency<?>> getDependencies() {
Christian Edward Grubercade8972014-04-01 15:07:02 -0700108 // We depend on Provider<T>, not T directly. This is an important distinction
109 // for dependency analysis tools that short-circuit on providers.
sameb9867f9c2015-02-02 12:45:25 -0800110 Key<?> providerKey = getKey().ofType(Types.providerOf(getKey().getTypeLiteral().getType()));
Christian Edward Grubercade8972014-04-01 15:07:02 -0700111 return ImmutableSet.<Dependency<?>>of(Dependency.get(providerKey));
112 }
limpbizkit8d620752009-03-31 22:37:26 +0000113
114 @Override public String toString() {
sameb9867f9c2015-02-02 12:45:25 -0800115 return "Provider<" + getKey().getTypeLiteral() + ">";
limpbizkit8d620752009-03-31 22:37:26 +0000116 }
117 };
118 }
limpbizkit00ca9f72008-08-02 17:56:17 +0000119}