This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 150202040 Clarify StopWatch Javadoc and test consecutive splits
150202040 is described below
commit 150202040e2eec357c7153f9e027fbde4a0d3daa
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 29 10:39:16 2026 -0500
Clarify StopWatch Javadoc and test consecutive splits
Correct lifecycle restrictions, split history, elapsed-time and
timestamp
descriptions, and callback state requirements.
Add coverage for consecutive unlabeled splits. Runtime behavior is
unchanged.
Assisted-By: OpenAI Codex
---
.../org/apache/commons/lang3/time/StopWatch.java | 196 ++++++++++++---------
.../apache/commons/lang3/time/StopWatchTest.java | 30 ++++
2 files changed, 141 insertions(+), 85 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
index 3fad94efd..ff462c4f1 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -39,16 +39,16 @@
* To start the watch, call {@link #start()} or {@link
StopWatch#createStarted()}. At this point you can:
* </p>
* <ul>
- * <li>{@link #split()} the watch to get the time while the watch continues in
the background. {@link #unsplit()} will remove the effect of the split. At this
- * point, these three options are available again.</li>
+ * <li>{@link #split()} the watch to record the elapsed time while the watch
continues running. You can split again, suspend, or stop without calling
+ * {@link #unsplit()}. Unsplitting removes the latest recorded split.</li>
* <li>{@link #suspend()} the watch to pause it. {@link #resume()} allows the
watch to continue. Any time between the suspend and resume will not be counted
in
* the total. At this point, these three options are available again.</li>
* <li>{@link #stop()} the watch to complete the timing session.</li>
* </ul>
*
* <p>
- * It is intended that the output methods {@link #toString()} and {@link
#getTime()} should only be called after stop, split or suspend, however a
suitable
- * result will be returned at other points.
+ * The elapsed-time methods, including {@link #getDuration()}, {@link
#getTime()}, and {@link #toString()}, can be called at any time. They report
the current
+ * elapsed time while the watch is running, including after a split. Use
{@link #getSplitDuration()} or {@link #toSplitString()} to retrieve the latest
split.
* </p>
*
* <p>
@@ -57,10 +57,10 @@
* </p>
*
* <ol>
- * <li>{@link #suspend()} or {@link #stop()} cannot be invoked twice</li>
- * <li>{@link #unsplit()} may only be called if the watch has been {@link
#split()}</li>
- * <li>{@link #resume()} may only be called if the watch has been {@link
#suspend()}</li>
- * <li>{@link #start()} cannot be called twice without calling {@link
#reset()}</li>
+ * <li>{@link #suspend()} or {@link #stop()} cannot be invoked twice
consecutively.</li>
+ * <li>{@link #unsplit()} may only be called if the watch is currently
split.</li>
+ * <li>{@link #resume()} may only be called if the watch is currently
suspended.</li>
+ * <li>{@link #start()} cannot be called twice without calling {@link
#reset()}.</li>
* </ol>
*
* <p>
@@ -111,7 +111,7 @@ public String getLabel() {
/**
* Converts this instance to a string.
*
- * @return this instance to a string.
+ * @return A string representation of this split.
*/
@Override
public String toString() {
@@ -127,7 +127,7 @@ private enum SplitState {
}
/**
- * Enumerates type status of a StopWatch.
+ * Enumerates the running states of a StopWatch.
*/
private enum State {
@@ -200,23 +200,23 @@ boolean isSuspended() {
};
/**
- * Tests whether this StopWatch is started. A suspended StopWatch is
also started.
+ * Tests whether this StopWatch is running or suspended.
*
- * @return boolean If this StopWatch is started.
+ * @return {@code true} if this StopWatch is running or suspended.
*/
abstract boolean isStarted();
/**
- * Tests whether this StopWatch is stopped. A StopWatch which is not
yet started and explicitly stopped is considered stopped.
+ * Tests whether this StopWatch is unstarted or stopped.
*
- * @return boolean If this StopWatch is stopped.
+ * @return {@code true} if this StopWatch is unstarted or stopped.
*/
abstract boolean isStopped();
/**
* Tests whether this StopWatch is suspended.
*
- * @return boolean If this StopWatch is suspended.
+ * @return {@code true} if this StopWatch is suspended.
*/
abstract boolean isSuspended();
}
@@ -258,43 +258,27 @@ public static StopWatch createStarted() {
private State runningState = State.UNSTARTED;
/**
- * Whether this StopWatch has a split time recorded.
+ * Whether the latest split is available through the split-time getters.
*/
private SplitState splitState = SplitState.UNSPLIT;
/**
- * The start time in nanoseconds.
- *
- * This field can be removed once we move off of Java 8.
+ * The start time from System.nanoTime(), adjusted on resume to exclude
time spent suspended.
*/
private long startTimeNanos;
/**
- * The start Instant.
- * <p>
- * nanoTime is only for elapsed time so we need to also store the
currentTimeMillis to maintain the old getStartTime API.
- * </p>
- * <p>
- * On Java 8, Instant has millisecond precision, later versions use
nanoseconds.
- * </p>
+ * The Instant recorded at the start of the timing session, independent of
the elapsed-time measurements.
*/
private Instant startInstant;
/**
- * The end Instant.
- * <p>
- * nanoTime is only for elapsed time so we need to also store the
currentTimeMillis to maintain the old getStartTime API.
- * </p>
- * <p>
- * On Java 8, Instant has millisecond precision, later versions use
nanoseconds.
- * </p>
+ * The Instant most recently recorded by a split, suspension, or stop
while running.
*/
private Instant stopInstant;
/**
- * The stop time in nanoseconds.
- *
- * This field can be removed once we move off of Java 8.
+ * The time from System.nanoTime() most recently recorded by a split,
suspension, or stop while running.
*/
private long stopTimeNanos;
@@ -324,6 +308,7 @@ public StopWatch(final String message) {
* Formats the split time with {@link
DurationFormatUtils#formatDurationHMS}.
*
* @return The split time formatted by {@link
DurationFormatUtils#formatDurationHMS}.
+ * @throws IllegalStateException Thrown if this StopWatch is not currently
split.
* @since 3.10
*/
public String formatSplitTime() {
@@ -331,7 +316,7 @@ public String formatSplitTime() {
}
/**
- * Formats the time formatted with {@link
DurationFormatUtils#formatDurationHMS}.
+ * Formats the elapsed time with {@link
DurationFormatUtils#formatDurationHMS}.
*
* @return The time formatted by {@link
DurationFormatUtils#formatDurationHMS}.
* @since 3.10
@@ -343,9 +328,16 @@ public String formatTime() {
/**
* Gets the result of {@link Supplier#get()} while recording the duration
of the call.
*
+ * <p>
+ * Starts or resumes this watch as needed, then calls {@link #suspend()}
when the supplier returns or throws. An already running watch keeps its current
+ * start time. The supplier must leave this watch running so it can be
suspended.
+ * Elapsed time accumulates across calls until {@link #reset()}. A stopped
watch must be reset before calling this method.
+ * </p>
+ *
* @param <T> the type of results supplied by this supplier.
* @param supplier The supplier to {@link Supplier#get()}.
* @return A result from the given Supplier.
+ * @throws IllegalStateException Thrown if this StopWatch is stopped
before the call, or is not running when the supplier completes.
* @since 3.18.0
*/
public <T> T get(final Supplier<T> supplier) {
@@ -358,13 +350,14 @@ public <T> T get(final Supplier<T> supplier) {
}
/**
- * Gets the Duration on this StopWatch.
+ * Gets the elapsed Duration on this StopWatch.
*
* <p>
- * This is either the Duration between the start and the moment this
method is called, or the Duration between start and stop.
+ * The Duration is zero before starting or after resetting, increases
while running, and remains fixed while suspended or stopped. Time spent
suspended is
+ * excluded. Splitting does not pause the elapsed Duration.
* </p>
*
- * @return The Duration.
+ * @return The elapsed Duration.
* @since 3.16.0
*/
public Duration getDuration() {
@@ -385,12 +378,13 @@ public String getMessage() {
* Gets the <em>elapsed</em> time in nanoseconds.
*
* <p>
- * This is either the time between the start and the moment this method is
called, or the amount of time between start and stop.
+ * The time is zero before starting or after resetting, increases while
running, and remains fixed while suspended or stopped. Time spent suspended is
+ * excluded. Splitting does not pause the elapsed time.
* </p>
*
* @return The <em>elapsed</em> time in nanoseconds.
- * @see System#nanoTime()
* @since 3.0
+ * @see System#nanoTime()
*/
public long getNanoTime() {
switch (runningState) {
@@ -411,11 +405,11 @@ public long getNanoTime() {
* Gets the split Duration on this StopWatch.
*
* <p>
- * This is the Duration between start and latest split.
+ * This is the elapsed Duration recorded at the latest split, excluding
time spent suspended.
* </p>
*
* @return The split Duration.
- * @throws IllegalStateException Thrown if this StopWatch has not yet been
split.
+ * @throws IllegalStateException Thrown if this StopWatch is not currently
split.
* @since 3.16.0
*/
public Duration getSplitDuration() {
@@ -426,11 +420,11 @@ public Duration getSplitDuration() {
* Gets the split time in nanoseconds.
*
* <p>
- * This is the time between start and latest split.
+ * This is the elapsed time recorded at the latest split, excluding time
spent suspended.
* </p>
*
* @return The split time in nanoseconds.
- * @throws IllegalStateException Thrown if this StopWatch has not yet been
split.
+ * @throws IllegalStateException Thrown if this StopWatch is not currently
split.
* @since 3.0
*/
public long getSplitNanoTime() {
@@ -441,9 +435,15 @@ public long getSplitNanoTime() {
}
/**
- * Gets the split list.
+ * Gets an unmodifiable view of the recorded splits in recording order.
*
- * @return The list of splits.
+ * <p>
+ * The view reflects subsequent splits, removal of the latest split by
{@link #unsplit()}, and clearing of the list by {@link #reset()} or {@link
#start()}.
+ * Each split records the elapsed duration since the start of the timing
session, excluding time spent suspended, rather than the duration since the
+ * preceding split.
+ * </p>
+ *
+ * @return An unmodifiable view of the recorded splits.
* @since 3.20.0
*/
public List<Split> getSplits() {
@@ -454,11 +454,11 @@ public List<Split> getSplits() {
* Gets the split time on this StopWatch.
*
* <p>
- * This is the time between start and latest split.
+ * This is the elapsed time recorded at the latest split, excluding time
spent suspended.
* </p>
*
* @return The split time in milliseconds.
- * @throws IllegalStateException Thrown if this StopWatch has not yet been
split.
+ * @throws IllegalStateException Thrown if this StopWatch is not currently
split.
* @since 2.1
* @deprecated Use {@link #getSplitDuration()}.
*/
@@ -468,10 +468,10 @@ public long getSplitTime() {
}
/**
- * Gets the Instant this StopWatch was started, between the current time
and midnight, January 1, 1970 UTC.
+ * Gets the Instant at which this StopWatch was started.
*
- * @return The Instant this StopWatch was started, between the current
time and midnight, January 1, 1970 UTC.
- * @throws IllegalStateException Thrown if this StopWatch has not been
started.
+ * @return The Instant at which the current timing session started.
+ * @throws IllegalStateException Thrown if this StopWatch has not been
started, or has been reset without being restarted.
* @since 3.16.0
*/
public Instant getStartInstant() {
@@ -482,10 +482,10 @@ public Instant getStartInstant() {
}
/**
- * Gets the time this StopWatch was started in milliseconds, between the
current time and midnight, January 1, 1970 UTC.
+ * Gets the start time in milliseconds since midnight, January 1, 1970 UTC.
*
- * @return The time this StopWatch was started in milliseconds, between
the current time and midnight, January 1, 1970 UTC.
- * @throws IllegalStateException Thrown if this StopWatch has not been
started.
+ * @return The start time of the current timing session in epoch
milliseconds.
+ * @throws IllegalStateException Thrown if this StopWatch has not been
started, or has been reset without being restarted.
* @since 2.4
* @deprecated Use {@link #getStartInstant()}.
*/
@@ -540,10 +540,17 @@ public long getStopTime() {
/**
* Gets the result of {@link FailableSupplier#get()} while recording the
duration of the call.
*
+ * <p>
+ * Starts or resumes this watch as needed, then calls {@link #suspend()}
when the supplier returns or throws. An already running watch keeps its current
+ * start time. The supplier must leave this watch running so it can be
suspended.
+ * Elapsed time accumulates across calls until {@link #reset()}. A stopped
watch must be reset before calling this method.
+ * </p>
+ *
* @param <T> the type of results supplied by this supplier.
* @param <E> The kind of thrown exception or error.
- * @param supplier The supplier to {@link Supplier#get()}.
+ * @param supplier The supplier to {@link FailableSupplier#get()}.
* @return A result from the given Supplier.
+ * @throws IllegalStateException Thrown if this StopWatch is stopped
before the call, or is not running when the supplier completes.
* @throws Throwable Thrown if the supplier fails.
* @since 3.18.0
*/
@@ -557,10 +564,11 @@ public <T, E extends Throwable> T getT(final
FailableSupplier<T, E> supplier) th
}
/**
- * Gets the time on this StopWatch.
+ * Gets the elapsed time on this StopWatch in milliseconds.
*
* <p>
- * This is either the time between the start and the moment this method is
called, or the amount of time between start and stop.
+ * The time is zero before starting or after resetting, increases while
running, and remains fixed while suspended or stopped. Time spent suspended is
+ * excluded. Splitting does not pause the elapsed time.
* </p>
*
* @return The time in milliseconds.
@@ -571,16 +579,16 @@ public long getTime() {
}
/**
- * Gets the time in the specified TimeUnit.
+ * Gets the elapsed time in the specified TimeUnit.
*
* <p>
- * This is either the time between the start and the moment this method is
called, or the amount of time between start and stop. The resulting time will be
- * expressed in the desired TimeUnit with any remainder rounded down. For
example, if the specified unit is {@code TimeUnit.HOURS} and this StopWatch time
- * is 59 minutes, then the result returned will be {@code 0}.
+ * The elapsed time is obtained from {@link #getNanoTime()} and expressed
in the desired TimeUnit with any remainder rounded down. For example, if the
+ * specified unit is {@code TimeUnit.HOURS} and this StopWatch time is 59
minutes, then the result returned will be {@code 0}.
* </p>
*
* @param timeUnit The unit of time, not null.
* @return The time in the specified TimeUnit, rounded down.
+ * @throws NullPointerException Thrown if the TimeUnit is null.
* @since 3.5
*/
public long getTime(final TimeUnit timeUnit) {
@@ -588,9 +596,9 @@ public long getTime(final TimeUnit timeUnit) {
}
/**
- * Tests whether this StopWatch is started. A suspended StopWatch is also
started watch.
+ * Tests whether this StopWatch is running or suspended.
*
- * @return boolean If this StopWatch is started.
+ * @return {@code true} if this StopWatch is running or suspended.
* @since 3.2
*/
public boolean isStarted() {
@@ -598,9 +606,9 @@ public boolean isStarted() {
}
/**
- * Tests whether StopWatch is stopped. this StopWatch which's not yet
started and explicitly stopped StopWatch is considered as stopped.
+ * Tests whether this StopWatch is unstarted or stopped.
*
- * @return boolean If this StopWatch is stopped.
+ * @return {@code true} if this StopWatch is unstarted or stopped.
* @since 3.2
*/
public boolean isStopped() {
@@ -610,7 +618,7 @@ public boolean isStopped() {
/**
* Tests whether this StopWatch is suspended.
*
- * @return boolean If this StopWatch is suspended.
+ * @return {@code true} if this StopWatch is suspended.
* @since 3.2
*/
public boolean isSuspended() {
@@ -628,10 +636,10 @@ private long nanosToMillis(final long nanos) {
}
/**
- * Resets this StopWatch. Stops it if need be.
+ * Resets this StopWatch to its unstarted state.
*
* <p>
- * This method clears the internal values to allow the object to be reused.
+ * This method discards the current timing session and all recorded
splits. The elapsed time becomes zero, and {@link #start()} can begin a new
session.
* </p>
*/
public void reset() {
@@ -647,7 +655,7 @@ public void reset() {
* This method resumes the watch after it was suspended. The watch will
not include time between the suspend and resume calls in the total time.
* </p>
*
- * @throws IllegalStateException Thrown if this StopWatch has not been
suspended.
+ * @throws IllegalStateException Thrown if this StopWatch is not currently
suspended.
*/
public void resume() {
if (runningState != State.SUSPENDED) {
@@ -660,7 +668,14 @@ public void resume() {
/**
* Delegates to {@link Runnable#run()} while recording the duration of the
call.
*
+ * <p>
+ * Starts or resumes this watch as needed, then calls {@link #suspend()}
when the runnable returns or throws. An already running watch keeps its current
+ * start time. The runnable must leave this watch running so it can be
suspended.
+ * Elapsed time accumulates across calls until {@link #reset()}. A stopped
watch must be reset before calling this method.
+ * </p>
+ *
* @param runnable The runnable to {@link Runnable#run()}.
+ * @throws IllegalStateException Thrown if this StopWatch is stopped
before the call, or is not running when the runnable completes.
* @since 3.18.0
*/
public void run(final Runnable runnable) {
@@ -675,8 +690,15 @@ public void run(final Runnable runnable) {
/**
* Delegates to {@link FailableRunnable#run()} while recording the
duration of the call.
*
+ * <p>
+ * Starts or resumes this watch as needed, then calls {@link #suspend()}
when the runnable returns or throws. An already running watch keeps its current
+ * start time. The runnable must leave this watch running so it can be
suspended.
+ * Elapsed time accumulates across calls until {@link #reset()}. A stopped
watch must be reset before calling this method.
+ * </p>
+ *
* @param <E> The kind of {@link Throwable}.
* @param runnable The runnable to {@link FailableRunnable#run()}.
+ * @throws IllegalStateException Thrown if this StopWatch is stopped
before the call, or is not running when the runnable completes.
* @throws Throwable Thrown by {@link FailableRunnable#run()}.
* @since 3.18.0
*/
@@ -690,11 +712,12 @@ public <E extends Throwable> void runT(final
FailableRunnable<E> runnable) throw
}
/**
- * Splits the time.
+ * Records a split of the elapsed time without stopping this StopWatch.
*
* <p>
- * This method sets the stop time of the watch to allow a time to be
extracted. The start time is unaffected, enabling {@link #unsplit()} to
continue the
- * timing from the original start point.
+ * This method appends a split to {@link #getSplits()} and makes its
duration available through {@link #getSplitDuration()}. The label is the number
of
+ * recorded splits before this call, converted to a string. The watch
continues running, and this method may be called repeatedly without calling
+ * {@link #unsplit()}.
* </p>
*
* @throws IllegalStateException Thrown if this StopWatch is not running.
@@ -709,14 +732,14 @@ public void split() {
}
/**
- * Splits the time with a label.
+ * Records a labeled split of the elapsed time without stopping this
StopWatch.
*
* <p>
- * This method sets the stop time of the watch to allow a time to be
extracted. The start time is unaffected, enabling {@link #unsplit()} to
continue the
- * timing from the original start point.
+ * This method appends a split to {@link #getSplits()} and makes its
duration available through {@link #getSplitDuration()}. The watch continues
running,
+ * and this method may be called repeatedly without calling {@link
#unsplit()}.
* </p>
*
- * @param label A message for string presentation.
+ * @param label The label stored with this split, may be null.
* @throws IllegalStateException Thrown if the StopWatch is not running.
* @since 3.20.0
*/
@@ -733,10 +756,11 @@ public void split(final String label) {
* Starts this StopWatch.
*
* <p>
- * This method starts a new timing session, clearing any previous values.
+ * This method starts a new timing session, clearing any previous timing
values and splits. Call {@link #reset()} before starting a watch that has
already
+ * been started, or {@link #resume()} to continue a suspended watch.
* </p>
*
- * @throws IllegalStateException Thrown if this StopWatch is already
running.
+ * @throws IllegalStateException Thrown if this StopWatch is running,
suspended, or stopped without being reset.
*/
public void start() {
if (runningState == State.STOPPED) {
@@ -768,10 +792,10 @@ private void startResume() {
* Stops this StopWatch.
*
* <p>
- * This method ends a new timing session, allowing the time to be
retrieved.
+ * This method ends the current timing session. A running watch records
the stop time; a suspended watch retains the time recorded when it was
suspended.
* </p>
*
- * @throws IllegalStateException Thrown if this StopWatch is not running.
+ * @throws IllegalStateException Thrown if this StopWatch is neither
running nor suspended.
*/
public void stop() {
if (runningState != State.RUNNING && runningState != State.SUSPENDED) {
@@ -813,6 +837,7 @@ public void suspend() {
* </p>
*
* @return The split time as a String.
+ * @throws IllegalStateException Thrown if this StopWatch is not currently
split.
* @since 2.1
* @since 3.10 Returns the prefix {@code "message "} if the message is set.
*/
@@ -840,13 +865,14 @@ public String toString() {
}
/**
- * Removes the split.
+ * Removes the latest recorded split.
*
* <p>
- * This method clears the stop time. The start time is unaffected,
enabling timing from the original start point to continue.
+ * This method removes the latest entry from {@link #getSplits()} and
marks the watch as unsplit. Earlier entries remain in the list, but the
split-time
+ * getters cannot be used again until another split is recorded. The
running state, elapsed time, and recorded stop Instant are unaffected.
* </p>
*
- * @throws IllegalStateException Thrown if this StopWatch has not been
split.
+ * @throws IllegalStateException Thrown if this StopWatch is not currently
split.
*/
public void unsplit() {
if (splitState != SplitState.SPLIT) {
diff --git a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
index d3e215599..fef8e1a16 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -476,6 +476,36 @@ void testSplitGetStopInstant() {
assertNotNull(watch.getStopInstant());
}
+ @Test
+ void testSplitsWithoutLabels() {
+ final StopWatch watch = StopWatch.createStarted();
+ final Instant startInstant = watch.getStartInstant();
+ watch.split();
+ final Duration firstDuration = watch.getSplitDuration();
+ watch.split();
+ final Duration secondDuration = watch.getSplitDuration();
+ watch.split();
+ final Duration thirdDuration = watch.getSplitDuration();
+
+ final List<StopWatch.Split> splits = watch.getSplits();
+ assertEquals(3, splits.size());
+ assertEquals("0", splits.get(0).getLabel());
+ assertEquals("1", splits.get(1).getLabel());
+ assertEquals("2", splits.get(2).getLabel());
+ assertEquals(firstDuration, splits.get(0).getDuration());
+ assertEquals(secondDuration, splits.get(1).getDuration());
+ assertEquals(thirdDuration, splits.get(2).getDuration());
+ assertTrue(firstDuration.compareTo(Duration.ZERO) >= 0);
+ assertTrue(secondDuration.compareTo(firstDuration) >= 0);
+ assertTrue(thirdDuration.compareTo(secondDuration) >= 0);
+ assertEquals(startInstant, watch.getStartInstant());
+ assertTrue(watch.isStarted());
+ assertFalse(watch.isSuspended());
+ watch.stop();
+ assertEquals(thirdDuration, watch.getSplitDuration());
+ assertTrue(watch.getDuration().compareTo(thirdDuration) >= 0);
+ }
+
@Test
void testSplitsWithStringLabels() {
final StopWatch watch = new StopWatch();