blob: 72d4ec23f5631cf2d4bf50d4352b04b4cac7f885 [file] [log] [blame]
limpbizkit76c24b12008-12-25 04:32:41 +00001/**
2 * Copyright (C) 2006 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.internal;
18
19import com.google.inject.Binding;
limpbizkit76c24b12008-12-25 04:32:41 +000020import com.google.inject.Key;
21import com.google.inject.Provider;
22import com.google.inject.spi.BindingScopingVisitor;
23import com.google.inject.spi.ElementVisitor;
24import com.google.inject.spi.InstanceBinding;
25
26/**
27 * @author crazybob@google.com (Bob Lee)
28 */
29public abstract class BindingImpl<T> implements Binding<T> {
30
limpbizkit5ae41eb2009-06-06 17:51:27 +000031 private final InjectorImpl injector;
limpbizkit76c24b12008-12-25 04:32:41 +000032 private final Key<T> key;
33 private final Object source;
34 private final Scoping scoping;
35 private final InternalFactory<? extends T> internalFactory;
36
limpbizkit5ae41eb2009-06-06 17:51:27 +000037 public BindingImpl(InjectorImpl injector, Key<T> key, Object source,
limpbizkit76c24b12008-12-25 04:32:41 +000038 InternalFactory<? extends T> internalFactory, Scoping scoping) {
39 this.injector = injector;
40 this.key = key;
41 this.source = source;
42 this.internalFactory = internalFactory;
43 this.scoping = scoping;
44 }
45
46 protected BindingImpl(Object source, Key<T> key, Scoping scoping) {
47 this.internalFactory = null;
48 this.injector = null;
49 this.source = source;
50 this.key = key;
51 this.scoping = scoping;
52 }
53
54 public Key<T> getKey() {
55 return key;
56 }
57
58 public Object getSource() {
59 return source;
60 }
61
62 private volatile Provider<T> provider;
63
64 public Provider<T> getProvider() {
65 if (provider == null) {
66 if (injector == null) {
67 throw new UnsupportedOperationException("getProvider() not supported for module bindings");
68 }
69
70 provider = injector.getProvider(key);
71 }
72 return provider;
73 }
74
75 public InternalFactory<? extends T> getInternalFactory() {
76 return internalFactory;
77 }
78
79 public Scoping getScoping() {
80 return scoping;
81 }
82
83 /**
84 * Is this a constant binding? This returns true for constant bindings as
85 * well as toInstance() bindings.
86 */
87 public boolean isConstant() {
88 return this instanceof InstanceBinding;
89 }
90
91 public <V> V acceptVisitor(ElementVisitor<V> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000092 return visitor.visit(this);
limpbizkit76c24b12008-12-25 04:32:41 +000093 }
94
95 public <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor) {
96 return scoping.acceptVisitor(visitor);
97 }
98
limpbizkit76c24b12008-12-25 04:32:41 +000099 protected BindingImpl<T> withScoping(Scoping scoping) {
100 throw new AssertionError();
101 }
102
103 protected BindingImpl<T> withKey(Key<T> key) {
104 throw new AssertionError();
105 }
106
107 @Override public String toString() {
108 return new ToStringBuilder(Binding.class)
109 .add("key", key)
110 .add("scope", scoping)
111 .add("source", source)
112 .toString();
113 }
114
limpbizkit5ae41eb2009-06-06 17:51:27 +0000115 public InjectorImpl getInjector() {
limpbizkit76c24b12008-12-25 04:32:41 +0000116 return injector;
117 }
118}