blob: 8289a39d03745802774aea52c3824b04391b0ef6 [file] [log] [blame]
limpbizkit76c24b12008-12-25 04:32:41 +00001/**
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
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.InstanceBinding;
27import com.google.inject.util.Providers;
28import java.util.Set;
29
limpbizkit5ae41eb2009-06-06 17:51:27 +000030final class InstanceBindingImpl<T> extends BindingImpl<T> implements InstanceBinding<T> {
limpbizkit76c24b12008-12-25 04:32:41 +000031
32 final T instance;
33 final Provider<T> provider;
34 final ImmutableSet<InjectionPoint> injectionPoints;
35
limpbizkit5ae41eb2009-06-06 17:51:27 +000036 public InstanceBindingImpl(InjectorImpl injector, Key<T> key, Object source,
limpbizkit76c24b12008-12-25 04:32:41 +000037 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
limpbizkit8996e802008-12-28 01:44:29 +000057 public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000058 return visitor.visit(this);
limpbizkit76c24b12008-12-25 04:32:41 +000059 }
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
limpbizkit03b81a62009-03-18 05:34:39 +000083 public void applyTo(Binder binder) {
84 // instance bindings aren't scoped
85 binder.withSource(getSource()).bind(getKey()).toInstance(instance);
86 }
87
limpbizkit76c24b12008-12-25 04:32:41 +000088 @Override public String toString() {
89 return new ToStringBuilder(InstanceBinding.class)
90 .add("key", getKey())
limpbizkit76c24b12008-12-25 04:32:41 +000091 .add("source", getSource())
limpbizkitc3f92842008-12-30 19:43:47 +000092 .add("instance", instance)
limpbizkit76c24b12008-12-25 04:32:41 +000093 .toString();
94 }
95}