commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r907373 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java
Date Sun, 07 Feb 2010 03:14:10 GMT
Author: mbenson
Date: Sun Feb  7 03:14:09 2010
New Revision: 907373

URL: http://svn.apache.org/viewvc?rev=907373&view=rev
Log:
[LANG-586] Clear ThreadLocal for HashCodeBuilder as well

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java?rev=907373&r1=907372&r2=907373&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java
Sun Feb  7 03:14:09 2010
@@ -101,14 +101,7 @@
      * 
      * @since 2.3
      */
-    private static final ThreadLocal<Set<IDKey>> registry = new ThreadLocal<Set<IDKey>>()
{
-        @Override
-        protected Set<IDKey> initialValue() {
-            // The HashSet implementation is not synchronized,
-            // which is just what we need here.
-            return new HashSet<IDKey>();
-        }
-    };
+    private static final ThreadLocal<Set<IDKey>> REGISTRY = new ThreadLocal<Set<IDKey>>();
 
     /*
      * N.B. we cannot store the actual objects in a HashSet, as that would use the very hashCode()
@@ -136,7 +129,7 @@
      * @since 2.3
      */
     static Set<IDKey> getRegistry() {
-        return registry.get();
+        return REGISTRY.get();
     }
 
     /**
@@ -151,7 +144,8 @@
      * @since 2.3
      */
     static boolean isRegistered(Object value) {
-        return getRegistry().contains(new IDKey(value));
+        Set<IDKey> registry = getRegistry();
+        return registry != null && registry.contains(new IDKey(value));
     }
 
     /**
@@ -521,6 +515,11 @@
      *            The object to register.
      */
     static void register(Object value) {
+        synchronized (HashCodeBuilder.class) {
+            if (getRegistry() == null) {
+                REGISTRY.set(new HashSet<IDKey>());
+            }
+        }
         getRegistry().add(new IDKey(value));
     }
 
@@ -537,7 +536,15 @@
      * @since 2.3
      */
     static void unregister(Object value) {
-        getRegistry().remove(new IDKey(value));
+        Set<IDKey> s = getRegistry();
+        if (s != null) {
+            s.remove(new IDKey(value));
+            synchronized (HashCodeBuilder.class) {
+                if (s.isEmpty()) {
+                    REGISTRY.remove();
+                }
+            }
+        }
     }
 
     /**

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java?rev=907373&r1=907372&r2=907373&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java
Sun Feb  7 03:14:09 2010
@@ -521,7 +521,9 @@
         // at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:422)
 
         a.hashCode();
+        assertNull(HashCodeBuilder.getRegistry());
         b.hashCode();
+        assertNull(HashCodeBuilder.getRegistry());
     }
 
     /**



Mime
View raw message