blob: f897230ac14ae212333b4689536db97ba4891066 [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;
limpbizkit76c24b12008-12-25 04:32:41 +000021import com.google.inject.spi.BindingTargetVisitor;
22import com.google.inject.spi.LinkedKeyBinding;
23
limpbizkit5ae41eb2009-06-06 17:51:27 +000024final class LinkedBindingImpl<T> extends BindingImpl<T> implements LinkedKeyBinding<T> {
limpbizkit76c24b12008-12-25 04:32:41 +000025
26 final Key<? extends T> targetKey;
27
limpbizkit5ae41eb2009-06-06 17:51:27 +000028 public LinkedBindingImpl(InjectorImpl injector, Key<T> key, Object source,
limpbizkit76c24b12008-12-25 04:32:41 +000029 InternalFactory<? extends T> internalFactory, Scoping scoping,
30 Key<? extends T> targetKey) {
31 super(injector, key, source, internalFactory, scoping);
32 this.targetKey = targetKey;
33 }
34
35 public LinkedBindingImpl(Object source, Key<T> key, Scoping scoping, Key<? extends T> targetKey) {
36 super(source, key, scoping);
37 this.targetKey = targetKey;
38 }
39
limpbizkit8996e802008-12-28 01:44:29 +000040 public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000041 return visitor.visit(this);
limpbizkit76c24b12008-12-25 04:32:41 +000042 }
43
44 public Key<? extends T> getLinkedKey() {
45 return targetKey;
46 }
47
48 public BindingImpl<T> withScoping(Scoping scoping) {
49 return new LinkedBindingImpl<T>(getSource(), getKey(), scoping, targetKey);
50 }
51
52 public BindingImpl<T> withKey(Key<T> key) {
53 return new LinkedBindingImpl<T>(getSource(), key, getScoping(), targetKey);
54 }
55
limpbizkit03b81a62009-03-18 05:34:39 +000056 public void applyTo(Binder binder) {
57 getScoping().applyTo(binder.withSource(getSource()).bind(getKey()).to(getLinkedKey()));
58 }
59
limpbizkit76c24b12008-12-25 04:32:41 +000060 @Override public String toString() {
61 return new ToStringBuilder(LinkedKeyBinding.class)
62 .add("key", getKey())
limpbizkit76c24b12008-12-25 04:32:41 +000063 .add("source", getSource())
limpbizkitc3f92842008-12-30 19:43:47 +000064 .add("scope", getScoping())
65 .add("target", targetKey)
limpbizkit76c24b12008-12-25 04:32:41 +000066 .toString();
67 }
68}