O
- the wrapped object type.L
- the wrapped lock type.public static class LockingVisitors.LockVisitor<O,L> extends Object
Modifier | Constructor and Description |
---|---|
protected |
LockVisitor(O object,
L lock,
Supplier<Lock> readLockSupplier,
Supplier<Lock> writeLockSupplier)
Constructs an instance.
|
Modifier and Type | Method and Description |
---|---|
void |
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.
|
L |
getLock()
Gets the lock.
|
O |
getObject()
Gets the guarded object.
|
protected void |
lockAcceptUnlock(Supplier<Lock> lockSupplier,
FailableConsumer<O,?> consumer)
This method provides the default implementation for
acceptReadLocked(FailableConsumer) , and
acceptWriteLocked(FailableConsumer) . |
protected <T> T |
lockApplyUnlock(Supplier<Lock> lockSupplier,
FailableFunction<O,T,?> function)
This method provides the actual implementation for
applyReadLocked(FailableFunction) , and
applyWriteLocked(FailableFunction) . |
protected LockVisitor(O object, L lock, Supplier<Lock> readLockSupplier, Supplier<Lock> writeLockSupplier)
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.public void acceptReadLocked(FailableConsumer<O,?> consumer)
Provides read (shared, non-exclusive) access to the locked (hidden) object. More precisely, what the method will do (in the given order):
consumer
, passing the locked object as the parameter.consumer
- The consumer, which is being invoked to use the hidden object, which will be passed as the
consumers parameter.acceptWriteLocked(FailableConsumer)
,
applyReadLocked(FailableFunction)
public void acceptWriteLocked(FailableConsumer<O,?> consumer)
Provides write (exclusive) access to the locked (hidden) object. More precisely, what the method will do (in the given order):
consumer
, passing the locked object as the parameter.consumer
- The consumer, which is being invoked to use the hidden object, which will be passed as the
consumers parameter.acceptReadLocked(FailableConsumer)
,
applyWriteLocked(FailableFunction)
public <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. More precisely, what the method will do (in the given order):
function
, passing the locked object as the parameter,
receiving the functions result.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(); }
T
- The result type (both the functions, and this method's.)function
- The function, which is being invoked to compute the result. The function will receive the
hidden object.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.acceptReadLocked(FailableConsumer)
,
applyWriteLocked(FailableFunction)
public <T> T applyWriteLocked(FailableFunction<O,T,?> function)
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):
function
, passing the locked object as the parameter,
receiving the functions result.T
- The result type (both the functions, and this method's.)function
- The function, which is being invoked to compute the result. The function will receive the
hidden object.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.acceptReadLocked(FailableConsumer)
,
applyWriteLocked(FailableFunction)
protected void lockAcceptUnlock(Supplier<Lock> lockSupplier, FailableConsumer<O,?> consumer)
acceptReadLocked(FailableConsumer)
, and
acceptWriteLocked(FailableConsumer)
.lockSupplier
- A supplier for the lock. (This provides, in fact, a long, because a StampedLock
is used
internally.)consumer
- The consumer, which is to be given access to the locked (hidden) object, which will be passed
as a parameter.acceptReadLocked(FailableConsumer)
,
acceptWriteLocked(FailableConsumer)
protected <T> T lockApplyUnlock(Supplier<Lock> lockSupplier, FailableFunction<O,T,?> function)
applyReadLocked(FailableFunction)
, and
applyWriteLocked(FailableFunction)
.T
- The result type (both the functions, and this method's.)lockSupplier
- A supplier for the lock. (This provides, in fact, a long, because a StampedLock
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.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.applyReadLocked(FailableFunction)
,
applyWriteLocked(FailableFunction)
Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.