KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > BoundProviderFactory


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;
18
19 import com.google.inject.BinderImpl.CreationListener;
20
21 /**
22  * Delegates to a custom factory which is also bound in the injector.
23  */

24 class BoundProviderFactory<T>
25     implements InternalFactory<T>, CreationListener {
26
27   final Key<? extends Provider<? extends T>> providerKey;
28   final Object JavaDoc source;
29   private InternalFactory<? extends Provider<? extends T>> providerFactory;
30
31   BoundProviderFactory(
32       Key<? extends Provider<? extends T>> providerKey,
33       Object JavaDoc source) {
34     this.providerKey = providerKey;
35     this.source = source;
36   }
37
38   BoundProviderFactory(
39       Key<? extends Provider<? extends T>> providerKey,
40       InternalFactory<? extends Provider<? extends T>> providerFactory,
41       Object JavaDoc source) {
42     this.providerKey = providerKey;
43     this.providerFactory = providerFactory;
44     this.source = source;
45   }
46
47   public void notify(final InjectorImpl injector) {
48     injector.withDefaultSource(source, new Runnable JavaDoc() {
49       public void run() {
50         providerFactory = injector.getInternalFactory(null, providerKey);
51       }
52     });
53   }
54
55   public String JavaDoc toString() {
56     return providerKey.toString();
57   }
58
59   public T get(InternalContext context) {
60     return providerFactory.get(context).get();
61   }
62 }
63
Popular Tags