blob: 907e000d4beafc0f9b8d45c8dbecfe7206928f18 [file] [log] [blame]
limpbizkit76c24b12008-12-25 04:32:41 +00001/*
2Copyright (C) 2007 Google Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package com.google.inject.internal;
18
limpbizkit76c24b12008-12-25 04:32:41 +000019import com.google.inject.Injector;
20import com.google.inject.Key;
21import com.google.inject.Provider;
limpbizkit03b81a62009-03-18 05:34:39 +000022import com.google.inject.Binder;
limpbizkit76c24b12008-12-25 04:32:41 +000023import com.google.inject.spi.BindingTargetVisitor;
24import com.google.inject.spi.Dependency;
25import com.google.inject.spi.HasDependencies;
26import com.google.inject.spi.InjectionPoint;
27import com.google.inject.spi.ProviderInstanceBinding;
28import java.util.Set;
29
30public final class ProviderInstanceBindingImpl<T> extends BindingImpl<T>
31 implements ProviderInstanceBinding<T> {
32
33 final Provider<? extends T> providerInstance;
34 final ImmutableSet<InjectionPoint> injectionPoints;
35
36 public ProviderInstanceBindingImpl(Injector injector, Key<T> key,
37 Object source, InternalFactory<? extends T> internalFactory, Scoping scoping,
38 Provider<? extends T> providerInstance,
39 Set<InjectionPoint> injectionPoints) {
40 super(injector, key, source, internalFactory, scoping);
41 this.providerInstance = providerInstance;
42 this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
43 }
44
45 public ProviderInstanceBindingImpl(Object source, Key<T> key, Scoping scoping,
46 Set<InjectionPoint> injectionPoints, Provider<? extends T> providerInstance) {
47 super(source, key, scoping);
48 this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
49 this.providerInstance = providerInstance;
50 }
51
limpbizkit8996e802008-12-28 01:44:29 +000052 public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000053 return visitor.visit(this);
limpbizkit76c24b12008-12-25 04:32:41 +000054 }
55
56 public Provider<? extends T> getProviderInstance() {
57 return providerInstance;
58 }
59
60 public Set<InjectionPoint> getInjectionPoints() {
61 return injectionPoints;
62 }
63
64 public Set<Dependency<?>> getDependencies() {
65 return providerInstance instanceof HasDependencies
66 ? ImmutableSet.copyOf(((HasDependencies) providerInstance).getDependencies())
67 : Dependency.forInjectionPoints(injectionPoints);
68 }
69
70 public BindingImpl<T> withScoping(Scoping scoping) {
71 return new ProviderInstanceBindingImpl<T>(
72 getSource(), getKey(), scoping, injectionPoints, providerInstance);
73 }
74
75 public BindingImpl<T> withKey(Key<T> key) {
76 return new ProviderInstanceBindingImpl<T>(
77 getSource(), key, getScoping(), injectionPoints, providerInstance);
78 }
79
limpbizkit03b81a62009-03-18 05:34:39 +000080 public void applyTo(Binder binder) {
81 getScoping().applyTo(
82 binder.withSource(getSource()).bind(getKey()).toProvider(getProviderInstance()));
83 }
84
limpbizkit76c24b12008-12-25 04:32:41 +000085 @Override
86 public String toString() {
87 return new ToStringBuilder(ProviderInstanceBinding.class)
88 .add("key", getKey())
limpbizkit76c24b12008-12-25 04:32:41 +000089 .add("source", getSource())
limpbizkitc3f92842008-12-30 19:43:47 +000090 .add("scope", getScoping())
91 .add("provider", providerInstance)
limpbizkit76c24b12008-12-25 04:32:41 +000092 .toString();
93 }
94}