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