• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.google.inject.spi;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import com.google.inject.Binder;
22 import com.google.inject.Scope;
23 import java.lang.annotation.Annotation;
24 
25 /**
26  * Registration of a scope annotation with the scope that implements it. Instances are created
27  * explicitly in a module using {@link com.google.inject.Binder#bindScope(Class, Scope) bindScope()}
28  * statements:
29  *
30  * <pre>
31  *     Scope recordScope = new RecordScope();
32  *     bindScope(RecordScoped.class, new RecordScope());</pre>
33  *
34  * @author jessewilson@google.com (Jesse Wilson)
35  * @since 2.0
36  */
37 public final class ScopeBinding implements Element {
38   private final Object source;
39   private final Class<? extends Annotation> annotationType;
40   private final Scope scope;
41 
ScopeBinding(Object source, Class<? extends Annotation> annotationType, Scope scope)42   ScopeBinding(Object source, Class<? extends Annotation> annotationType, Scope scope) {
43     this.source = checkNotNull(source, "source");
44     this.annotationType = checkNotNull(annotationType, "annotationType");
45     this.scope = checkNotNull(scope, "scope");
46   }
47 
48   @Override
getSource()49   public Object getSource() {
50     return source;
51   }
52 
getAnnotationType()53   public Class<? extends Annotation> getAnnotationType() {
54     return annotationType;
55   }
56 
getScope()57   public Scope getScope() {
58     return scope;
59   }
60 
61   @Override
acceptVisitor(ElementVisitor<T> visitor)62   public <T> T acceptVisitor(ElementVisitor<T> visitor) {
63     return visitor.visit(this);
64   }
65 
66   @Override
applyTo(Binder binder)67   public void applyTo(Binder binder) {
68     binder.withSource(getSource()).bindScope(annotationType, scope);
69   }
70 }
71