blob: 4b35c390f98893e990f8f690c455e834bc04eeed [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
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;
kevinb9na99dca72007-02-11 04:48:57 +000053 }
54 else {
55 ConstructionContext<T> constructionContext
56 = (ConstructionContext<T>) constructionContexts.get(key);
crazybobleeb052dd82007-01-25 22:33:56 +000057 if (constructionContext == null) {
58 constructionContext = new ConstructionContext<T>();
59 constructionContexts.put(key, constructionContext);
60 }
61 return constructionContext;
crazyboblee66b415a2006-08-25 02:01:19 +000062 }
crazyboblee66b415a2006-08-25 02:01:19 +000063 }
64
65 @SuppressWarnings("unchecked")
66 <T> ExternalContext<T> getExternalContext() {
67 return (ExternalContext<T>) externalContext;
68 }
69
70 void setExternalContext(ExternalContext<?> externalContext) {
71 this.externalContext = externalContext;
72 }
73}