Add LockingVisitors.create(O, ReadWriteLock)

This commit is contained in:
Gary Gregory 2023-07-07 08:06:55 -04:00
parent f2f19ab4fd
commit 2275cca426
3 changed files with 29 additions and 1 deletions

View File

@ -215,6 +215,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1677" type="add" dev="ggregory" due-to="Dennis Baerten, Gary Gregory">Add ReflectionDiffBuilder.setExcludeFieldNames(...) and DiffExclude a… #838.</action>
<action issue="LANG-1647" type="add" dev="ggregory" due-to="Arturo Bernal, Dimitrios Efthymiou, Gary Gregory">Add and ExceptionUtils.isChecked() and isUnchecked() #1069</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use ExceptionUtils.throwUnchecked(throwable).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LockingVisitors.create(O, ReadWriteLock).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.10 #742, #752, #764, #833, #867, #959, #964.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963.</action>

View File

@ -348,6 +348,19 @@ public class LockingVisitors {
}
}
/**
* Creates a new instance of {@link ReadWriteLockVisitor} with the given (hidden) object and lock.
*
* @param <O> 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 <O> ReadWriteLockVisitor<O> 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 <O> ReadWriteLockVisitor<O> reentrantReadWriteLockVisitor(final O object) {
return new LockingVisitors.ReadWriteLockVisitor<>(object, new ReentrantReadWriteLock());
return create(object, new ReentrantReadWriteLock());
}
/**

View File

@ -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 {