|
19 | 19 | import java.util.Objects; |
20 | 20 | import java.util.concurrent.locks.Lock; |
21 | 21 | import java.util.concurrent.locks.ReadWriteLock; |
| 22 | +import java.util.concurrent.locks.ReentrantLock; |
22 | 23 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
23 | 24 | import java.util.concurrent.locks.StampedLock; |
24 | 25 | import java.util.function.Supplier; |
@@ -325,6 +326,30 @@ protected ReadWriteLockVisitor(final O object, final ReadWriteLock readWriteLock |
325 | 326 | } |
326 | 327 | } |
327 | 328 |
|
| 329 | + /** |
| 330 | + * This class implements a wrapper for a locked (hidden) object, and provides the means to access it. The basic |
| 331 | + * idea, is that the user code forsakes all references to the locked object, using only the wrapper object, and the |
| 332 | + * accessor methods {@link #acceptReadLocked(FailableConsumer)}, {@link #acceptWriteLocked(FailableConsumer)}, |
| 333 | + * {@link #applyReadLocked(FailableFunction)}, and {@link #applyWriteLocked(FailableFunction)}. By doing so, the |
| 334 | + * necessary protections are guaranteed. |
| 335 | + * |
| 336 | + * @param <O> The locked (hidden) objects type. |
| 337 | + * @since 3.18.0 |
| 338 | + */ |
| 339 | + public static class ReentrantLockVisitor<O> extends LockVisitor<O, ReentrantLock> { |
| 340 | + |
| 341 | + /** |
| 342 | + * Creates a new instance with the given locked object. This constructor is supposed to be used for subclassing |
| 343 | + * only. In general, it is suggested to use {@link LockingVisitors#reentrantLockVisitor(Object)} instead. |
| 344 | + * |
| 345 | + * @param object The locked (hidden) object. The caller is supposed to drop all references to the locked object. |
| 346 | + * @param reentrantLock the lock to use. |
| 347 | + */ |
| 348 | + protected ReentrantLockVisitor(final O object, final ReentrantLock reentrantLock) { |
| 349 | + super(object, reentrantLock, () -> reentrantLock, () -> reentrantLock); |
| 350 | + } |
| 351 | + } |
| 352 | + |
328 | 353 | /** |
329 | 354 | * This class implements a wrapper for a locked (hidden) object, and provides the means to access it. The basic |
330 | 355 | * idea is that the user code forsakes all references to the locked object, using only the wrapper object, and the |
@@ -361,6 +386,31 @@ public static <O> ReadWriteLockVisitor<O> create(final O object, final ReadWrite |
361 | 386 | return new LockingVisitors.ReadWriteLockVisitor<>(object, readWriteLock); |
362 | 387 | } |
363 | 388 |
|
| 389 | + /** |
| 390 | + * Creates a new instance of {@link ReadWriteLockVisitor} with the given (hidden) object and lock. |
| 391 | + * |
| 392 | + * @param <O> The locked objects type. |
| 393 | + * @param object The locked (hidden) object. |
| 394 | + * @param reentrantLock The lock to use. |
| 395 | + * @return The created instance, a {@link StampedLockVisitor lock} for the given object. |
| 396 | + * @since 3.18.0 |
| 397 | + */ |
| 398 | + public static <O> ReentrantLockVisitor<O> create(final O object, final ReentrantLock reentrantLock) { |
| 399 | + return new LockingVisitors.ReentrantLockVisitor<>(object, reentrantLock); |
| 400 | + } |
| 401 | + |
| 402 | + /** |
| 403 | + * Creates a new instance of {@link ReadWriteLockVisitor} with the given (hidden) object. |
| 404 | + * |
| 405 | + * @param <O> The locked objects type. |
| 406 | + * @param object The locked (hidden) object. |
| 407 | + * @return The created instance, a {@link StampedLockVisitor lock} for the given object. |
| 408 | + * @since 3.18.0 |
| 409 | + */ |
| 410 | + public static <O> ReentrantLockVisitor<O> reentrantLockVisitor(final O object) { |
| 411 | + return create(object, new ReentrantLock()); |
| 412 | + } |
| 413 | + |
364 | 414 | /** |
365 | 415 | * Creates a new instance of {@link ReadWriteLockVisitor} with the given (hidden) object. |
366 | 416 | * |
|
0 commit comments