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 | |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 19 | import java.util.HashMap; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame^] | 20 | import java.util.Map; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 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; |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 31 | Map<Object, ConstructionContext<?>> constructionContexts; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 32 | ExternalContext<?> externalContext; |
| 33 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 34 | InternalContext(ContainerImpl container) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 35 | this.container = container; |
| 36 | } |
| 37 | |
| 38 | public Container getContainer() { |
| 39 | return container; |
| 40 | } |
| 41 | |
| 42 | ContainerImpl getContainerImpl() { |
| 43 | return container; |
| 44 | } |
| 45 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 46 | @SuppressWarnings("unchecked") |
| 47 | <T> ConstructionContext<T> getConstructionContext(Object key) { |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 48 | if (constructionContexts == null) { |
| 49 | constructionContexts = new HashMap<Object, ConstructionContext<?>>(); |
| 50 | ConstructionContext<T> constructionContext = new ConstructionContext<T>(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 51 | constructionContexts.put(key, constructionContext); |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 52 | return constructionContext; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame^] | 53 | } |
| 54 | else { |
| 55 | ConstructionContext<T> constructionContext |
| 56 | = (ConstructionContext<T>) constructionContexts.get(key); |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 57 | if (constructionContext == null) { |
| 58 | constructionContext = new ConstructionContext<T>(); |
| 59 | constructionContexts.put(key, constructionContext); |
| 60 | } |
| 61 | return constructionContext; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 62 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 63 | } |
| 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 | } |