Class StopWatch

java.lang.Object
org.jpos.util.StopWatch

public class StopWatch extends Object
Simple wall-clock timer that pads a block of work to a minimum duration, useful when masking timing differences between branches (e.g. successful vs. failed authentications).
  • Constructor Summary

    Constructors
    Constructor
    Description
    StopWatch(long periodInMillis)
    Convenience constructor that takes the minimum duration in milliseconds.
    StopWatch(long period, TimeUnit unit)
    Constructs a StopWatch that finishes no earlier than period from now.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Sleeps until the configured deadline, returning immediately when already past it.
    static <T> T
    get(long period, TimeUnit unit, Supplier<T> f)
    Runs f and pads its execution to at least period.
    static <T> T
    get(long period, Supplier<T> f)
    Runs f and pads its execution to at least period milliseconds.
    boolean
    Indicates whether the deadline has been reached.

    Methods inherited from class Object

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

    • StopWatch

      public StopWatch(long period, TimeUnit unit)
      Constructs a StopWatch that finishes no earlier than period from now.
      Parameters:
      period - minimum duration to elapse before finish() returns
      unit - unit for period
    • StopWatch

      public StopWatch(long periodInMillis)
      Convenience constructor that takes the minimum duration in milliseconds.
      Parameters:
      periodInMillis - minimum duration in milliseconds
  • Method Details

    • finish

      public void finish()
      Sleeps until the configured deadline, returning immediately when already past it.
    • isFinished

      public boolean isFinished()
      Indicates whether the deadline has been reached.
      Returns:
      true if the configured period has elapsed
    • get

      public static <T> T get(long period, TimeUnit unit, Supplier<T> f)
      Runs f and pads its execution to at least period.
      Type Parameters:
      T - result type returned by f
      Parameters:
      period - minimum duration to elapse
      unit - unit for period
      f - the work to perform
      Returns:
      the value returned by f
    • get

      public static <T> T get(long period, Supplier<T> f)
      Runs f and pads its execution to at least period milliseconds.
      Type Parameters:
      T - result type returned by f
      Parameters:
      period - minimum duration in milliseconds
      f - the work to perform
      Returns:
      the value returned by f