Home | History | Annotate | Download | only in inject
      1 /**
      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 
     17 package com.google.inject;
     18 
     19 /**
     20  * A scope is a level of visibility that instances provided by Guice may have.
     21  * By default, an instance created by the {@link Injector} has <i>no scope</i>,
     22  * meaning it has no state from the framework's perspective -- the
     23  * {@code Injector} creates it, injects it once into the class that required it,
     24  * and then immediately forgets it. Associating a scope with a particular
     25  * binding allows the created instance to be "remembered" and possibly used
     26  * again for other injections.
     27  *
     28  * <p>An example of a scope is {@link Scopes#SINGLETON}.
     29  *
     30  * @author crazybob (at) google.com (Bob Lee)
     31  */
     32 public interface Scope {
     33 
     34   /**
     35    * Scopes a provider. The returned provider returns objects from this scope.
     36    * If an object does not exist in this scope, the provider can use the given
     37    * unscoped provider to retrieve one.
     38    *
     39    * <p>Scope implementations are strongly encouraged to override
     40    * {@link Object#toString} in the returned provider and include the backing
     41    * provider's {@code toString()} output.
     42    *
     43    * @param key binding key
     44    * @param unscoped locates an instance when one doesn't already exist in this
     45    *  scope.
     46    * @return a new provider which only delegates to the given unscoped provider
     47    *  when an instance of the requested object doesn't already exist in this
     48    *  scope
     49    */
     50   public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped);
     51 
     52   /**
     53    * A short but useful description of this scope.  For comparison, the standard
     54    * scopes that ship with guice use the descriptions
     55    * {@code "Scopes.SINGLETON"}, {@code "ServletScopes.SESSION"} and
     56    * {@code "ServletScopes.REQUEST"}.
     57    */
     58   String toString();
     59 }
     60