blob: abea51d15fa1e898ae2ccf5d4103f528a2d56f73 [file] [log] [blame]
crazybobleefe2f1672007-01-22 04:16:18 +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.lang.reflect.Member;
20import 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 */
28class ExternalContext<T> implements Context {
29
30 final Member member;
31 final Key<T> key;
32 final ContainerImpl container;
crazyboblee15969212007-02-01 02:01:59 +000033 final int parameterIndex;
crazybobleefe2f1672007-01-22 04:16:18 +000034
crazyboblee15969212007-02-01 02:01:59 +000035 public ExternalContext(Member member, int paramterIndex, Key<T> key,
36 ContainerImpl container) {
crazybobleefe2f1672007-01-22 04:16:18 +000037 this.member = member;
38 this.key = key;
39 this.container = container;
crazyboblee15969212007-02-01 02:01:59 +000040 this.parameterIndex = paramterIndex;
crazybobleefe2f1672007-01-22 04:16:18 +000041 }
42
crazyboblee63b592b2007-01-25 02:45:24 +000043 public Key<?> getKey() {
44 return this.key;
crazybobleefe2f1672007-01-22 04:16:18 +000045 }
46
47 public Container getContainer() {
48 return container;
49 }
50
51 public Member getMember() {
52 return member;
53 }
54
crazyboblee15969212007-02-01 02:01:59 +000055 public int getParameterIndex() {
56 return parameterIndex;
57 }
58
crazybobleefe2f1672007-01-22 04:16:18 +000059 public String toString() {
60 return "Context" + new LinkedHashMap<String, Object>() {{
61 put("member", member);
crazyboblee63b592b2007-01-25 02:45:24 +000062 put("key", getKey());
crazybobleefe2f1672007-01-22 04:16:18 +000063 put("container", container);
64 }}.toString();
65 }
66
crazyboblee15969212007-02-01 02:01:59 +000067 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);
crazybobleefe2f1672007-01-22 04:16:18 +000075 }
76}