blob: 4e21de68de25c35fba980d05c281c3e68077343e [file] [log] [blame]
crazyboblee1fc49782007-02-25 21:02:47 +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;
18
19import com.google.inject.util.ToStringBuilder;
20
21/**
22 * @author crazybob@google.com (Bob Lee)
23 */
24class BindingImpl<T> implements Binding<T> {
25
26 final ContainerImpl container;
27 final Key<T> key;
28 final Object source;
29 final InternalFactory<? extends T> internalFactory;
30
31 BindingImpl(ContainerImpl container, Key<T> key, Object source,
32 InternalFactory<? extends T> internalFactory) {
33 this.container = container;
34 this.key = key;
35 this.source = source;
36 this.internalFactory = internalFactory;
37 }
38
39 public Key<T> getKey() {
40 return key;
41 }
42
43 public Object getSource() {
44 return source;
45 }
46
47 volatile Provider<T> provider;
48
49 public Provider<T> getProvider() {
50 if (provider == null) {
51 provider = container.getProvider(key);
52 }
53 return provider;
54 }
55
56 InternalFactory<? extends T> getInternalFactory() {
57 return internalFactory;
58 }
59
60 static <T> BindingImpl<T> newInstance(ContainerImpl container, Key<T> key,
61 Object source, InternalFactory<? extends T> internalFactory) {
62 return new BindingImpl<T>(container, key, source, 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)
74 .add("key", key)
75 .add("source", source)
76 .add("provider", internalFactory)
77 .toString();
78 }
79}