Use Java 8 API to manage thread local

This commit is contained in:
Gary Gregory 2024-03-16 08:05:26 -04:00
parent e3658ad7bb
commit fdb6e394b8
2 changed files with 5 additions and 16 deletions

View File

@ -93,7 +93,7 @@ public class EqualsBuilder implements Builder<Boolean> {
* *
* @since 3.0 * @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() * 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 * @param rhs the other object to register
*/ */
private static void register(final Object lhs, final Object rhs) { private static void register(final Object lhs, final Object rhs) {
Set<Pair<IDKey, IDKey>> registry = getRegistry(); getRegistry().add(getRegisterPair(lhs, rhs));
if (registry == null) {
registry = new HashSet<>();
REGISTRY.set(registry);
}
registry.add(getRegisterPair(lhs, rhs));
} }
/** /**
@ -350,11 +345,9 @@ public class EqualsBuilder implements Builder<Boolean> {
*/ */
private static void unregister(final Object lhs, final Object rhs) { private static void unregister(final Object lhs, final Object rhs) {
final Set<Pair<IDKey, IDKey>> registry = getRegistry(); final Set<Pair<IDKey, IDKey>> registry = getRegistry();
if (registry != null) { registry.remove(getRegisterPair(lhs, rhs));
registry.remove(getRegisterPair(lhs, rhs)); if (registry.isEmpty()) {
if (registry.isEmpty()) { REGISTRY.remove();
REGISTRY.remove();
}
} }
} }

View File

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
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.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -543,11 +542,8 @@ public class EqualsBuilderTest extends AbstractLangTest {
refX3.setObjectReference(x3); refX3.setObjectReference(x3);
assertEquals(x1, x2); assertEquals(x1, x2);
assertNull(EqualsBuilder.getRegistry());
assertNotEquals(x1, x3); assertNotEquals(x1, x3);
assertNull(EqualsBuilder.getRegistry());
assertNotEquals(x2, x3); assertNotEquals(x2, x3);
assertNull(EqualsBuilder.getRegistry());
} }
@Test @Test