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 | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame^] | 24 | public enum Scopes implements Scope { |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame^] | 27 | * Instance per injection. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 28 | */ |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame^] | 29 | DEFAULT { |
| 30 | public <T> Factory<T> scope(Key<T> key, Factory<T> creator) { |
| 31 | return creator; |
| 32 | } |
| 33 | }, |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame^] | 36 | * Instance per container. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 37 | */ |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame^] | 38 | CONTAINER { |
| 39 | public <T> Factory<T> scope(Key<T> key, final Factory<T> creator) { |
| 40 | return new Factory<T>() { |
| 41 | |
| 42 | private volatile T instance; |
| 43 | |
| 44 | public T get() { |
| 45 | // Double checked locking improves performance and is safe as of Java 5. |
| 46 | if (instance == null) { |
| 47 | // Use a pretty coarse lock. We don't want to run into deadlocks when |
| 48 | // two threads try to load circularly-dependent objects. |
| 49 | // Maybe one of these days we will identify independent graphs of |
| 50 | // objects and offer to load them in parallel. |
| 51 | synchronized (Container.class) { |
| 52 | if (instance == null) { |
| 53 | instance = creator.get(); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | return instance; |
| 58 | } |
| 59 | |
| 60 | public String toString() { |
| 61 | return creator.toString(); |
| 62 | } |
| 63 | }; |
| 64 | } |
| 65 | } |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 66 | } |