crazyboblee | 1fc4978 | 2007-02-25 21:02:47 +0000 | [diff] [blame^] | 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 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 | } |