From 2275cca426e0ff13c40278ae428368615943ee2e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Fri, 7 Jul 2023 08:06:55 -0400 Subject: [PATCH] Add LockingVisitors.create(O, ReadWriteLock) --- src/changes/changes.xml | 1 + .../lang3/concurrent/locks/LockingVisitors.java | 15 ++++++++++++++- .../concurrent/locks/LockingVisitorsTest.java | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5d64f6699..b9930b0d2 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -215,6 +215,7 @@ The type attribute can be add,update,fix,remove. Add ReflectionDiffBuilder.setExcludeFieldNames(...) and DiffExclude a… #838. Add and ExceptionUtils.isChecked() and isUnchecked() #1069 Add and use ExceptionUtils.throwUnchecked(throwable). + Add LockingVisitors.create(O, ReadWriteLock). Bump actions/cache from 2.1.4 to 3.0.10 #742, #752, #764, #833, #867, #959, #964. Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963. diff --git a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java index 0d10ebe4c..0d11fad77 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java @@ -348,6 +348,19 @@ public class LockingVisitors { } } + /** + * Creates a new instance of {@link ReadWriteLockVisitor} with the given (hidden) object and lock. + * + * @param The locked objects type. + * @param object The locked (hidden) object. + * @param readWriteLock The lock to use. + * @return The created instance, a {@link StampedLockVisitor lock} for the given object. + * @since 3.13.0 + */ + public static ReadWriteLockVisitor create(final O object, final ReadWriteLock readWriteLock) { + return new LockingVisitors.ReadWriteLockVisitor<>(object, readWriteLock); + } + /** * Creates a new instance of {@link ReadWriteLockVisitor} with the given (hidden) object. * @@ -356,7 +369,7 @@ public class LockingVisitors { * @return The created instance, a {@link StampedLockVisitor lock} for the given object. */ public static ReadWriteLockVisitor reentrantReadWriteLockVisitor(final O object) { - return new LockingVisitors.ReadWriteLockVisitor<>(object, new ReentrantReadWriteLock()); + return create(object, new ReentrantReadWriteLock()); } /** diff --git a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java index d8d1a453d..cdf78dae4 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/locks/LockingVisitorsTest.java @@ -16,11 +16,15 @@ */ package org.apache.commons.lang3.concurrent.locks; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertTrue; import java.time.Duration; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.LongConsumer; import org.apache.commons.lang3.AbstractLangTest; @@ -84,6 +88,16 @@ public class LockingVisitorsTest extends AbstractLangTest { } } + @Test + public void testCreate() { + final AtomicInteger res = new AtomicInteger(); + final ReadWriteLock rwLock = new ReentrantReadWriteLock(); + LockingVisitors.create(res, rwLock).acceptReadLocked(AtomicInteger::incrementAndGet); + assertEquals(1, res.get()); + LockingVisitors.create(res, rwLock).acceptWriteLocked(AtomicInteger::incrementAndGet); + assertEquals(2, res.get()); + } + @Test public void testReentrantReadWriteLockExclusive() throws Exception {