blob: 949270c812935d6fa4595c4b5d31b9fa77ba7f63 [file] [log] [blame]
crazybobleeabc4dd02007-02-01 01:44:36 +00001/**
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 */
crazyboblee66b415a2006-08-25 02:01:19 +000016
17package com.google.inject;
18
crazyboblee66b415a2006-08-25 02:01:19 +000019/**
kevinb9n5a33f382007-02-27 07:38:03 +000020 * A scope is a level of visibility that instances provided by Guice may have.
kevinb9n9fd9fe62007-05-15 16:49:48 +000021 * 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
crazybobleec1d0c642007-03-07 17:20:22 +000023 * {@code Injector} creates it, injects it once into the class that required it,
kevinb9n9fd9fe62007-05-15 16:49:48 +000024 * 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.
crazyboblee66b415a2006-08-25 02:01:19 +000027 *
kevinb9n9fd9fe62007-05-15 16:49:48 +000028 * <p>An example of a scope is {@link Scopes#SINGLETON}.
crazybobleea6e73982007-02-02 00:21:07 +000029 *
crazyboblee63b592b2007-01-25 02:45:24 +000030 * @author crazybob@google.com (Bob Lee)
crazyboblee66b415a2006-08-25 02:01:19 +000031 */
crazyboblee63b592b2007-01-25 02:45:24 +000032public interface Scope {
crazyboblee66b415a2006-08-25 02:01:19 +000033
34 /**
kevinb9n9fd9fe62007-05-15 16:49:48 +000035 * 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
crazybobleebd9544e2007-02-25 20:32:11 +000037 * unscoped provider to retrieve one.
crazyboblee66b415a2006-08-25 02:01:19 +000038 *
kevinb9n5a33f382007-02-27 07:38:03 +000039 * <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 *
crazyboblee63b592b2007-01-25 02:45:24 +000043 * @param key binding key
crazyboblee5746d5d2007-02-18 21:52:24 +000044 * @param unscoped locates an instance when one doesn't already exist in this
45 * scope.
crazybobleebd9544e2007-02-25 20:32:11 +000046 * @return a new provider which only delegates to the given unscoped provider
crazyboblee5746d5d2007-02-18 21:52:24 +000047 * when an instance of the requested object doesn't already exist in this
48 * scope
crazyboblee66b415a2006-08-25 02:01:19 +000049 */
crazybobleebd9544e2007-02-25 20:32:11 +000050 public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped);
kevinb9n5a33f382007-02-27 07:38:03 +000051
52 /**
53 * A short but useful description of this scope. For comparison, the standard
kevinb9na2915a92007-02-28 06:20:30 +000054 * scopes that ship with guice use the descriptions
55 * {@code "Scopes.SINGLETON"}, {@code "ServletScopes.SESSION"} and
56 * {@code "ServletScopes.REQUEST"}.
kevinb9n5a33f382007-02-27 07:38:03 +000057 */
58 String toString();
crazyboblee66b415a2006-08-25 02:01:19 +000059}