Package no.digipost.time
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 withinspect(..), 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 ofinspect(..), or it returnsOptional.empty().
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classConditionalTimer.WithCustomClockBuilder
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<Duration>getDuration()voidinspect(T value)Perform inspection of a value to determine if timing should occur.booleanlongerThan(Duration threshold)booleansameOrlessThan(Duration threshold)static <T> ConditionalTimer<T>timeWhen(Predicate<T> condition)static ConditionalTimer.WithCustomClockBuilderusing(Clock clock)
-
-
-
Method Detail
-
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:
falseif condition is currently not met,trueif the currentdurationis longer than the given threshold, orfalseotherwise.
-
-