crazyboblee | fe2f167 | 2007-01-22 04:16:18 +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.lang.reflect.Member; |
| 20 | import java.util.LinkedHashMap; |
| 21 | |
| 22 | /** |
| 23 | * An immutable snapshot of the current context which is safe to |
| 24 | * expose to client code. |
| 25 | * |
| 26 | * @author crazybob@google.com (Bob Lee) |
| 27 | */ |
| 28 | class ExternalContext<T> implements Context { |
| 29 | |
| 30 | final Member member; |
| 31 | final Key<T> key; |
| 32 | final ContainerImpl container; |
crazyboblee | 1596921 | 2007-02-01 02:01:59 +0000 | [diff] [blame] | 33 | final int parameterIndex; |
crazyboblee | fe2f167 | 2007-01-22 04:16:18 +0000 | [diff] [blame] | 34 | |
crazyboblee | 1596921 | 2007-02-01 02:01:59 +0000 | [diff] [blame] | 35 | public ExternalContext(Member member, int paramterIndex, Key<T> key, |
| 36 | ContainerImpl container) { |
crazyboblee | fe2f167 | 2007-01-22 04:16:18 +0000 | [diff] [blame] | 37 | this.member = member; |
| 38 | this.key = key; |
| 39 | this.container = container; |
crazyboblee | 1596921 | 2007-02-01 02:01:59 +0000 | [diff] [blame] | 40 | this.parameterIndex = paramterIndex; |
crazyboblee | fe2f167 | 2007-01-22 04:16:18 +0000 | [diff] [blame] | 41 | } |
| 42 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 43 | public Key<?> getKey() { |
| 44 | return this.key; |
crazyboblee | fe2f167 | 2007-01-22 04:16:18 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public Container getContainer() { |
| 48 | return container; |
| 49 | } |
| 50 | |
| 51 | public Member getMember() { |
| 52 | return member; |
| 53 | } |
| 54 | |
crazyboblee | 1596921 | 2007-02-01 02:01:59 +0000 | [diff] [blame] | 55 | public int getParameterIndex() { |
| 56 | return parameterIndex; |
| 57 | } |
| 58 | |
crazyboblee | fe2f167 | 2007-01-22 04:16:18 +0000 | [diff] [blame] | 59 | public String toString() { |
| 60 | return "Context" + new LinkedHashMap<String, Object>() {{ |
| 61 | put("member", member); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 62 | put("key", getKey()); |
crazyboblee | fe2f167 | 2007-01-22 04:16:18 +0000 | [diff] [blame] | 63 | put("container", container); |
| 64 | }}.toString(); |
| 65 | } |
| 66 | |
crazyboblee | 1596921 | 2007-02-01 02:01:59 +0000 | [diff] [blame] | 67 | static <T> ExternalContext<T> newInstance(Member member, |
| 68 | Key<T> key, ContainerImpl container) { |
| 69 | return new ExternalContext<T>(member, -1, key, container); |
| 70 | } |
| 71 | |
| 72 | static <T> ExternalContext<T> newInstance(Member member, int parameterIndex, |
| 73 | Key<T> key, ContainerImpl container) { |
| 74 | return new ExternalContext<T>(member, parameterIndex, key, container); |
crazyboblee | fe2f167 | 2007-01-22 04:16:18 +0000 | [diff] [blame] | 75 | } |
| 76 | } |