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