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