limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +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 | package com.google.inject.internal; |
| 18 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 19 | import com.google.inject.Injector; |
| 20 | import com.google.inject.Key; |
| 21 | import com.google.inject.Provider; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 22 | import com.google.inject.Binder; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.BindingTargetVisitor; |
| 24 | import com.google.inject.spi.Dependency; |
| 25 | import com.google.inject.spi.HasDependencies; |
| 26 | import com.google.inject.spi.InjectionPoint; |
| 27 | import com.google.inject.spi.InstanceBinding; |
| 28 | import com.google.inject.util.Providers; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | public class InstanceBindingImpl<T> extends BindingImpl<T> implements InstanceBinding<T> { |
| 32 | |
| 33 | final T instance; |
| 34 | final Provider<T> provider; |
| 35 | final ImmutableSet<InjectionPoint> injectionPoints; |
| 36 | |
| 37 | public InstanceBindingImpl(Injector injector, Key<T> key, Object source, |
| 38 | InternalFactory<? extends T> internalFactory, Set<InjectionPoint> injectionPoints, |
| 39 | T instance) { |
| 40 | super(injector, key, source, internalFactory, Scoping.UNSCOPED); |
| 41 | this.injectionPoints = ImmutableSet.copyOf(injectionPoints); |
| 42 | this.instance = instance; |
| 43 | this.provider = Providers.of(instance); |
| 44 | } |
| 45 | |
| 46 | public InstanceBindingImpl(Object source, Key<T> key, Scoping scoping, |
| 47 | Set<InjectionPoint> injectionPoints, T instance) { |
| 48 | super(source, key, scoping); |
| 49 | this.injectionPoints = ImmutableSet.copyOf(injectionPoints); |
| 50 | this.instance = instance; |
| 51 | this.provider = Providers.of(instance); |
| 52 | } |
| 53 | |
| 54 | @Override public Provider<T> getProvider() { |
| 55 | return this.provider; |
| 56 | } |
| 57 | |
limpbizkit | 8996e80 | 2008-12-28 01:44:29 +0000 | [diff] [blame] | 58 | public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 59 | return visitor.visit(this); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public T getInstance() { |
| 63 | return instance; |
| 64 | } |
| 65 | |
| 66 | public Set<InjectionPoint> getInjectionPoints() { |
| 67 | return injectionPoints; |
| 68 | } |
| 69 | |
| 70 | public Set<Dependency<?>> getDependencies() { |
| 71 | return instance instanceof HasDependencies |
| 72 | ? ImmutableSet.copyOf(((HasDependencies) instance).getDependencies()) |
| 73 | : Dependency.forInjectionPoints(injectionPoints); |
| 74 | } |
| 75 | |
| 76 | public BindingImpl<T> withScoping(Scoping scoping) { |
| 77 | return new InstanceBindingImpl<T>(getSource(), getKey(), scoping, injectionPoints, instance); |
| 78 | } |
| 79 | |
| 80 | public BindingImpl<T> withKey(Key<T> key) { |
| 81 | return new InstanceBindingImpl<T>(getSource(), key, getScoping(), injectionPoints, instance); |
| 82 | } |
| 83 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 84 | public void applyTo(Binder binder) { |
| 85 | // instance bindings aren't scoped |
| 86 | binder.withSource(getSource()).bind(getKey()).toInstance(instance); |
| 87 | } |
| 88 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 89 | @Override public String toString() { |
| 90 | return new ToStringBuilder(InstanceBinding.class) |
| 91 | .add("key", getKey()) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 92 | .add("source", getSource()) |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 93 | .add("instance", instance) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 94 | .toString(); |
| 95 | } |
| 96 | } |