lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uschind...@apache.org
Subject svn commit: r826532 - /lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java
Date Sun, 18 Oct 2009 21:44:01 GMT
Author: uschindler
Date: Sun Oct 18 21:44:01 2009
New Revision: 826532

URL: http://svn.apache.org/viewvc?rev=826532&view=rev
Log:
LUCENE-1257: Generify CloseableThreadLocal

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java?rev=826532&r1=826531&r2=826532&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/util/CloseableThreadLocal.java Sun Oct 18
21:44:01 2009
@@ -42,20 +42,20 @@
  *  references are cleared and then GC is freely able to
  *  reclaim space by objects stored in it. */
 
-public class CloseableThreadLocal {
+public class CloseableThreadLocal<T> {
 
-  private ThreadLocal t = new ThreadLocal();
+  private ThreadLocal<WeakReference<T>> t = new ThreadLocal<WeakReference<T>>();
 
-  private Map hardRefs = new HashMap();
+  private Map<Thread,T> hardRefs = new HashMap<Thread,T>();
   
-  protected Object initialValue() {
+  protected T initialValue() {
     return null;
   }
   
-  public Object get() {
-    WeakReference weakRef = (WeakReference) t.get();
+  public T get() {
+    WeakReference<T> weakRef = t.get();
     if (weakRef == null) {
-      Object iv = initialValue();
+      T iv = initialValue();
       if (iv != null) {
         set(iv);
         return iv;
@@ -66,17 +66,16 @@
     }
   }
 
-  public void set(Object object) {
+  public void set(T object) {
 
-    t.set(new WeakReference(object));
+    t.set(new WeakReference<T>(object));
 
     synchronized(hardRefs) {
       hardRefs.put(Thread.currentThread(), object);
 
       // Purge dead threads
-      Iterator it = hardRefs.keySet().iterator();
-      while(it.hasNext()) {
-        Thread t = (Thread) it.next();
+      for (Iterator<Thread> it = hardRefs.keySet().iterator(); it.hasNext();) {
+        final Thread t = it.next();
         if (!t.isAlive())
           it.remove();
       }



Mime
View raw message