blob: efa3f128288797c232ab61c320dff0e9bf90ed26 [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
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.InstanceBinding;
28import com.google.inject.util.Providers;
29import java.util.Set;
30
31public class InstanceBindingImpl<T> extends BindingImpl<T> implements InstanceBinding<T> {
32
33 final T instance;
34 final Provider<T> provider;
35 final ImmutableSet<InjectionPoint> injectionPoints;
36
37 public InstanceBindingImpl(Injector injector, Key<T> key, Object source,
38 InternalFactory<? extends T> internalFactory, Set<InjectionPoint> injectionPoints,
39 T instance) {
40 super(injector, key, source, internalFactory, Scoping.UNSCOPED);
41 this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
42 this.instance = instance;
43 this.provider = Providers.of(instance);
44 }
45
46 public InstanceBindingImpl(Object source, Key<T> key, Scoping scoping,
47 Set<InjectionPoint> injectionPoints, T instance) {
48 super(source, key, scoping);
49 this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
50 this.instance = instance;
51 this.provider = Providers.of(instance);
52 }
53
54 @Override public Provider<T> getProvider() {
55 return this.provider;
56 }
57
limpbizkit8996e802008-12-28 01:44:29 +000058 public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000059 return visitor.visit(this);
limpbizkit76c24b12008-12-25 04:32:41 +000060 }
61
62 public T getInstance() {
63 return instance;
64 }
65
66 public Set<InjectionPoint> getInjectionPoints() {
67 return injectionPoints;
68 }
69
70 public Set<Dependency<?>> getDependencies() {
71 return instance instanceof HasDependencies
72 ? ImmutableSet.copyOf(((HasDependencies) instance).getDependencies())
73 : Dependency.forInjectionPoints(injectionPoints);
74 }
75
76 public BindingImpl<T> withScoping(Scoping scoping) {
77 return new InstanceBindingImpl<T>(getSource(), getKey(), scoping, injectionPoints, instance);
78 }
79
80 public BindingImpl<T> withKey(Key<T> key) {
81 return new InstanceBindingImpl<T>(getSource(), key, getScoping(), injectionPoints, instance);
82 }
83
limpbizkit03b81a62009-03-18 05:34:39 +000084 public void applyTo(Binder binder) {
85 // instance bindings aren't scoped
86 binder.withSource(getSource()).bind(getKey()).toInstance(instance);
87 }
88
limpbizkit76c24b12008-12-25 04:32:41 +000089 @Override public String toString() {
90 return new ToStringBuilder(InstanceBinding.class)
91 .add("key", getKey())
limpbizkit76c24b12008-12-25 04:32:41 +000092 .add("source", getSource())
limpbizkitc3f92842008-12-30 19:43:47 +000093 .add("instance", instance)
limpbizkit76c24b12008-12-25 04:32:41 +000094 .toString();
95 }
96}