blob: 322e06527bb30d90736a71e3e262c865c37eb5c8 [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
19import java.util.HashMap;
20import java.util.Map;
21
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;
31 final Map<Object, ConstructionContext<?>> constructionContexts =
32 new HashMap<Object, ConstructionContext<?>>();
crazyboblee07e41822006-11-21 01:27:08 +000033 final Scope.Strategy scopeStrategy;
crazyboblee66b415a2006-08-25 02:01:19 +000034 ExternalContext<?> externalContext;
35
crazyboblee07e41822006-11-21 01:27:08 +000036 InternalContext(ContainerImpl container, Scope.Strategy scopeStrategy) {
crazyboblee66b415a2006-08-25 02:01:19 +000037 this.container = container;
crazyboblee07e41822006-11-21 01:27:08 +000038 this.scopeStrategy = scopeStrategy;
crazyboblee66b415a2006-08-25 02:01:19 +000039 }
40
41 public Container getContainer() {
42 return container;
43 }
44
45 ContainerImpl getContainerImpl() {
46 return container;
47 }
48
49 Scope.Strategy getScopeStrategy() {
50 if (scopeStrategy == null) {
crazyboblee07e41822006-11-21 01:27:08 +000051 throw new IllegalStateException("Scope strategy not set. "
52 + "Please call Container.setScopeStrategy().");
crazyboblee66b415a2006-08-25 02:01:19 +000053 }
54
55 return scopeStrategy;
56 }
57
58 @SuppressWarnings("unchecked")
59 <T> ConstructionContext<T> getConstructionContext(Object key) {
60 ConstructionContext<T> constructionContext =
61 (ConstructionContext<T>) constructionContexts.get(key);
62 if (constructionContext == null) {
63 constructionContext = new ConstructionContext<T>();
64 constructionContexts.put(key, constructionContext);
65 }
66 return constructionContext;
67 }
68
69 @SuppressWarnings("unchecked")
70 <T> ExternalContext<T> getExternalContext() {
71 return (ExternalContext<T>) externalContext;
72 }
73
74 void setExternalContext(ExternalContext<?> externalContext) {
75 this.externalContext = externalContext;
76 }
77}