package com.google.inject.scopes;

import java.util.Map;
import java.util.WeakHashMap;

import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Scope;

public class ThreadLocalScope implements Scope {

	private Map<String, Object> context = new WeakHashMap<String, Object>();

	private ThreadLocalScope() {
		super();
	}

	/**
	 * Thread Local scope.
	 */
	public static final Scope THREAD_LOCAL = new ThreadLocalScope();

	/** A sentinel attribute value representing null. */
	private enum NullObject {
		INSTANCE
	}

	@Override
	public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) {
		final String name = key.toString();
		return new Provider<T>() {
			@SuppressWarnings("unchecked")
			public T get() {
				Object obj = context.get(name);
				if (NullObject.INSTANCE == obj)
					return null;
				ThreadLocal threadLocal = (ThreadLocal) obj;
				if (threadLocal == null) {
					threadLocal = new ThreadLocal();
				}
				T t = (T) threadLocal.get();
				if (t == null) {
					t = creator.get();
					threadLocal.set(t);
					context.put(name, (t != null) ? threadLocal
							: NullObject.INSTANCE);
				}
				return t;
			}
		};
	}
}

@@ -34,62 +34,8 @@
 %0D%0A%0D%0A
-import java.util.Map;%0D%0Aimport java.util.WeakHashMap;%0D%0A
 %0D%0Aim
@@ -131,16 +131,108 @@
 .Scope;%0D
+%0Aimport java.util.concurrent.ConcurrentHashMap;%0D%0Aimport java.util.concurrent.ConcurrentMap;%0D
 %0A%0D%0Apubli
@@ -278,25 +278,28 @@
 pe %7B%0D%0A%0D%0A
-%09
+    
 private 
 Map%3CStri
@@ -290,16 +290,26 @@
 private 
+Concurrent
 Map%3CStri
@@ -338,12 +338,18 @@
 new 
-Weak
+Concurrent
 Hash
@@ -374,17 +374,20 @@
 %3E();%0D%0A%0D%0A
-%09
+    
 private 
@@ -408,18 +408,24 @@
 pe() %7B%0D%0A
-%09%09
+        
 super();
@@ -430,21 +430,28 @@
 );%0D%0A
-%09%7D%0D%0A%0D%0A%09
+    %7D%0D%0A    
 /**%0D%0A
-%09
+    
  * T
@@ -474,15 +474,21 @@
 e.%0D%0A
-%09
+    
  */%0D%0A
-%09
+    
 publ
@@ -551,17 +551,20 @@
 e();%0D%0A%0D%0A
-%09
+    
 /** A se
@@ -609,17 +609,20 @@
 ll. */%0D%0A
-%09
+    
 private 
@@ -640,18 +640,26 @@
 ject %7B%0D%0A
-%09%09
+%0D%0A        
 INSTANCE
@@ -664,15 +664,21 @@
 CE%0D%0A
-%09
+    
 %7D%0D%0A%0D%0A
-%09
+    
 @Ove
@@ -684,17 +684,20 @@
 erride%0D%0A
-%09
+    
 public %3C
@@ -759,18 +759,24 @@
 tor) %7B%0D%0A
-%09%09
+        
 final St
@@ -804,18 +804,24 @@
 ing();%0D%0A
-%09%09
+        
 return n
@@ -840,19 +840,30 @@
 T%3E() %7B%0D%0A
-%09%09%09
+%0D%0A            
 @Suppres
@@ -886,19 +886,51 @@
 cked%22)%0D%0A
-%09%09%09
+            @Override%0D%0A            
 public T
@@ -939,20 +939,32 @@
 et() %7B%0D%0A
-%09%09%09%09
+                
 Object o
@@ -984,28 +984,40 @@
 get(name);%0D%0A
-%09%09%09%09
+                
 if (NullObje
@@ -1039,33 +1039,81 @@
 obj)
-%0D%0A%09%09%09%09%09return null;%0D%0A%09%09%09%09
+ %7B%0D%0A                    return null;%0D%0A                %7D%0D%0A                
 Thre
@@ -1154,20 +1154,32 @@
 ) obj;%0D%0A
-%09%09%09%09
+                
 if (thre
@@ -1198,21 +1198,36 @@
 ull) %7B%0D%0A
-%09%09%09%09%09
+                    
 threadLo
@@ -1252,27 +1252,51 @@
 cal();%0D%0A
-%09%09%09%09%7D%0D%0A%09%09%09%09
+                %7D%0D%0A                
 T t = (T
@@ -1317,20 +1317,32 @@
 get();%0D%0A
-%09%09%09%09
+                
 if (t ==
@@ -1355,38 +1355,68 @@
  %7B%0D%0A
-%09%09%09%09%09t = creator.get();%0D%0A%09%09%09%09%09
+                    t = creator.get();%0D%0A                    
 thre
@@ -1432,21 +1432,36 @@
 et(t);%0D%0A
-%09%09%09%09%09
+                    
 context.
@@ -1501,75 +1501,138 @@
 al%0D%0A
-%09%09%09%09%09%09%09: NullObject.INSTANCE);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09return t;%0D%0A%09%09%09%7D%0D%0A%09%09%7D;%0D%0A%09
+                            : NullObject.INSTANCE);%0D%0A                %7D%0D%0A                return t;%0D%0A            %7D%0D%0A        %7D;%0D%0A    
 %7D%0D%0A%7D

change to concurrentMap