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 | |
| 19 | import com.google.inject.Injector; |
| 20 | import com.google.inject.Key; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 21 | import com.google.inject.Binder; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.BindingTargetVisitor; |
| 23 | import com.google.inject.spi.LinkedKeyBinding; |
| 24 | |
| 25 | public final class LinkedBindingImpl<T> extends BindingImpl<T> implements LinkedKeyBinding<T> { |
| 26 | |
| 27 | final Key<? extends T> targetKey; |
| 28 | |
| 29 | public LinkedBindingImpl(Injector injector, Key<T> key, Object source, |
| 30 | InternalFactory<? extends T> internalFactory, Scoping scoping, |
| 31 | Key<? extends T> targetKey) { |
| 32 | super(injector, key, source, internalFactory, scoping); |
| 33 | this.targetKey = targetKey; |
| 34 | } |
| 35 | |
| 36 | public LinkedBindingImpl(Object source, Key<T> key, Scoping scoping, Key<? extends T> targetKey) { |
| 37 | super(source, key, scoping); |
| 38 | this.targetKey = targetKey; |
| 39 | } |
| 40 | |
limpbizkit | 8996e80 | 2008-12-28 01:44:29 +0000 | [diff] [blame] | 41 | public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 42 | return visitor.visit(this); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | public Key<? extends T> getLinkedKey() { |
| 46 | return targetKey; |
| 47 | } |
| 48 | |
| 49 | public BindingImpl<T> withScoping(Scoping scoping) { |
| 50 | return new LinkedBindingImpl<T>(getSource(), getKey(), scoping, targetKey); |
| 51 | } |
| 52 | |
| 53 | public BindingImpl<T> withKey(Key<T> key) { |
| 54 | return new LinkedBindingImpl<T>(getSource(), key, getScoping(), targetKey); |
| 55 | } |
| 56 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 57 | public void applyTo(Binder binder) { |
| 58 | getScoping().applyTo(binder.withSource(getSource()).bind(getKey()).to(getLinkedKey())); |
| 59 | } |
| 60 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 61 | @Override public String toString() { |
| 62 | return new ToStringBuilder(LinkedKeyBinding.class) |
| 63 | .add("key", getKey()) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 64 | .add("source", getSource()) |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 65 | .add("scope", getScoping()) |
| 66 | .add("target", targetKey) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 67 | .toString(); |
| 68 | } |
| 69 | } |