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 | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 19 | import com.google.inject.internal.InjectorBuilder; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 20 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 21 | /** |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 22 | * Built-in scope implementations. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 23 | * |
| 24 | * @author crazybob@google.com (Bob Lee) |
| 25 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 26 | public class Scopes { |
| 27 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 28 | private Scopes() {} |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
crazyboblee | 61257a8 | 2007-03-03 00:23:40 +0000 | [diff] [blame] | 31 | * One instance per {@link Injector}. Also see {@code @}{@link Singleton}. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 32 | */ |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 33 | public static final Scope SINGLETON = new Scope() { |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 34 | public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) { |
| 35 | return new Provider<T>() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 36 | |
| 37 | private volatile T instance; |
| 38 | |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 39 | // DCL on a volatile is safe as of Java 5, which we obviously require. |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 40 | @SuppressWarnings("DoubleCheckedLocking") |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 41 | public T get() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 42 | if (instance == null) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Use a pretty coarse lock. We don't want to run into deadlocks |
| 45 | * when two threads try to load circularly-dependent objects. |
| 46 | * Maybe one of these days we will identify independent graphs of |
| 47 | * objects and offer to load them in parallel. |
| 48 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 49 | synchronized (InjectorBuilder.class) { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 50 | if (instance == null) { |
| 51 | instance = creator.get(); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | return instance; |
| 56 | } |
| 57 | |
| 58 | public String toString() { |
kevinb9n | da11d0d | 2007-04-20 15:58:38 +0000 | [diff] [blame] | 59 | return String.format("%s[%s]", creator, SINGLETON); |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 60 | } |
| 61 | }; |
| 62 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 63 | |
limpbizkit | c9465f9 | 2008-05-31 19:01:54 +0000 | [diff] [blame] | 64 | @Override public String toString() { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 65 | return "Scopes.SINGLETON"; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 66 | } |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 67 | }; |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
kevinb9n | b605482 | 2007-03-19 03:17:47 +0000 | [diff] [blame] | 70 | * No scope; the same as not applying any scope at all. Each time the |
| 71 | * Injector obtains an instance of an object with "no scope", it injects this |
| 72 | * instance then immediately forgets it. When the next request for the same |
| 73 | * binding arrives it will need to obtain the instance over again. |
| 74 | * |
| 75 | * <p>This exists only in case a class has been annotated with a scope |
| 76 | * annotation such as {@link Singleton @Singleton}, and you need to override |
| 77 | * this to "no scope" in your binding. |
limpbizkit | c489adf | 2008-11-18 07:01:33 +0000 | [diff] [blame] | 78 | * |
| 79 | * @since 2.0 |
kevinb9n | b605482 | 2007-03-19 03:17:47 +0000 | [diff] [blame] | 80 | */ |
| 81 | public static final Scope NO_SCOPE = new Scope() { |
| 82 | public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) { |
| 83 | return unscoped; |
| 84 | } |
limpbizkit | c9465f9 | 2008-05-31 19:01:54 +0000 | [diff] [blame] | 85 | @Override public String toString() { |
kevinb9n | b605482 | 2007-03-19 03:17:47 +0000 | [diff] [blame] | 86 | return "Scopes.NO_SCOPE"; |
| 87 | } |
| 88 | }; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 89 | } |