From fdb6e394b8b0bba1988c3262d90c232cb1a4b575 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 16 Mar 2024 08:05:26 -0400 Subject: [PATCH] Use Java 8 API to manage thread local --- .../commons/lang3/builder/EqualsBuilder.java | 17 +++++------------ .../lang3/builder/EqualsBuilderTest.java | 4 ---- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java index 1231b813b..bae95f9d4 100644 --- a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java @@ -93,7 +93,7 @@ public class EqualsBuilder implements Builder { * * @since 3.0 */ - private static final ThreadLocal>> REGISTRY = new ThreadLocal<>(); + private static final ThreadLocal>> REGISTRY = ThreadLocal.withInitial(HashSet::new); /* * NOTE: we cannot store the actual objects in a HashSet, as that would use the very hashCode() @@ -329,12 +329,7 @@ public class EqualsBuilder implements Builder { * @param rhs the other object to register */ private static void register(final Object lhs, final Object rhs) { - Set> registry = getRegistry(); - if (registry == null) { - registry = new HashSet<>(); - REGISTRY.set(registry); - } - registry.add(getRegisterPair(lhs, rhs)); + getRegistry().add(getRegisterPair(lhs, rhs)); } /** @@ -350,11 +345,9 @@ public class EqualsBuilder implements Builder { */ private static void unregister(final Object lhs, final Object rhs) { final Set> registry = getRegistry(); - if (registry != null) { - registry.remove(getRegisterPair(lhs, rhs)); - if (registry.isEmpty()) { - REGISTRY.remove(); - } + registry.remove(getRegisterPair(lhs, rhs)); + if (registry.isEmpty()) { + REGISTRY.remove(); } } diff --git a/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderTest.java index 3108490e3..69c591ffa 100644 --- a/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/EqualsBuilderTest.java @@ -20,7 +20,6 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Method; @@ -543,11 +542,8 @@ public class EqualsBuilderTest extends AbstractLangTest { refX3.setObjectReference(x3); assertEquals(x1, x2); - assertNull(EqualsBuilder.getRegistry()); assertNotEquals(x1, x3); - assertNull(EqualsBuilder.getRegistry()); assertNotEquals(x2, x3); - assertNull(EqualsBuilder.getRegistry()); } @Test