Class Guarded<T>

java.lang.Object
de.arstwo.twotil.Guarded<T>

public class Guarded<T> extends Object
Functional approach of thread-safe lock access to a given target.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Guarded(T target)
     
    Guarded(T target, boolean fair)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    access(Consumer<T> action)
    Accesses the target and supplies it to the given consumer when ready.
    <R> R
    process(Function<T,R> action)
    Accesses the target, supplies it to the given function when ready and returns the computed result.
    boolean
    tryAccess(Consumer<T> action)
    Try immediate access to the target and forward it to the given consumer.
    <R> Optional<R>
    tryProcess(Function<T,R> action)
    Try to access the target immediately, supplies it to the given function when ready and possibly returns the computed result.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Guarded

      public Guarded(T target, boolean fair)
    • Guarded

      public Guarded(T target)
  • Method Details

    • access

      public void access(Consumer<T> action)
      Accesses the target and supplies it to the given consumer when ready.
      Parameters:
      action - any consumer for the target
    • process

      public <R> R process(Function<T,R> action)
      Accesses the target, supplies it to the given function when ready and returns the computed result.
      Type Parameters:
      R - any result type of the function given
      Parameters:
      action - the function to apply to the target once ready
      Returns:
      the result of the function
    • tryAccess

      public boolean tryAccess(Consumer<T> action)
      Try immediate access to the target and forward it to the given consumer.
      Parameters:
      action - any consumer for the target
      Returns:
      true if the target could be accessed immediately, false otherwise.
    • tryProcess

      public <R> Optional<R> tryProcess(Function<T,R> action)
      Try to access the target immediately, supplies it to the given function when ready and possibly returns the computed result.
      Type Parameters:
      R - any result type of the function given
      Parameters:
      action - the function to apply to the target if ready
      Returns:
      an optional result of the function applied if the target was ready, an empty optional if it was not ready.