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 read lock, usually from the lock object.
        writeLockSupplier - Supplies the write lock, usually from the lock object.
    • Method Detail

      • acceptReadLocked

        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):

        1. Obtain a read (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
        2. Invokes the given consumer, passing the locked object as the parameter.
        3. 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(FailableConsumer), applyReadLocked(FailableFunction)
      • acceptWriteLocked

        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):

        1. Obtain a write (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
        2. Invokes the given consumer, passing the locked object as the parameter.
        3. 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:
        acceptReadLocked(FailableConsumer), applyWriteLocked(FailableFunction)
      • applyReadLocked

        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):

        1. Obtain a read (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
        2. Invokes the given function, passing the locked object as the parameter, receiving the functions result.
        3. Release the lock, as soon as the consumers invocation is done. If the invocation results in an error, the lock will be released anyways.
        4. 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:
        java.lang.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:
        acceptReadLocked(FailableConsumer), applyWriteLocked(FailableFunction)
      • applyWriteLocked

        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):

        1. Obtain a read (shared) lock on the locked (hidden) object. The current thread may block, until such a lock is granted.
        2. Invokes the given function, passing the locked object as the parameter, receiving the functions result.
        3. Release the lock, as soon as the consumers invocation is done. If the invocation results in an error, the lock will be released anyways.
        4. 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:
        java.lang.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:
        acceptReadLocked(FailableConsumer), applyWriteLocked(FailableFunction)
      • getLock

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

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

        protected <T> T lockApplyUnlock​(java.util.function.Supplier<java.util.concurrent.locks.Lock> lockSupplier,
                                        FailableFunction<O,​T,​?> function)
        This method provides the actual implementation for applyReadLocked(FailableFunction), and applyWriteLocked(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 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.
        Returns:
        The result object, which has been returned by the functions invocation.
        Throws:
        java.lang.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:
        applyReadLocked(FailableFunction), applyWriteLocked(FailableFunction)