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 | |
| 19 | /** |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 20 | * Built in scope implementations. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 21 | * |
| 22 | * @author crazybob@google.com (Bob Lee) |
| 23 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 24 | public class Scopes { |
| 25 | |
| 26 | private Scopes() {} |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 29 | * Name of the default scope. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 30 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 31 | public static final String DEFAULT_NAME = "DEFAULT"; |
| 32 | |
| 33 | /** |
| 34 | * The default scope, one instance per injection. |
| 35 | */ |
| 36 | public static final Scope DEFAULT = new Scope() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 37 | public <T> Factory<T> scope(Key<T> key, Factory<T> creator) { |
| 38 | return creator; |
| 39 | } |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Name of container scope. |
| 44 | */ |
| 45 | public static final String CONTAINER_NAME = "CONTAINER"; |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
crazyboblee | baaaf2d | 2007-02-07 03:35:09 +0000 | [diff] [blame] | 48 | * One instance per container. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 49 | */ |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 50 | public static final Scope CONTAINER = new Scope() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 51 | public <T> Factory<T> scope(Key<T> key, final Factory<T> creator) { |
| 52 | return new Factory<T>() { |
| 53 | |
| 54 | private volatile T instance; |
| 55 | |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame^] | 56 | // DCL is safe as of Java 5, which we obviously require |
| 57 | @SuppressWarnings("DoubleCheckedLocking") |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 58 | public T get() { |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 59 | if (instance == null) { |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 60 | // Use a pretty coarse lock. We don't want to run into deadlocks |
| 61 | // when two threads try to load circularly-dependent objects. |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 62 | // Maybe one of these days we will identify independent graphs of |
| 63 | // objects and offer to load them in parallel. |
| 64 | synchronized (Container.class) { |
| 65 | if (instance == null) { |
| 66 | instance = creator.get(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | return instance; |
| 71 | } |
| 72 | |
| 73 | public String toString() { |
| 74 | return creator.toString(); |
| 75 | } |
| 76 | }; |
| 77 | } |
crazyboblee | 68d2f4b | 2007-02-07 18:47:24 +0000 | [diff] [blame] | 78 | }; |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 79 | } |