Class TryLocker

java.lang.Object
org.kiwiproject.concurrent.TryLocker

public class TryLocker extends Object
Utility that aids in using Lock.tryLock(long, TimeUnit).

This can also make unit testing easier by allowing easy mocking.

  • Field Details

    • DEFAULT_LOCK_WAIT_TIME

      public static final int DEFAULT_LOCK_WAIT_TIME
      The default lock wait time if not specified.
      See Also:
    • DEFAULT_LOCK_WAIT_TIME_UNIT

      public static final TimeUnit DEFAULT_LOCK_WAIT_TIME_UNIT
      The default lock wait time unit if not specified.
  • Method Details

    • usingReentrantLock

      public static TryLocker usingReentrantLock()
      Create a new TryLocker using a ReentrantLock and the default lock maximum wait time.
      Returns:
      a new instance
      See Also:
    • usingReentrantLock

      public static TryLocker usingReentrantLock(int maxWaitTime, TimeUnit maxWaitTimeUnit)
      Create a new TryLocker using a ReentrantLock with the given lock wait time.
      Parameters:
      maxWaitTime - maximum lock wait time
      maxWaitTimeUnit - maximum lock wait time value
      Returns:
      a new instance
    • using

      public static TryLocker using(Lock lock, long maxWaitTime, TimeUnit maxWaitTimeUnit)
      Create a new TryLocker using the given lock and maximum wait time.
      Parameters:
      lock - the Lock to use
      maxWaitTime - maximum lock wait time
      maxWaitTimeUnit - maximum lock wait time value
      Returns:
      a new instance
    • getLockWaitDuration

      public Duration getLockWaitDuration()
      Return the maximum lock wait time as a Duration.
      Returns:
      the lock wait duration
    • withLockOrElse

      public void withLockOrElse(Runnable withLockAction, Runnable orElseAction)
      Run withLockAction if the lock is obtained within the lock timeout period. Otherwise, run orElseAction.
      Parameters:
      withLockAction - action to run if lock is obtained
      orElseAction - action to run if lock is not obtained
    • withLockSupplyOrNull

      public <T> T withLockSupplyOrNull(Supplier<T> withLockSupplier)
      Execute the given withLockSupplier if the lock is obtained within the lock timeout period and return its value. Otherwise, return null.
      Type Parameters:
      T - type of object returned
      Parameters:
      withLockSupplier - supplier to execute if lock is obtained
      Returns:
      the supplied value or null
    • withLockSupplyOrFallback

      public <T> T withLockSupplyOrFallback(Supplier<T> withLockSupplier, T fallbackValue)
      Execute the given withLockSupplier if the lock is obtained within the lock timeout period and return its value. Otherwise, return the fallbackValue.
      Type Parameters:
      T - type of object returned
      Parameters:
      withLockSupplier - supplier to execute if lock is obtained
      fallbackValue - the value to use if the lock is not obtained
      Returns:
      the supplied value or the fallback value
    • withLockSupplyOrFallbackSupply

      public <T> T withLockSupplyOrFallbackSupply(Supplier<T> withLockSupplier, Supplier<T> fallbackSupplier)
      Execute the given withLockSupplier if the lock is obtained within the lock timeout period and return its value. Otherwise, return the valued supplied by fallbackSupplier.
      Type Parameters:
      T - type of object returned
      Parameters:
      withLockSupplier - supplier to execute if lock is obtained
      fallbackSupplier - the supplier to execute if the lock is not obtained
      Returns:
      the supplied value or the supplied fallback value
    • getLockWaitTime

      public long getLockWaitTime()
    • getLockWaitTimeUnit

      public TimeUnit getLockWaitTimeUnit()