crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.google.inject; |
| 18 | |
| 19 | import java.util.HashMap; |
| 20 | import 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 | */ |
| 28 | class InternalContext { |
| 29 | |
| 30 | final ContainerImpl container; |
| 31 | final Map<Object, ConstructionContext<?>> constructionContexts = |
| 32 | new HashMap<Object, ConstructionContext<?>>(); |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame^] | 33 | final Scope.Strategy scopeStrategy; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 34 | ExternalContext<?> externalContext; |
| 35 | |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame^] | 36 | InternalContext(ContainerImpl container, Scope.Strategy scopeStrategy) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 37 | this.container = container; |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame^] | 38 | this.scopeStrategy = scopeStrategy; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 39 | } |
| 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) { |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame^] | 51 | throw new IllegalStateException("Scope strategy not set. " |
| 52 | + "Please call Container.setScopeStrategy()."); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 53 | } |
| 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 | } |