limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.google.inject.internal; |
| 18 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 19 | import com.google.inject.Binder; |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 20 | import com.google.inject.Key; |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.base.Objects; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.BindingTargetVisitor; |
sberlin | cba3a51 | 2010-04-26 01:52:19 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.Dependency; |
| 25 | import com.google.inject.spi.HasDependencies; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 26 | import com.google.inject.spi.LinkedKeyBinding; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 27 | import java.util.Set; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 28 | |
sberlin | cba3a51 | 2010-04-26 01:52:19 +0000 | [diff] [blame] | 29 | public final class LinkedBindingImpl<T> extends BindingImpl<T> implements LinkedKeyBinding<T>, HasDependencies { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 30 | |
| 31 | final Key<? extends T> targetKey; |
| 32 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 33 | public LinkedBindingImpl(InjectorImpl injector, Key<T> key, Object source, |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 34 | 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 | |
limpbizkit | 8996e80 | 2008-12-28 01:44:29 +0000 | [diff] [blame] | 45 | public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 46 | return visitor.visit(this); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public Key<? extends T> getLinkedKey() { |
| 50 | return targetKey; |
| 51 | } |
sberlin | cba3a51 | 2010-04-26 01:52:19 +0000 | [diff] [blame] | 52 | |
| 53 | public Set<Dependency<?>> getDependencies() { |
| 54 | return ImmutableSet.<Dependency<?>>of(Dependency.get(targetKey)); |
| 55 | } |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 56 | |
| 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 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 65 | public void applyTo(Binder binder) { |
| 66 | getScoping().applyTo(binder.withSource(getSource()).bind(getKey()).to(getLinkedKey())); |
| 67 | } |
| 68 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 69 | @Override public String toString() { |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 70 | return Objects.toStringHelper(LinkedKeyBinding.class) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 71 | .add("key", getKey()) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 72 | .add("source", getSource()) |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 73 | .add("scope", getScoping()) |
| 74 | .add("target", targetKey) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 75 | .toString(); |
| 76 | } |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 77 | |
| 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 | } |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 94 | } |