limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 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.Binding; |
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.BindingScopingVisitor; |
| 23 | import com.google.inject.spi.ElementVisitor; |
| 24 | import com.google.inject.spi.InstanceBinding; |
| 25 | |
| 26 | /** |
| 27 | * @author crazybob@google.com (Bob Lee) |
| 28 | */ |
| 29 | public abstract class BindingImpl<T> implements Binding<T> { |
| 30 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 31 | private final InjectorImpl injector; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 32 | private final Key<T> key; |
| 33 | private final Object source; |
| 34 | private final Scoping scoping; |
| 35 | private final InternalFactory<? extends T> internalFactory; |
| 36 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 37 | public BindingImpl(InjectorImpl injector, Key<T> key, Object source, |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 38 | InternalFactory<? extends T> internalFactory, Scoping scoping) { |
| 39 | this.injector = injector; |
| 40 | this.key = key; |
| 41 | this.source = source; |
| 42 | this.internalFactory = internalFactory; |
| 43 | this.scoping = scoping; |
| 44 | } |
| 45 | |
| 46 | protected BindingImpl(Object source, Key<T> key, Scoping scoping) { |
| 47 | this.internalFactory = null; |
| 48 | this.injector = null; |
| 49 | this.source = source; |
| 50 | this.key = key; |
| 51 | this.scoping = scoping; |
| 52 | } |
| 53 | |
| 54 | public Key<T> getKey() { |
| 55 | return key; |
| 56 | } |
| 57 | |
| 58 | public Object getSource() { |
| 59 | return source; |
| 60 | } |
| 61 | |
| 62 | private volatile Provider<T> provider; |
| 63 | |
| 64 | public Provider<T> getProvider() { |
| 65 | if (provider == null) { |
| 66 | if (injector == null) { |
| 67 | throw new UnsupportedOperationException("getProvider() not supported for module bindings"); |
| 68 | } |
| 69 | |
| 70 | provider = injector.getProvider(key); |
| 71 | } |
| 72 | return provider; |
| 73 | } |
| 74 | |
| 75 | public InternalFactory<? extends T> getInternalFactory() { |
| 76 | return internalFactory; |
| 77 | } |
| 78 | |
| 79 | public Scoping getScoping() { |
| 80 | return scoping; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Is this a constant binding? This returns true for constant bindings as |
| 85 | * well as toInstance() bindings. |
| 86 | */ |
| 87 | public boolean isConstant() { |
| 88 | return this instanceof InstanceBinding; |
| 89 | } |
| 90 | |
| 91 | public <V> V acceptVisitor(ElementVisitor<V> visitor) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 92 | return visitor.visit(this); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | public <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor) { |
| 96 | return scoping.acceptVisitor(visitor); |
| 97 | } |
| 98 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 99 | protected BindingImpl<T> withScoping(Scoping scoping) { |
| 100 | throw new AssertionError(); |
| 101 | } |
| 102 | |
| 103 | protected BindingImpl<T> withKey(Key<T> key) { |
| 104 | throw new AssertionError(); |
| 105 | } |
| 106 | |
| 107 | @Override public String toString() { |
| 108 | return new ToStringBuilder(Binding.class) |
| 109 | .add("key", key) |
| 110 | .add("scope", scoping) |
| 111 | .add("source", source) |
| 112 | .toString(); |
| 113 | } |
| 114 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 115 | public InjectorImpl getInjector() { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 116 | return injector; |
| 117 | } |
| 118 | } |