Use Java 8 API to manage thread local
This commit is contained in:
parent
8f73bd15d4
commit
155abe805b
|
@ -118,7 +118,7 @@ public class HashCodeBuilder implements Builder<Integer> {
|
|||
*
|
||||
* @since 2.3
|
||||
*/
|
||||
private static final ThreadLocal<Set<IDKey>> REGISTRY = new ThreadLocal<>();
|
||||
private static final ThreadLocal<Set<IDKey>> REGISTRY = ThreadLocal.withInitial(HashSet::new);
|
||||
|
||||
/*
|
||||
* NOTE: we cannot store the actual objects in a HashSet, as that would use the very hashCode()
|
||||
|
@ -467,12 +467,7 @@ public class HashCodeBuilder implements Builder<Integer> {
|
|||
* The object to register.
|
||||
*/
|
||||
private static void register(final Object value) {
|
||||
Set<IDKey> registry = getRegistry();
|
||||
if (registry == null) {
|
||||
registry = new HashSet<>();
|
||||
REGISTRY.set(registry);
|
||||
}
|
||||
registry.add(new IDKey(value));
|
||||
getRegistry().add(new IDKey(value));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -480,6 +475,7 @@ public class HashCodeBuilder implements Builder<Integer> {
|
|||
*
|
||||
* <p>
|
||||
* Used by the reflection methods to avoid infinite loops.
|
||||
* </p>
|
||||
*
|
||||
* @param value
|
||||
* The object to unregister.
|
||||
|
@ -487,11 +483,9 @@ public class HashCodeBuilder implements Builder<Integer> {
|
|||
*/
|
||||
private static void unregister(final Object value) {
|
||||
final Set<IDKey> registry = getRegistry();
|
||||
if (registry != null) {
|
||||
registry.remove(new IDKey(value));
|
||||
if (registry.isEmpty()) {
|
||||
REGISTRY.remove();
|
||||
}
|
||||
registry.remove(new IDKey(value));
|
||||
if (registry.isEmpty()) {
|
||||
REGISTRY.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,8 +19,8 @@ package org.apache.commons.lang3.builder;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.apache.commons.lang3.AbstractLangTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -563,9 +563,9 @@ public class HashCodeBuilderTest extends AbstractLangTest {
|
|||
// at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:422)
|
||||
|
||||
a.hashCode();
|
||||
assertNull(HashCodeBuilder.getRegistry());
|
||||
assertTrue(HashCodeBuilder.getRegistry().isEmpty());
|
||||
b.hashCode();
|
||||
assertNull(HashCodeBuilder.getRegistry());
|
||||
assertTrue(HashCodeBuilder.getRegistry().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue