KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > BindingImpl


1 /**
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
17 package com.google.inject;
18
19 import com.google.inject.util.ToStringBuilder;
20
21 /**
22  * @author crazybob@google.com (Bob Lee)
23  */

24 class BindingImpl<T> implements Binding<T> {
25
26   final InjectorImpl injector;
27   final Key<T> key;
28   final Object JavaDoc source;
29   final InternalFactory<? extends T> internalFactory;
30
31   BindingImpl(InjectorImpl injector, Key<T> key, Object JavaDoc source,
32       InternalFactory<? extends T> internalFactory) {
33     this.injector = injector;
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 JavaDoc getSource() {
44     return source;
45   }
46
47   volatile Provider<T> provider;
48
49   public Provider<T> getProvider() {
50     if (provider == null) {
51       provider = injector.getProvider(key);
52     }
53     return provider;
54   }
55
56   InternalFactory<? extends T> getInternalFactory() {
57     return internalFactory;
58   }
59
60   static <T> BindingImpl<T> newInstance(InjectorImpl injector, Key<T> key,
61       Object JavaDoc source, InternalFactory<? extends T> internalFactory) {
62     return new BindingImpl<T>(injector, 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 JavaDoc toString() {
73     return new ToStringBuilder(BindingImpl.class)
74         .add("key", key)
75         .add("source", source)
76         .add("provider", internalFactory)
77         .toString();
78   }
79 }
Popular Tags