Class Timing
- java.lang.Object
-
- org.kiwiproject.beta.time.Timing
-
@Beta public final class Timing extends Object
Timing utilities that provide a convenient way to measure elapsed time of operations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTiming.TimedRepresents an operation that is timed.static classTiming.TimedNoResultRepresents an operation that is timed and returns no result.static classTiming.TimedWithResult<R>Represents an operation that is timed and returns a result.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Timing.TimedNoResulttimeNoResult(org.apache.commons.lang3.time.StopWatch stopWatch, Runnable operation)Time an operation that does not return a result usingStopWatch.static <R> Timing.TimedWithResult<R>timeWithResult(org.apache.commons.lang3.time.StopWatch stopWatch, Supplier<R> operation)Time an operation that returns a result usingStopWatch.
-
-
-
Method Detail
-
timeWithResult
public static <R> Timing.TimedWithResult<R> timeWithResult(org.apache.commons.lang3.time.StopWatch stopWatch, Supplier<R> operation)
Time an operation that returns a result usingStopWatch. Any exception thrown by the Supplier is propagated (not caught).The
StopWatchis reset before starting. It is always stopped, even if the operation throws an exception.If you need the elapsed time when an exception is thrown, you can simply call
StopWatch.getTime().Since
StopWatchis not thread-safe, callers are responsible for ensuring thread-safety. Passing it as an argument permits timing more than one sequential operation in the same method, but could lead to problems if callers are not careful. Typical use is to instantiate theStopWatchwithin a method and use it only within that method.- Type Parameters:
R- the type of result returned by the operation- Parameters:
stopWatch- the StopWatch to useoperation- the operation to time- Returns:
- a
Timing.TimedWithResultcontaining the elapsed time and the result of the operation
-
timeNoResult
public static Timing.TimedNoResult timeNoResult(org.apache.commons.lang3.time.StopWatch stopWatch, Runnable operation)
Time an operation that does not return a result usingStopWatch. Any exception thrown by the Supplier is propagated (not caught).The
StopWatchis reset before starting. It is always stopped, even if the operation throws an exception.If you need the elapsed time when an exception is thrown, you can simply call
StopWatch.getTime().Since
StopWatchis not thread-safe, callers are responsible for ensuring thread-safety. Passing it as an argument permits timing more than one sequential operation in the same method, but could lead to problems if callers are not careful. Typical use is to instantiate theStopWatchwithin a method and use it only within that method.- Parameters:
stopWatch- the StopWatch to useoperation- the operation to time- Returns:
- a
Timing.TimedNoResultcontaining the elapsed time of the operation
-
-