You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/03/16 12:41:07 UTC
(commons-lang) 04/06: Use Java 8 API to manage thread local
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit fdb6e394b8b0bba1988c3262d90c232cb1a4b575
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Mar 16 08:05:26 2024 -0400
Use Java 8 API to manage thread local
---
.../org/apache/commons/lang3/builder/EqualsBuilder.java | 17 +++++------------
.../apache/commons/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<Boolean> {
*
* @since 3.0
*/
- private static final ThreadLocal<Set<Pair<IDKey, IDKey>>> REGISTRY = new ThreadLocal<>();
+ private static final ThreadLocal<Set<Pair<IDKey, IDKey>>> 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<Boolean> {
* @param rhs the other object to register
*/
private static void register(final Object lhs, final Object rhs) {
- Set<Pair<IDKey, IDKey>> 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<Boolean> {
*/
private static void unregister(final Object lhs, final Object rhs) {
final Set<Pair<IDKey, IDKey>> 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