Class ConditionalTimer<T>

java.lang.Object
no.digipost.time.ConditionalTimer<T>

public final class ConditionalTimer<T> extends Object
A util for timing the duration a type of value meets a set condition. The value is supplied periodically to the timer with inspect(..), and the timing starts once a supplied value matches the predicate the timer is initialized with. The duration can only be retrieved as long as the condition was met at least on the last invocation of inspect(..), or it returns Optional.empty().
  • Method Details

    • using

      public static ConditionalTimer.WithCustomClockBuilder using(Clock clock)
    • timeWhen

      public static <T> ConditionalTimer<T> timeWhen(Predicate<T> condition)
    • inspect

      public final void inspect(T value)
      Perform inspection of a value to determine if timing should occur.
    • getDuration

      public Optional<Duration> getDuration()
      Returns:
      the duration of the currently ongoing met condition, or Optional.empty() if the condition is currently not met.
    • longerThan

      public boolean longerThan(Duration threshold)
      Returns:
      false if condition is currently not met, true if the current duration is longer than the given threshold, or false otherwise.
    • sameOrlessThan

      public boolean sameOrlessThan(Duration threshold)
      Returns:
      true if condition is currently not met or the current duration is less than the given threshold, or false otherwise.