KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * Internal context. Used to coordinate injections and support circular
24  * dependencies.
25  *
26  * @author crazybob@google.com (Bob Lee)
27  */

28 class InternalContext {
29
30   final InjectorImpl injector;
31   Map JavaDoc<Object JavaDoc, ConstructionContext<?>> constructionContexts;
32   ExternalContext<?> externalContext;
33
34   InternalContext(InjectorImpl injector) {
35     this.injector = injector;
36   }
37
38   InjectorImpl getInjectorImpl() {
39     return injector;
40   }
41
42   @SuppressWarnings JavaDoc("unchecked")
43   <T> ConstructionContext<T> getConstructionContext(Object JavaDoc key) {
44     if (constructionContexts == null) {
45       constructionContexts = new HashMap JavaDoc<Object JavaDoc, ConstructionContext<?>>();
46       ConstructionContext<T> constructionContext = new ConstructionContext<T>();
47       constructionContexts.put(key, constructionContext);
48       return constructionContext;
49     }
50     else {
51       ConstructionContext<T> constructionContext
52           = (ConstructionContext<T>) constructionContexts.get(key);
53       if (constructionContext == null) {
54         constructionContext = new ConstructionContext<T>();
55         constructionContexts.put(key, constructionContext);
56       }
57       return constructionContext;
58     }
59   }
60
61   @SuppressWarnings JavaDoc("unchecked")
62   <T> ExternalContext<T> getExternalContext() {
63     return (ExternalContext<T>) externalContext;
64   }
65
66   Class JavaDoc<?> getExpectedType() {
67     return externalContext.getKey().getRawType();
68   }
69
70   void setExternalContext(ExternalContext<?> externalContext) {
71     this.externalContext = externalContext;
72   }
73 }
74
Popular Tags