blob: 1bf8e97240d8bde9c44f66e85eebacaeaec41b81 [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Binder;
limpbizkit76c24b12008-12-25 04:32:41 +000020import com.google.inject.Key;
21import com.google.inject.Provider;
22import com.google.inject.spi.BindingTargetVisitor;
23import com.google.inject.spi.Dependency;
24import com.google.inject.spi.HasDependencies;
25import com.google.inject.spi.InjectionPoint;
26import com.google.inject.spi.ProviderInstanceBinding;
27import java.util.Set;
28
limpbizkit5ae41eb2009-06-06 17:51:27 +000029final class ProviderInstanceBindingImpl<T> extends BindingImpl<T>
limpbizkit76c24b12008-12-25 04:32:41 +000030 implements ProviderInstanceBinding<T> {
31
32 final Provider<? extends T> providerInstance;
33 final ImmutableSet<InjectionPoint> injectionPoints;
34
limpbizkit5ae41eb2009-06-06 17:51:27 +000035 public ProviderInstanceBindingImpl(InjectorImpl injector, Key<T> key,
limpbizkit76c24b12008-12-25 04:32:41 +000036 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
limpbizkit8996e802008-12-28 01:44:29 +000051 public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000052 return visitor.visit(this);
limpbizkit76c24b12008-12-25 04:32:41 +000053 }
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
limpbizkit03b81a62009-03-18 05:34:39 +000079 public void applyTo(Binder binder) {
80 getScoping().applyTo(
81 binder.withSource(getSource()).bind(getKey()).toProvider(getProviderInstance()));
82 }
83
limpbizkit76c24b12008-12-25 04:32:41 +000084 @Override
85 public String toString() {
86 return new ToStringBuilder(ProviderInstanceBinding.class)
87 .add("key", getKey())
limpbizkit76c24b12008-12-25 04:32:41 +000088 .add("source", getSource())
limpbizkitc3f92842008-12-30 19:43:47 +000089 .add("scope", getScoping())
90 .add("provider", providerInstance)
limpbizkit76c24b12008-12-25 04:32:41 +000091 .toString();
92 }
93}