[LANG-586] Clear ThreadLocal for HashCodeBuilder as well

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@907373 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2010-02-07 03:14:09 +00:00
parent 2fb9e71da6
commit 1f35e7c99b
2 changed files with 20 additions and 11 deletions

View File

@ -101,14 +101,7 @@ public class HashCodeBuilder {
*
* @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 @@ public class HashCodeBuilder {
* @since 2.3
*/
static Set<IDKey> getRegistry() {
return registry.get();
return REGISTRY.get();
}
/**
@ -151,7 +144,8 @@ public class HashCodeBuilder {
* @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 @@ public class HashCodeBuilder {
* 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 @@ public class HashCodeBuilder {
* @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();
}
}
}
}
/**

View File

@ -521,7 +521,9 @@ public class HashCodeBuilderTest extends TestCase {
// at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:422)
a.hashCode();
assertNull(HashCodeBuilder.getRegistry());
b.hashCode();
assertNull(HashCodeBuilder.getRegistry());
}
/**