blob: b316fce0c55cc34e7b217e030d1b130f2e2ca161 [file] [log] [blame]
limpbizkit477f9f92008-07-28 07:05:14 +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.spi;
18
limpbizkit03b81a62009-03-18 05:34:39 +000019import com.google.inject.Binder;
limpbizkit@gmail.com9a227be2010-07-03 15:51:31 +000020import com.google.inject.Scope;
sberlind9c913a2011-06-26 21:02:54 +000021import static com.google.common.base.Preconditions.checkNotNull;
limpbizkit477f9f92008-07-28 07:05:14 +000022import java.lang.annotation.Annotation;
23
24/**
limpbizkit00ca9f72008-08-02 17:56:17 +000025 * Registration of a scope annotation with the scope that implements it. Instances are created
26 * explicitly in a module using {@link com.google.inject.Binder#bindScope(Class, Scope) bindScope()}
27 * statements:
28 * <pre>
29 * Scope recordScope = new RecordScope();
30 * bindScope(RecordScoped.class, new RecordScope());</pre>
limpbizkit477f9f92008-07-28 07:05:14 +000031 *
32 * @author jessewilson@google.com (Jesse Wilson)
limpbizkitc489adf2008-11-18 07:01:33 +000033 * @since 2.0
limpbizkit477f9f92008-07-28 07:05:14 +000034 */
limpbizkit00ca9f72008-08-02 17:56:17 +000035public final class ScopeBinding implements Element {
limpbizkit477f9f92008-07-28 07:05:14 +000036 private final Object source;
37 private final Class<? extends Annotation> annotationType;
38 private final Scope scope;
39
limpbizkit00ca9f72008-08-02 17:56:17 +000040 ScopeBinding(Object source, Class<? extends Annotation> annotationType, Scope scope) {
limpbizkit477f9f92008-07-28 07:05:14 +000041 this.source = checkNotNull(source, "source");
42 this.annotationType = checkNotNull(annotationType, "annotationType");
43 this.scope = checkNotNull(scope, "scope");
44 }
45
46 public Object getSource() {
47 return source;
48 }
49
50 public Class<? extends Annotation> getAnnotationType() {
51 return annotationType;
52 }
53
54 public Scope getScope() {
55 return scope;
56 }
57
limpbizkitafa4b5d2008-08-02 18:40:47 +000058 public <T> T acceptVisitor(ElementVisitor<T> visitor) {
limpbizkit03b81a62009-03-18 05:34:39 +000059 return visitor.visit(this);
60 }
61
62 public void applyTo(Binder binder) {
63 binder.withSource(getSource()).bindScope(annotationType, scope);
limpbizkit477f9f92008-07-28 07:05:14 +000064 }
65}