Home | History | Annotate | Download | only in spi
      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 
     24 import java.lang.annotation.Annotation;
     25 
     26 /**
     27  * Registration of a scope annotation with the scope that implements it. Instances are created
     28  * explicitly in a module using {@link com.google.inject.Binder#bindScope(Class, Scope) bindScope()}
     29  * statements:
     30  * <pre>
     31  *     Scope recordScope = new RecordScope();
     32  *     bindScope(RecordScoped.class, new RecordScope());</pre>
     33  *
     34  * @author jessewilson (at) 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 
     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   public Object getSource() {
     49     return source;
     50   }
     51 
     52   public Class<? extends Annotation> getAnnotationType() {
     53     return annotationType;
     54   }
     55 
     56   public Scope getScope() {
     57     return scope;
     58   }
     59 
     60   public <T> T acceptVisitor(ElementVisitor<T> visitor) {
     61     return visitor.visit(this);
     62   }
     63 
     64   public void applyTo(Binder binder) {
     65     binder.withSource(getSource()).bindScope(annotationType, scope);
     66   }
     67 }
     68