01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.google.inject;
16:
17: import com.google.inject.BinderImpl.CreationListener;
18:
19: /**
20: * Delegates to a custom factory which is also bound in the injector.
21: */
22: class BoundProviderFactory<T> implements InternalFactory<T>,
23: CreationListener {
24:
25: final Key<? extends Provider<? extends T>> providerKey;
26: final Object source;
27: private InternalFactory<? extends Provider<? extends T>> providerFactory;
28:
29: BoundProviderFactory(
30: Key<? extends Provider<? extends T>> providerKey,
31: Object source) {
32: this .providerKey = providerKey;
33: this .source = source;
34: }
35:
36: BoundProviderFactory(
37: Key<? extends Provider<? extends T>> providerKey,
38: InternalFactory<? extends Provider<? extends T>> providerFactory,
39: Object source) {
40: this .providerKey = providerKey;
41: this .providerFactory = providerFactory;
42: this .source = source;
43: }
44:
45: public void notify(final InjectorImpl injector) {
46: injector.withDefaultSource(source, new Runnable() {
47: public void run() {
48: providerFactory = injector.getInternalFactory(null,
49: providerKey);
50: }
51: });
52: }
53:
54: public String toString() {
55: return providerKey.toString();
56: }
57:
58: public T get(InternalContext context) {
59: return providerFactory.get(context).get();
60: }
61: }
|