blob: dca03424a07f01c707e74f3e701004f558e82634 [file] [log] [blame]
limpbizkit76c24b12008-12-25 04:32:41 +00001/*
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
17package com.google.inject.internal;
18
limpbizkit03b81a62009-03-18 05:34:39 +000019import com.google.inject.Binder;
limpbizkit5ae41eb2009-06-06 17:51:27 +000020import com.google.inject.Key;
sberlind9c913a2011-06-26 21:02:54 +000021import com.google.common.collect.ImmutableSet;
22import com.google.common.base.Objects;
limpbizkit76c24b12008-12-25 04:32:41 +000023import com.google.inject.spi.BindingTargetVisitor;
sberlincba3a512010-04-26 01:52:19 +000024import com.google.inject.spi.Dependency;
25import com.google.inject.spi.HasDependencies;
limpbizkit76c24b12008-12-25 04:32:41 +000026import com.google.inject.spi.LinkedKeyBinding;
limpbizkit@gmail.com9a227be2010-07-03 15:51:31 +000027import java.util.Set;
limpbizkit76c24b12008-12-25 04:32:41 +000028
sberlincba3a512010-04-26 01:52:19 +000029public final class LinkedBindingImpl<T> extends BindingImpl<T> implements LinkedKeyBinding<T>, HasDependencies {
limpbizkit76c24b12008-12-25 04:32:41 +000030
31 final Key<? extends T> targetKey;
32
limpbizkit5ae41eb2009-06-06 17:51:27 +000033 public LinkedBindingImpl(InjectorImpl injector, Key<T> key, Object source,
limpbizkit76c24b12008-12-25 04:32:41 +000034 InternalFactory<? extends T> internalFactory, Scoping scoping,
35 Key<? extends T> targetKey) {
36 super(injector, key, source, internalFactory, scoping);
37 this.targetKey = targetKey;
38 }
39
40 public LinkedBindingImpl(Object source, Key<T> key, Scoping scoping, Key<? extends T> targetKey) {
41 super(source, key, scoping);
42 this.targetKey = targetKey;
43 }
44
limpbizkit8996e802008-12-28 01:44:29 +000045 public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000046 return visitor.visit(this);
limpbizkit76c24b12008-12-25 04:32:41 +000047 }
48
49 public Key<? extends T> getLinkedKey() {
50 return targetKey;
51 }
sberlincba3a512010-04-26 01:52:19 +000052
53 public Set<Dependency<?>> getDependencies() {
54 return ImmutableSet.<Dependency<?>>of(Dependency.get(targetKey));
55 }
limpbizkit76c24b12008-12-25 04:32:41 +000056
57 public BindingImpl<T> withScoping(Scoping scoping) {
58 return new LinkedBindingImpl<T>(getSource(), getKey(), scoping, targetKey);
59 }
60
61 public BindingImpl<T> withKey(Key<T> key) {
62 return new LinkedBindingImpl<T>(getSource(), key, getScoping(), targetKey);
63 }
64
limpbizkit03b81a62009-03-18 05:34:39 +000065 public void applyTo(Binder binder) {
66 getScoping().applyTo(binder.withSource(getSource()).bind(getKey()).to(getLinkedKey()));
67 }
68
limpbizkit76c24b12008-12-25 04:32:41 +000069 @Override public String toString() {
sberlind9c913a2011-06-26 21:02:54 +000070 return Objects.toStringHelper(LinkedKeyBinding.class)
limpbizkit76c24b12008-12-25 04:32:41 +000071 .add("key", getKey())
limpbizkit76c24b12008-12-25 04:32:41 +000072 .add("source", getSource())
limpbizkitc3f92842008-12-30 19:43:47 +000073 .add("scope", getScoping())
74 .add("target", targetKey)
limpbizkit76c24b12008-12-25 04:32:41 +000075 .toString();
76 }
sberlin@gmail.com7bcec882010-06-24 01:03:26 +000077
78 @Override
79 public boolean equals(Object obj) {
80 if(obj instanceof LinkedBindingImpl) {
81 LinkedBindingImpl<?> o = (LinkedBindingImpl<?>)obj;
82 return getKey().equals(o.getKey())
83 && getScoping().equals(o.getScoping())
84 && Objects.equal(targetKey, o.targetKey);
85 } else {
86 return false;
87 }
88 }
89
90 @Override
91 public int hashCode() {
92 return Objects.hashCode(getKey(), getScoping(), targetKey);
93 }
limpbizkit76c24b12008-12-25 04:32:41 +000094}