limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright (C) 2008 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 | |
| 18 | package com.google.inject; |
| 19 | |
| 20 | import com.google.inject.spi.InstanceBinding; |
| 21 | import com.google.inject.spi.BindingVisitor; |
| 22 | import com.google.inject.spi.Dependency; |
| 23 | import com.google.inject.util.Providers; |
| 24 | import com.google.inject.internal.ToStringBuilder; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | |
| 28 | class InstanceBindingImpl<T> extends BindingImpl<T> |
| 29 | implements InstanceBinding<T> { |
| 30 | |
| 31 | final T instance; |
| 32 | final Provider<T> provider; |
| 33 | |
| 34 | InstanceBindingImpl(InjectorImpl injector, Key<T> key, Object source, |
| 35 | InternalFactory<? extends T> internalFactory, T instance) { |
| 36 | super(injector, key, source, internalFactory, Scopes.NO_SCOPE); |
| 37 | this.instance = instance; |
| 38 | this.provider = Providers.of(instance); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public Provider<T> getProvider() { |
| 43 | return this.provider; |
| 44 | } |
| 45 | |
| 46 | public void accept(BindingVisitor<? super T> bindingVisitor) { |
| 47 | bindingVisitor.visit(this); |
| 48 | } |
| 49 | |
| 50 | public T getInstance() { |
| 51 | return this.instance; |
| 52 | } |
| 53 | |
| 54 | public Collection<Dependency<?>> getDependencies() { |
| 55 | return injector.getFieldAndMethodDependenciesFor(instance.getClass()); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String toString() { |
| 60 | return new ToStringBuilder(InstanceBinding.class) |
| 61 | .add("key", key) |
| 62 | .add("instance", instance) |
| 63 | .add("source", source) |
| 64 | .toString(); |
| 65 | } |
| 66 | } |