Use Java 8 API to manage thread local

This commit is contained in:
Gary Gregory 2024-03-16 08:40:57 -04:00
parent 8f73bd15d4
commit 155abe805b
2 changed files with 9 additions and 15 deletions

View File

@ -118,7 +118,7 @@ public class HashCodeBuilder implements Builder<Integer> {
* *
* @since 2.3 * @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() * 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. * The object to register.
*/ */
private static void register(final Object value) { private static void register(final Object value) {
Set<IDKey> registry = getRegistry(); getRegistry().add(new IDKey(value));
if (registry == null) {
registry = new HashSet<>();
REGISTRY.set(registry);
}
registry.add(new IDKey(value));
} }
/** /**
@ -480,6 +475,7 @@ public class HashCodeBuilder implements Builder<Integer> {
* *
* <p> * <p>
* Used by the reflection methods to avoid infinite loops. * Used by the reflection methods to avoid infinite loops.
* </p>
* *
* @param value * @param value
* The object to unregister. * The object to unregister.
@ -487,11 +483,9 @@ public class HashCodeBuilder implements Builder<Integer> {
*/ */
private static void unregister(final Object value) { private static void unregister(final Object value) {
final Set<IDKey> registry = getRegistry(); final Set<IDKey> registry = getRegistry();
if (registry != null) { registry.remove(new IDKey(value));
registry.remove(new IDKey(value)); if (registry.isEmpty()) {
if (registry.isEmpty()) { REGISTRY.remove();
REGISTRY.remove();
}
} }
} }

View File

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; 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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.commons.lang3.AbstractLangTest; import org.apache.commons.lang3.AbstractLangTest;
import org.junit.jupiter.api.Test; 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) // at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:422)
a.hashCode(); a.hashCode();
assertNull(HashCodeBuilder.getRegistry()); assertTrue(HashCodeBuilder.getRegistry().isEmpty());
b.hashCode(); b.hashCode();
assertNull(HashCodeBuilder.getRegistry()); assertTrue(HashCodeBuilder.getRegistry().isEmpty());
} }
@Test @Test