Class Timing
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceRepresents an operation that is timed.static final classRepresents an operation that is timed and returns no result.static interfaceRepresents an operation that is timed and may throw a runtime exception.static final classRepresents an operation that is timed and returns no result, but may throw an exception.static final classRepresents an operation that is timed and returns a (possibly null) result, but may throw an exception.static final classRepresents an operation that is timed and returns a result. -
Method Summary
Modifier and TypeMethodDescriptiontimeNoResult(Runnable operation) Time an operation that does not return a result, but may throw an exception.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.TimedWithErrorOrResult<R>timeWithResult(Supplier<R> operation) Time an operation that returns a result or throws an exception.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 Details
-
timeWithResult
@CheckReturnValue 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.getDuration()orStopWatch.getNanoTime().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
@CheckReturnValue 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.getDuration()orStopWatch.getNanoTime().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
-
timeWithResult
@CheckReturnValue public static <R> Timing.TimedWithErrorOrResult<R> timeWithResult(Supplier<R> operation) Time an operation that returns a result or throws an exception. The return value should be inspected to determine whether the operation succeeded or failed.- Type Parameters:
R- the type of result returned by the operation- Parameters:
operation- the operation to time- Returns:
- a
Timing.TimedWithErrorOrResultcontaining the elapsed time and the result of the operation or an exception if the operation failed
-
timeNoResult
Time an operation that does not return a result, but may throw an exception. The return value should be inspected to determine whether the operation succeeded or failed.- Parameters:
operation- the operation to time- Returns:
- a
Timing.TimedWithErrorNoResultcontaining the elapsed time of the operation and an exception if the operation failed
-