Class LockingVisitors.LockVisitor<O,L>
java.lang.Object
org.apache.commons.lang3.concurrent.locks.LockingVisitors.LockVisitor<O,L>
- Type Parameters:
O
- the wrapped object type.L
- the wrapped lock type.
- Direct Known Subclasses:
LockingVisitors.ReadWriteLockVisitor
,LockingVisitors.StampedLockVisitor
- Enclosing class:
- LockingVisitors
Wraps a domain object and a lock for access by lambdas.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
acceptReadLocked
(FailableConsumer<O, ?> consumer) Provides read (shared, non-exclusive) access to the locked (hidden) object.void
acceptWriteLocked
(FailableConsumer<O, ?> consumer) Provides write (exclusive) access to the locked (hidden) object.<T> T
applyReadLocked
(FailableFunction<O, T, ?> function) Provides read (shared, non-exclusive) access to the locked (hidden) object for the purpose of computing a result object.<T> T
applyWriteLocked
(FailableFunction<O, T, ?> function) Provides write (exclusive) access to the locked (hidden) object for the purpose of computing a result object.getLock()
Gets the lock.Gets the guarded object.protected void
lockAcceptUnlock
(Supplier<Lock> lockSupplier, FailableConsumer<O, ?> consumer) This method provides the default implementation foracceptReadLocked(FailableConsumer)
, andacceptWriteLocked(FailableConsumer)
.protected <T> T
lockApplyUnlock
(Supplier<Lock> lockSupplier, FailableFunction<O, T, ?> function) This method provides the actual implementation forapplyReadLocked(FailableFunction)
, andapplyWriteLocked(FailableFunction)
.
-
Constructor Details
-
LockVisitor
protected LockVisitor(O object, L lock, Supplier<Lock> readLockSupplier, Supplier<Lock> writeLockSupplier) Constructs an instance.- Parameters:
object
- The object to guard.lock
- The locking object.readLockSupplier
- Supplies the read lock, usually from the lock object.writeLockSupplier
- Supplies the write lock, usually from the lock object.
-
-
Method Details
-
acceptReadLocked
Provides read (shared, non-exclusive) access to the locked (hidden) object. More precisely, what the method will do (in the given order):- Obtain a read (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
- Invokes the given
consumer
, passing the locked object as the parameter. - Release the lock, as soon as the consumers invocation is done. If the invocation results in an error, the lock will be released anyways.
- Parameters:
consumer
- The consumer, which is being invoked to use the hidden object, which will be passed as the consumers parameter.- See Also:
-
acceptWriteLocked
Provides write (exclusive) access to the locked (hidden) object. More precisely, what the method will do (in the given order):- Obtain a write (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
- Invokes the given
consumer
, passing the locked object as the parameter. - Release the lock, as soon as the consumers invocation is done. If the invocation results in an error, the lock will be released anyways.
- Parameters:
consumer
- The consumer, which is being invoked to use the hidden object, which will be passed as the consumers parameter.- See Also:
-
applyReadLocked
Provides read (shared, non-exclusive) access to the locked (hidden) object for the purpose of computing a result object. More precisely, what the method will do (in the given order):- Obtain a read (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
- Invokes the given
function
, passing the locked object as the parameter, receiving the functions result. - Release the lock, as soon as the consumers invocation is done. If the invocation results in an error, the lock will be released anyways.
- Return the result object, that has been received from the functions invocation.
Example: Consider that the hidden object is a list, and we wish to know the current size of the list. This might be achieved with the following:
private Lock<List<Object>> listLock; public int getCurrentListSize() { final Integer sizeInteger = listLock.applyReadLocked(list -> Integer.valueOf(list.size)); return sizeInteger.intValue(); }
- Type Parameters:
T
- The result type (both the functions, and this method's.)- Parameters:
function
- The function, which is being invoked to compute the result. The function will receive the hidden object.- Returns:
- The result object, which has been returned by the functions invocation.
- Throws:
IllegalStateException
- The result object would be, in fact, the hidden object. This would extend access to the hidden object beyond this methods lifetime and will therefore be prevented.- See Also:
-
applyWriteLocked
Provides write (exclusive) access to the locked (hidden) object for the purpose of computing a result object. More precisely, what the method will do (in the given order):- Obtain a read (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
- Invokes the given
function
, passing the locked object as the parameter, receiving the functions result. - Release the lock, as soon as the consumers invocation is done. If the invocation results in an error, the lock will be released anyways.
- Return the result object, that has been received from the functions invocation.
- Type Parameters:
T
- The result type (both the functions, and this method's.)- Parameters:
function
- The function, which is being invoked to compute the result. The function will receive the hidden object.- Returns:
- The result object, which has been returned by the functions invocation.
- Throws:
IllegalStateException
- The result object would be, in fact, the hidden object. This would extend access to the hidden object beyond this methods lifetime and will therefore be prevented.- See Also:
-
getLock
Gets the lock.- Returns:
- the lock.
-
getObject
Gets the guarded object.- Returns:
- the object.
-
lockAcceptUnlock
This method provides the default implementation foracceptReadLocked(FailableConsumer)
, andacceptWriteLocked(FailableConsumer)
.- Parameters:
lockSupplier
- A supplier for the lock. (This provides, in fact, a long, because aStampedLock
is used internally.)consumer
- The consumer, which is to be given access to the locked (hidden) object, which will be passed as a parameter.- See Also:
-
lockApplyUnlock
This method provides the actual implementation forapplyReadLocked(FailableFunction)
, andapplyWriteLocked(FailableFunction)
.- Type Parameters:
T
- The result type (both the functions, and this method's.)- Parameters:
lockSupplier
- A supplier for the lock. (This provides, in fact, a long, because aStampedLock
is used internally.)function
- The function, which is being invoked to compute the result object. This function will receive the locked (hidden) object as a parameter.- Returns:
- The result object, which has been returned by the functions invocation.
- Throws:
IllegalStateException
- The result object would be, in fact, the hidden object. This would extend access to the hidden object beyond this methods lifetime and will therefore be prevented.- See Also:
-