lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r695899 - in /lucene/java/trunk/src: java/org/apache/lucene/util/CloseableThreadLocal.java test/org/apache/lucene/index/TestCloseableThreadLocal.java
Date Tue, 16 Sep 2008 14:24:17 GMT
Author: mikemccand
Date: Tue Sep 16 07:24:17 2008
New Revision: 695899

URL: http://svn.apache.org/viewvc?rev=695899&view=rev
Log:
LUCENE-1388: add initialValue() method to CloseableThreadLocal

Added:
    lucene/java/trunk/src/test/org/apache/lucene/index/TestCloseableThreadLocal.java   (with
props)
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=695899&r1=695898&r2=695899&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 Tue Sep 16
07:24:17 2008
@@ -42,17 +42,26 @@
  *  references are cleared and then GC is freely able to
  *  reclaim space by objects stored in it. */
 
-public final class CloseableThreadLocal {
+public class CloseableThreadLocal {
 
   private ThreadLocal t = new ThreadLocal();
 
   private Map hardRefs = new HashMap();
-
+  
+  protected Object initialValue() {
+    return null;
+  }
+  
   public Object get() {
     WeakReference weakRef = (WeakReference) t.get();
-    if (weakRef == null)
-      return null;
-    else {
+    if (weakRef == null) {
+      Object iv = initialValue();
+      if (iv != null) {
+        set(iv);
+        return iv;
+      } else
+        return null;
+    } else {
       Object v = weakRef.get();
       // This can never be null, because we hold a hard
       // reference to the underlying object:

Added: lucene/java/trunk/src/test/org/apache/lucene/index/TestCloseableThreadLocal.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestCloseableThreadLocal.java?rev=695899&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestCloseableThreadLocal.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestCloseableThreadLocal.java Tue Sep
16 07:24:17 2008
@@ -0,0 +1,20 @@
+package org.apache.lucene.index;
+
+import org.apache.lucene.util.CloseableThreadLocal;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestCloseableThreadLocal extends LuceneTestCase {
+  public static final String TEST_VALUE = "initvaluetest";
+  
+  public void testInitValue() {
+    InitValueThreadLocal tl = new InitValueThreadLocal();
+    String str = (String)tl.get();
+    assertEquals(TEST_VALUE, str);
+  }
+  
+  public class InitValueThreadLocal extends CloseableThreadLocal {
+    protected Object initialValue() {
+      return TEST_VALUE;
+    } 
+  }
+}

Propchange: lucene/java/trunk/src/test/org/apache/lucene/index/TestCloseableThreadLocal.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message