crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 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 | */ |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 19 | /** |
kevinb9n | 5a33f38 | 2007-02-27 07:38:03 +0000 | [diff] [blame] | 20 | * A scope is a level of visibility that instances provided by Guice may have. |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame^] | 21 | * 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. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 26 | * |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame^] | 27 | * @see Scopes#SINGLETON |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 28 | * |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 29 | * @author crazybob@google.com (Bob Lee) |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 30 | */ |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 31 | public interface Scope { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 34 | * 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. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 37 | * |
kevinb9n | 5a33f38 | 2007-02-27 07:38:03 +0000 | [diff] [blame] | 38 | * <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 | * |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 42 | * @param key binding key |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame] | 43 | * @param unscoped locates an instance when one doesn't already exist in this |
| 44 | * scope. |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 45 | * @return a new provider which only delegates to the given unscoped provider |
crazyboblee | 5746d5d | 2007-02-18 21:52:24 +0000 | [diff] [blame] | 46 | * when an instance of the requested object doesn't already exist in this |
| 47 | * scope |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 48 | */ |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 49 | public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped); |
kevinb9n | 5a33f38 | 2007-02-27 07:38:03 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * A short but useful description of this scope. For comparison, the standard |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame^] | 53 | * scopes that ship with guice use the descriptions |
| 54 | * {@code "Scopes.SINGLETON"}, {@code "ServletScopes.SESSION"} and |
| 55 | * {@code "ServletScopes.REQUEST"}. |
kevinb9n | 5a33f38 | 2007-02-27 07:38:03 +0000 | [diff] [blame] | 56 | */ |
| 57 | String toString(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 58 | } |