blob: 86a6ca3f18da43df520d7f2a6f21b3e782af0fb2 [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 */
crazyboblee63b592b2007-01-25 02:45:24 +000016
17package com.google.inject;
18
19/**
crazybobleee5fbbb02007-02-05 07:00:27 +000020 * Built in scope implementations.
crazyboblee63b592b2007-01-25 02:45:24 +000021 *
22 * @author crazybob@google.com (Bob Lee)
23 */
crazyboblee68d2f4b2007-02-07 18:47:24 +000024public class Scopes {
25
kevinb9na99dca72007-02-11 04:48:57 +000026 private Scopes() {
27 }
crazyboblee63b592b2007-01-25 02:45:24 +000028
29 /**
crazyboblee68d2f4b2007-02-07 18:47:24 +000030 * Name of the default scope.
crazyboblee63b592b2007-01-25 02:45:24 +000031 */
crazyboblee68d2f4b2007-02-07 18:47:24 +000032 public static final String DEFAULT_NAME = "DEFAULT";
33
34 /**
35 * The default scope, one instance per injection.
36 */
37 public static final Scope DEFAULT = new Scope() {
crazybobleee5fbbb02007-02-05 07:00:27 +000038 public <T> Factory<T> scope(Key<T> key, Factory<T> creator) {
39 return creator;
40 }
crazyboblee68d2f4b2007-02-07 18:47:24 +000041 };
42
43 /**
44 * Name of container scope.
45 */
46 public static final String CONTAINER_NAME = "CONTAINER";
crazyboblee63b592b2007-01-25 02:45:24 +000047
48 /**
crazybobleebaaaf2d2007-02-07 03:35:09 +000049 * One instance per container.
crazyboblee63b592b2007-01-25 02:45:24 +000050 */
crazyboblee68d2f4b2007-02-07 18:47:24 +000051 public static final Scope CONTAINER = new Scope() {
crazybobleee5fbbb02007-02-05 07:00:27 +000052 public <T> Factory<T> scope(Key<T> key, final Factory<T> creator) {
53 return new Factory<T>() {
54
55 private volatile T instance;
56
kevinb9na99dca72007-02-11 04:48:57 +000057 // DCL on a volatile is safe as of Java 5, which we obviously require
kevinb9n6a565c72007-02-11 01:58:33 +000058 @SuppressWarnings("DoubleCheckedLocking")
crazybobleee5fbbb02007-02-05 07:00:27 +000059 public T get() {
crazybobleee5fbbb02007-02-05 07:00:27 +000060 if (instance == null) {
kevinb9na99dca72007-02-11 04:48:57 +000061 /*
62 * Use a pretty coarse lock. We don't want to run into deadlocks
63 * when two threads try to load circularly-dependent objects.
64 * Maybe one of these days we will identify independent graphs of
65 * objects and offer to load them in parallel.
66 */
crazybobleee5fbbb02007-02-05 07:00:27 +000067 synchronized (Container.class) {
68 if (instance == null) {
69 instance = creator.get();
70 }
71 }
72 }
73 return instance;
74 }
75
76 public String toString() {
77 return creator.toString();
78 }
79 };
80 }
crazyboblee68d2f4b2007-02-07 18:47:24 +000081 };
kevinb9na99dca72007-02-11 04:48:57 +000082}