[LANG-586] clear ThreadLocal recursion registry (compatibly with existing tests, first pass)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@906673 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2010-02-04 21:46:22 +00:00
parent c98d0840bc
commit 5c3ec55e15
1 changed files with 103 additions and 89 deletions

View File

@ -19,9 +19,10 @@
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.HashSet;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.ObjectUtils;
@ -133,14 +134,7 @@ public abstract class ToStringStyle implements Serializable {
* to detect cyclical object references and avoid infinite loops.
* </p>
*/
private static final ThreadLocal<Set<Object>> registry = new ThreadLocal<Set<Object>>() {
@Override
protected Set<Object> initialValue() {
// The HashSet implementation is not synchronized,
// which is just what we need here.
return new HashSet<Object>();
}
};
private static final ThreadLocal<WeakHashMap<Object, Object>> REGISTRY = new ThreadLocal<WeakHashMap<Object,Object>>();
/**
* <p>
@ -151,7 +145,8 @@ protected Set<Object> initialValue() {
* @return Set the registry of objects being traversed
*/
static Set<Object> getRegistry() {
return registry.get();
WeakHashMap<Object, Object> m = REGISTRY.get();
return m == null ? Collections.<Object> emptySet() : m.keySet();
}
/**
@ -180,7 +175,15 @@ static boolean isRegistered(Object value) {
*/
static void register(Object value) {
if (value != null) {
getRegistry().add(value);
WeakHashMap<Object, Object> m;
synchronized (ToStringStyle.class) {
m = REGISTRY.get();
if (m == null) {
m = new WeakHashMap<Object, Object>();
REGISTRY.set(m);
}
}
m.put(value, null);
}
}
@ -197,7 +200,18 @@ static void register(Object value) {
* The object to unregister.
*/
static void unregister(Object value) {
getRegistry().remove(value);
if (value != null) {
WeakHashMap<Object, Object> m;
synchronized (ToStringStyle.class) {
m = REGISTRY.get();
if (m != null) {
m.remove(value);
if (m.isEmpty()) {
REGISTRY.remove();
}
}
}
}
}
/**