blob: 1c256c2083c3bbf0a09af3d70b5d5b4939abcca7 [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
crazybobleeb052dd82007-01-25 22:33:56 +000019import java.util.HashMap;
kevinb9na99dca72007-02-11 04:48:57 +000020import java.util.Map;
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
crazyboblee66b415a2006-08-25 02:01:19 +000038 ContainerImpl getContainerImpl() {
39 return container;
40 }
41
crazyboblee66b415a2006-08-25 02:01:19 +000042 @SuppressWarnings("unchecked")
43 <T> ConstructionContext<T> getConstructionContext(Object key) {
crazybobleeb052dd82007-01-25 22:33:56 +000044 if (constructionContexts == null) {
45 constructionContexts = new HashMap<Object, ConstructionContext<?>>();
46 ConstructionContext<T> constructionContext = new ConstructionContext<T>();
crazyboblee66b415a2006-08-25 02:01:19 +000047 constructionContexts.put(key, constructionContext);
crazybobleeb052dd82007-01-25 22:33:56 +000048 return constructionContext;
kevinb9na99dca72007-02-11 04:48:57 +000049 }
50 else {
51 ConstructionContext<T> constructionContext
52 = (ConstructionContext<T>) constructionContexts.get(key);
crazybobleeb052dd82007-01-25 22:33:56 +000053 if (constructionContext == null) {
54 constructionContext = new ConstructionContext<T>();
55 constructionContexts.put(key, constructionContext);
56 }
57 return constructionContext;
crazyboblee66b415a2006-08-25 02:01:19 +000058 }
crazyboblee66b415a2006-08-25 02:01:19 +000059 }
60
61 @SuppressWarnings("unchecked")
62 <T> ExternalContext<T> getExternalContext() {
63 return (ExternalContext<T>) externalContext;
64 }
65
66 void setExternalContext(ExternalContext<?> externalContext) {
67 this.externalContext = externalContext;
68 }
69}