01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.bm.ejb3guice.inject;
16:
17: import com.bm.ejb3guice.internal.ToStringBuilder;
18:
19: /**
20: * @author crazybob@google.com (Bob Lee)
21: */
22: class BindingImpl<T> implements Binding<T> {
23:
24: final InjectorImpl injector;
25: final Key<T> key;
26: final Object source;
27: final InternalFactory<? extends T> internalFactory;
28:
29: BindingImpl(InjectorImpl injector, Key<T> key, Object source,
30: InternalFactory<? extends T> internalFactory) {
31: this .injector = injector;
32: this .key = key;
33: this .source = source;
34: this .internalFactory = internalFactory;
35: }
36:
37: public Key<T> getKey() {
38: return key;
39: }
40:
41: public Object getSource() {
42: return source;
43: }
44:
45: volatile Provider<T> provider;
46:
47: public Provider<T> getProvider() {
48: if (provider == null) {
49: provider = injector.getProvider(key);
50: }
51: return provider;
52: }
53:
54: InternalFactory<? extends T> getInternalFactory() {
55: return internalFactory;
56: }
57:
58: static <T> BindingImpl<T> newInstance(InjectorImpl injector,
59: Key<T> key, Object source,
60: InternalFactory<? extends T> internalFactory) {
61: return new BindingImpl<T>(injector, key, source,
62: internalFactory);
63: }
64:
65: /**
66: * Is this a constant binding?
67: */
68: boolean isConstant() {
69: return internalFactory instanceof ConstantFactory<?>;
70: }
71:
72: public String toString() {
73: return new ToStringBuilder(BindingImpl.class).add("key", key)
74: .add("source", source).add("provider", internalFactory)
75: .toString();
76: }
77: }
|