blob: e8184f94860aa971740a5768815220fdacb6d633 [file] [log] [blame]
crazyboblee66b415a2006-08-25 02:01:19 +00001/**
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
17package com.google.inject;
18
crazyboblee66b415a2006-08-25 02:01:19 +000019import java.util.Map;
crazybobleeb052dd82007-01-25 22:33:56 +000020import java.util.HashMap;
crazyboblee66b415a2006-08-25 02:01:19 +000021
22/**
23 * Internal context. Used to coordinate injections and support circular
24 * dependencies.
25 *
26 * @author crazybob@google.com (Bob Lee)
27 */
28class InternalContext {
29
30 final ContainerImpl container;
crazybobleeb052dd82007-01-25 22:33:56 +000031 Map<Object, ConstructionContext<?>> constructionContexts;
crazyboblee66b415a2006-08-25 02:01:19 +000032 ExternalContext<?> externalContext;
33
crazyboblee63b592b2007-01-25 02:45:24 +000034 InternalContext(ContainerImpl container) {
crazyboblee66b415a2006-08-25 02:01:19 +000035 this.container = container;
36 }
37
38 public Container getContainer() {
39 return container;
40 }
41
42 ContainerImpl getContainerImpl() {
43 return container;
44 }
45
crazyboblee66b415a2006-08-25 02:01:19 +000046 @SuppressWarnings("unchecked")
47 <T> ConstructionContext<T> getConstructionContext(Object key) {
crazybobleeb052dd82007-01-25 22:33:56 +000048 if (constructionContexts == null) {
49 constructionContexts = new HashMap<Object, ConstructionContext<?>>();
50 ConstructionContext<T> constructionContext = new ConstructionContext<T>();
crazyboblee66b415a2006-08-25 02:01:19 +000051 constructionContexts.put(key, constructionContext);
crazybobleeb052dd82007-01-25 22:33:56 +000052 return constructionContext;
53 } else {
54 ConstructionContext<T> constructionContext =
55 (ConstructionContext<T>) constructionContexts.get(key);
56 if (constructionContext == null) {
57 constructionContext = new ConstructionContext<T>();
58 constructionContexts.put(key, constructionContext);
59 }
60 return constructionContext;
crazyboblee66b415a2006-08-25 02:01:19 +000061 }
crazyboblee66b415a2006-08-25 02:01:19 +000062 }
63
64 @SuppressWarnings("unchecked")
65 <T> ExternalContext<T> getExternalContext() {
66 return (ExternalContext<T>) externalContext;
67 }
68
69 void setExternalContext(ExternalContext<?> externalContext) {
70 this.externalContext = externalContext;
71 }
72}