blob: 59510b37bcd47f43f36beafb6d00c53a26757a25 [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.
kevinb9na2915a92007-02-28 06:20:30 +000021 * By default, an instance created by the {@link Injector} has <i>no scope</i>,
22 * meaning it has no visibility -- the Injector creates it, injects it once
23 * into the class that required it, then immediately forgets it. Associating a
24 * scope with a particular binding allows the created instance to be
25 * "remembered" and possibly used again for other injections.
crazyboblee66b415a2006-08-25 02:01:19 +000026 *
kevinb9na2915a92007-02-28 06:20:30 +000027 * @see Scopes#SINGLETON
crazybobleea6e73982007-02-02 00:21:07 +000028 *
crazyboblee63b592b2007-01-25 02:45:24 +000029 * @author crazybob@google.com (Bob Lee)
crazyboblee66b415a2006-08-25 02:01:19 +000030 */
crazyboblee63b592b2007-01-25 02:45:24 +000031public interface Scope {
crazyboblee66b415a2006-08-25 02:01:19 +000032
33 /**
crazybobleebd9544e2007-02-25 20:32:11 +000034 * Scopes a provider. The returned locator returns objects from this scope. If
35 * an object does not exist in this scope, the provider can use the given
36 * unscoped provider to retrieve one.
crazyboblee66b415a2006-08-25 02:01:19 +000037 *
kevinb9n5a33f382007-02-27 07:38:03 +000038 * <p>Scope implementations are strongly encouraged to override
39 * {@link Object#toString} in the returned provider and include the backing
40 * provider's {@code toString()} output.
41 *
crazyboblee63b592b2007-01-25 02:45:24 +000042 * @param key binding key
crazyboblee5746d5d2007-02-18 21:52:24 +000043 * @param unscoped locates an instance when one doesn't already exist in this
44 * scope.
crazybobleebd9544e2007-02-25 20:32:11 +000045 * @return a new provider which only delegates to the given unscoped provider
crazyboblee5746d5d2007-02-18 21:52:24 +000046 * when an instance of the requested object doesn't already exist in this
47 * scope
crazyboblee66b415a2006-08-25 02:01:19 +000048 */
crazybobleebd9544e2007-02-25 20:32:11 +000049 public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped);
kevinb9n5a33f382007-02-27 07:38:03 +000050
51 /**
52 * A short but useful description of this scope. For comparison, the standard
kevinb9na2915a92007-02-28 06:20:30 +000053 * scopes that ship with guice use the descriptions
54 * {@code "Scopes.SINGLETON"}, {@code "ServletScopes.SESSION"} and
55 * {@code "ServletScopes.REQUEST"}.
kevinb9n5a33f382007-02-27 07:38:03 +000056 */
57 String toString();
crazyboblee66b415a2006-08-25 02:01:19 +000058}