Class LockingVisitors.LockVisitor<O,​L>

    • Constructor Detail

      • LockVisitor

        protected LockVisitor​(O object,
                              L lock,
                              java.util.function.Supplier<java.util.concurrent.locks.Lock> readLockSupplier,
                              java.util.function.Supplier<java.util.concurrent.locks.Lock> writeLockSupplier)
        Constructs an instance.
        Parameters:
        object - The object to guard.
        lock - The locking object.
        readLockSupplier - Supplies the lock used by read methods.
        writeLockSupplier - Supplies the lock used by write methods.
    • Method Detail

      • acceptReadLocked

        public void acceptReadLocked​(FailableConsumer<O,​?> consumer)
        Invokes the consumer while holding the lock supplied for read operations. The lock is released in a finally block after the consumer returns or throws. Whether other readers can proceed concurrently depends on the supplied lock.
        Parameters:
        consumer - The consumer of the guarded object.
        See Also:
        acceptWriteLocked(FailableConsumer), applyReadLocked(FailableFunction)
      • applyReadLocked

        public <T> T applyReadLocked​(FailableFunction<O,​T,​?> function)
        Applies the function while holding the lock supplied for read operations. The lock is released in a finally block after the function returns or throws. Whether other readers can proceed concurrently depends on the supplied lock.
        Type Parameters:
        T - The result type.
        Parameters:
        function - The function applied to the guarded object.
        Returns:
        The function result.
        Throws:
        java.lang.NullPointerException - Thrown if the lock supplier is null or returns null.
        See Also:
        acceptReadLocked(FailableConsumer), applyWriteLocked(FailableFunction)
      • applyWriteLocked

        public <T> T applyWriteLocked​(FailableFunction<O,​T,​?> function)
        Applies the function while holding the lock supplied for write operations. The lock is released in a finally block after the function returns or throws.
        Type Parameters:
        T - The result type.
        Parameters:
        function - The function applied to the guarded object.
        Returns:
        The function result.
        Throws:
        java.lang.NullPointerException - Thrown if the lock supplier is null or returns null.
        See Also:
        acceptWriteLocked(FailableConsumer), applyReadLocked(FailableFunction)
      • getLock

        public L getLock()
        Gets the lock.
        Returns:
        The lock.
      • getObject

        public O getObject()
        Gets the guarded object.
        Returns:
        The object.