Make LockVisitor.acceptReadLocked(FailableConsumer) null-safe

Make LockVisitor.applyWriteLocked(FailableConsumer) null-safe
This commit is contained in:
Gary Gregory 2024-05-12 15:31:29 -04:00
parent ba3f9102bf
commit b9f15bc007
3 changed files with 10 additions and 3 deletions

View File

@ -126,6 +126,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Stephan Peters, Gary Gregory, Bernd">StringUtils.stripAccents() should handle ligatures, UTF32 math blocks, etc. #1201.</action>
<action issue="LANG-1524" type="fix" dev="ggregory" due-to="kijong.youn, Aakash Gupta, Gary Gregory">TypeUtils.toString(Type) StackOverflowError for an inner class in the inner class parameterized enclosing class #657.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate SystemUtils.getUserName(String) in favor of SystemProperties.getUserName(Supplier).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make LockVisitor.acceptReadLocked(FailableConsumer) null-safe.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make LockVisitor.applyWriteLocked(FailableConsumer) null-safe.</action>
<!-- UPDATE -->
<action type="update" dev="sebb" due-to="Dependabot">Bump commons-parent from 64 to 69 #1194.</action>
<action type="update" dev="ggregory" due-to="Dependabot">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.1 to 3.2.0 #1175.</action>

View File

@ -26,6 +26,7 @@ import java.util.function.Supplier;
import org.apache.commons.lang3.function.Failable;
import org.apache.commons.lang3.function.FailableConsumer;
import org.apache.commons.lang3.function.FailableFunction;
import org.apache.commons.lang3.function.Suppliers;
/**
* Combines the monitor and visitor pattern to work with {@link java.util.concurrent.locks.Lock locked objects}. Locked
@ -262,10 +263,12 @@ public class LockingVisitors {
* @see #acceptWriteLocked(FailableConsumer)
*/
protected void lockAcceptUnlock(final Supplier<Lock> lockSupplier, final FailableConsumer<O, ?> consumer) {
final Lock lock = lockSupplier.get();
final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock");
lock.lock();
try {
consumer.accept(object);
if (consumer != null) {
consumer.accept(object);
}
} catch (final Throwable t) {
throw Failable.rethrow(t);
} finally {
@ -289,7 +292,7 @@ public class LockingVisitors {
* @see #applyWriteLocked(FailableFunction)
*/
protected <T> T lockApplyUnlock(final Supplier<Lock> lockSupplier, final FailableFunction<O, T, ?> function) {
final Lock lock = lockSupplier.get();
final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock");
lock.lock();
try {
return function.apply(object);

View File

@ -93,8 +93,10 @@ public class LockingVisitorsTest extends AbstractLangTest {
final AtomicInteger res = new AtomicInteger();
final ReadWriteLock rwLock = new ReentrantReadWriteLock();
LockingVisitors.create(res, rwLock).acceptReadLocked(AtomicInteger::incrementAndGet);
LockingVisitors.create(res, rwLock).acceptReadLocked(null);
assertEquals(1, res.get());
LockingVisitors.create(res, rwLock).acceptWriteLocked(AtomicInteger::incrementAndGet);
LockingVisitors.create(res, rwLock).acceptWriteLocked(null);
assertEquals(2, res.get());
}