blob: a0b358372f8acd17255f9b722f6af680b5f5f3fe [file] [log] [blame]
limpbizkit76c24b12008-12-25 04:32:41 +00001/**
2 * Copyright (C) 2008 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.internal;
18
19import com.google.inject.Scope;
20import com.google.inject.Scopes;
21import com.google.inject.Singleton;
22import com.google.inject.Stage;
limpbizkit03b81a62009-03-18 05:34:39 +000023import com.google.inject.binder.ScopedBindingBuilder;
limpbizkit76c24b12008-12-25 04:32:41 +000024import com.google.inject.spi.BindingScopingVisitor;
25import java.lang.annotation.Annotation;
26
27/**
28 * References a scope, either directly (as a scope instance), or indirectly (as a scope annotation).
29 * The scope's eager or laziness is also exposed.
30 *
31 * @author jessewilson@google.com (Jesse Wilson)
32 */
33public abstract class Scoping {
34
35 /**
36 * No scoping annotation has been applied. Note that this is different from {@code
37 * in(Scopes.NO_SCOPE)}, where the 'NO_SCOPE' has been explicitly applied.
38 */
39 public static final Scoping UNSCOPED = new Scoping() {
40 public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
41 return visitor.visitNoScoping();
42 }
43
44 @Override public Scope getScopeInstance() {
45 return Scopes.NO_SCOPE;
46 }
47
48 @Override public String toString() {
49 return Scopes.NO_SCOPE.toString();
50 }
limpbizkit03b81a62009-03-18 05:34:39 +000051
52 public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
53 // do nothing
54 }
limpbizkit76c24b12008-12-25 04:32:41 +000055 };
56
57 public static final Scoping SINGLETON_ANNOTATION = new Scoping() {
58 public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
59 return visitor.visitScopeAnnotation(Singleton.class);
60 }
61
62 @Override public Class<? extends Annotation> getScopeAnnotation() {
63 return Singleton.class;
64 }
65
66 @Override public String toString() {
67 return Singleton.class.getName();
68 }
limpbizkit03b81a62009-03-18 05:34:39 +000069
70 public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
71 scopedBindingBuilder.in(Singleton.class);
72 }
limpbizkit76c24b12008-12-25 04:32:41 +000073 };
74
75 public static final Scoping SINGLETON_INSTANCE = new Scoping() {
76 public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
77 return visitor.visitScope(Scopes.SINGLETON);
78 }
79
80 @Override public Scope getScopeInstance() {
81 return Scopes.SINGLETON;
82 }
83
84 @Override public String toString() {
85 return Scopes.SINGLETON.toString();
86 }
limpbizkit03b81a62009-03-18 05:34:39 +000087
88 public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
89 scopedBindingBuilder.in(Scopes.SINGLETON);
90 }
limpbizkit76c24b12008-12-25 04:32:41 +000091 };
92
93 public static final Scoping EAGER_SINGLETON = new Scoping() {
94 public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
95 return visitor.visitEagerSingleton();
96 }
97
98 @Override public Scope getScopeInstance() {
99 return Scopes.SINGLETON;
100 }
101
102 @Override public String toString() {
103 return "eager singleton";
104 }
limpbizkit03b81a62009-03-18 05:34:39 +0000105
106 public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
107 scopedBindingBuilder.asEagerSingleton();
108 }
limpbizkit76c24b12008-12-25 04:32:41 +0000109 };
110
111 public static Scoping forAnnotation(final Class<? extends Annotation> scopingAnnotation) {
112 if (scopingAnnotation == Singleton.class) {
113 return SINGLETON_ANNOTATION;
114 }
115
116 return new Scoping() {
117 public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
118 return visitor.visitScopeAnnotation(scopingAnnotation);
119 }
120
121 @Override public Class<? extends Annotation> getScopeAnnotation() {
122 return scopingAnnotation;
123 }
124
125 @Override public String toString() {
126 return scopingAnnotation.getName();
127 }
limpbizkit03b81a62009-03-18 05:34:39 +0000128
129 public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
130 scopedBindingBuilder.in(scopingAnnotation);
131 }
limpbizkit76c24b12008-12-25 04:32:41 +0000132 };
133 }
134
135 public static Scoping forInstance(final Scope scope) {
136 if (scope == Scopes.SINGLETON) {
137 return SINGLETON_INSTANCE;
138 }
139
140 return new Scoping() {
141 public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
142 return visitor.visitScope(scope);
143 }
144
145 @Override public Scope getScopeInstance() {
146 return scope;
147 }
148
149 @Override public String toString() {
150 return scope.toString();
151 }
limpbizkit03b81a62009-03-18 05:34:39 +0000152
153 public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
154 scopedBindingBuilder.in(scope);
155 }
limpbizkit76c24b12008-12-25 04:32:41 +0000156 };
157 }
158
159 /**
160 * Returns true if this scope was explicitly applied. If no scope was explicitly applied then the
161 * scoping annotation will be used.
162 */
163 public boolean isExplicitlyScoped() {
164 return this != UNSCOPED;
165 }
166
167 /**
168 * Returns true if this is the default scope. In this case a new instance will be provided for
169 * each injection.
170 */
171 public boolean isNoScope() {
172 return getScopeInstance() == Scopes.NO_SCOPE;
173 }
174
175 /**
176 * Returns true if this scope is a singleton that should be loaded eagerly in {@code stage}.
177 */
178 public boolean isEagerSingleton(Stage stage) {
179 if (this == EAGER_SINGLETON) {
180 return true;
181 }
182
183 if (stage == Stage.PRODUCTION) {
184 return this == SINGLETON_ANNOTATION || this == SINGLETON_INSTANCE;
185 }
186
187 return false;
188 }
189
190 /**
191 * Returns the scope instance, or {@code null} if that isn't known for this instance.
192 */
193 public Scope getScopeInstance() {
194 return null;
195 }
196
197 /**
198 * Returns the scope annotation, or {@code null} if that isn't known for this instance.
199 */
200 public Class<? extends Annotation> getScopeAnnotation() {
201 return null;
202 }
203
204 public abstract <V> V acceptVisitor(BindingScopingVisitor<V> visitor);
205
limpbizkit03b81a62009-03-18 05:34:39 +0000206 public abstract void applyTo(ScopedBindingBuilder scopedBindingBuilder);
207
limpbizkit76c24b12008-12-25 04:32:41 +0000208 private Scoping() {}
209}