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 java.util.HashMap;
18: import java.util.Map;
19:
20: /**
21: * Internal context. Used to coordinate injections and support circular
22: * dependencies.
23: *
24: * @author crazybob@google.com (Bob Lee)
25: */
26: class InternalContext {
27:
28: final InjectorImpl injector;
29: Map<Object, ConstructionContext<?>> constructionContexts;
30: ExternalContext<?> externalContext;
31:
32: InternalContext(InjectorImpl injector) {
33: this .injector = injector;
34: }
35:
36: InjectorImpl getInjectorImpl() {
37: return injector;
38: }
39:
40: @SuppressWarnings("unchecked")
41: <T> ConstructionContext<T> getConstructionContext(Object key) {
42: if (constructionContexts == null) {
43: constructionContexts = new HashMap<Object, ConstructionContext<?>>();
44: ConstructionContext<T> constructionContext = new ConstructionContext<T>();
45: constructionContexts.put(key, constructionContext);
46: return constructionContext;
47: } else {
48: ConstructionContext<T> constructionContext = (ConstructionContext<T>) constructionContexts
49: .get(key);
50: if (constructionContext == null) {
51: constructionContext = new ConstructionContext<T>();
52: constructionContexts.put(key, constructionContext);
53: }
54: return constructionContext;
55: }
56: }
57:
58: @SuppressWarnings("unchecked")
59: <T> ExternalContext<T> getExternalContext() {
60: return (ExternalContext<T>) externalContext;
61: }
62:
63: Class<?> getExpectedType() {
64: return externalContext.getKey().getRawType();
65: }
66:
67: void setExternalContext(ExternalContext<?> externalContext) {
68: this.externalContext = externalContext;
69: }
70: }
|