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 5551df008 Clear stale StopWatch stop state when starting a new session
5551df008 is described below

commit 5551df00882bc5e6a70ccc730cc321e527529bf7
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 18 15:56:19 2026 -0500

    Clear stale StopWatch stop state when starting a new session
    
    Reset the stop Instant and nanosecond timestamp so restarted watches
    behave like freshly started watches.
    
    Clarify stop timestamp Javadoc and add regression coverage for restarts
    after stopping, suspension, and both split variants.
    
    Assisted-By: OpenAI Codex
---
 .../org/apache/commons/lang3/time/StopWatch.java   | 28 ++++++++---
 .../apache/commons/lang3/time/StopWatchTest.java   | 58 ++++++++++++++++++++++
 2 files changed, 80 insertions(+), 6 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 622234fde..45fd5f4e1 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -495,10 +495,19 @@ public long getStartTime() {
     }
 
     /**
-     * Gets the Instant this StopWatch was stopped, between the current time 
and midnight, January 1, 1970 UTC.
+     * Gets the most recently recorded stop Instant.
      *
-     * @return The Instant this StopWatch was stopped in milliseconds, between 
the current time and midnight, January 1, 1970 UTC.
-     * @throws IllegalStateException Thrown if this StopWatch has not been 
started.
+     * <p>
+     * The Instant is updated by {@link #split()}, {@link #split(String)}, and 
{@link #suspend()}, and by {@link #stop()} if the watch is running.
+     * Stopping a suspended watch retains the Instant recorded when it was 
suspended. Resuming or unsplitting the watch also retains the recorded Instant.
+     * </p>
+     *
+     * <p>
+     * After {@link #start()}, this method returns {@code null} until a stop 
Instant is recorded in the new timing session.
+     * </p>
+     *
+     * @return The most recently recorded stop Instant, or {@code null} if 
none has been recorded in the current timing session.
+     * @throws IllegalStateException Thrown if this StopWatch has not been 
started, or has been reset without being restarted.
      * @since 3.16.0
      */
     public Instant getStopInstant() {
@@ -509,10 +518,15 @@ public Instant getStopInstant() {
     }
 
     /**
-     * Gets the time this StopWatch was stopped in milliseconds, between the 
current time and midnight, January 1, 1970 UTC.
+     * Gets the most recently recorded stop time in milliseconds since 
midnight, January 1, 1970 UTC.
      *
-     * @return The time this StopWatch was stopped in milliseconds, between 
the current time and midnight, January 1, 1970 UTC.
-     * @throws IllegalStateException Thrown if this StopWatch has not been 
started.
+     * <p>
+     * This method returns the Instant from {@link #getStopInstant()} as epoch 
milliseconds, or {@code 0} if no stop Instant has been recorded in the current
+     * timing session.
+     * </p>
+     *
+     * @return The most recently recorded stop time in epoch milliseconds, or 
{@code 0} if none has been recorded in the current timing session.
+     * @throws IllegalStateException Thrown if this StopWatch has not been 
started, or has been reset without being restarted.
      * @since 3.12.0
      * @deprecated Use {@link #getStopInstant()}.
      */
@@ -733,6 +747,8 @@ public void start() {
         }
         startTimeNanos = System.nanoTime();
         startInstant = Instant.now();
+        stopTimeNanos = 0;
+        stopInstant = null;
         runningState = State.RUNNING;
         splits.clear();
     }
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 c48a86b95..d3e215599 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -49,6 +49,22 @@ class StopWatchTest extends AbstractLangTest {
     private static final String ZERO_HOURS_PREFIX = "00:";
     private static final String ZERO_TIME_ELAPSED = "00:00:00.000";
 
+    private void assertResetAndStartClearsPreviousStopState(final StopWatch 
watch) {
+        assertNotNull(watch.getStopInstant());
+        watch.reset();
+        assertThrows(IllegalStateException.class, watch::getStopInstant);
+        assertThrows(IllegalStateException.class, watch::getStopTime);
+        watch.start();
+        assertNull(watch.getStopInstant());
+        assertEquals(0, watch.getStopTime());
+        assertTrue(watch.getSplits().isEmpty());
+        assertThrows(IllegalStateException.class, watch::getSplitDuration);
+        // Stopping again records the new session's stop time.
+        watch.stop();
+        assertNotNull(watch.getStopInstant());
+        assertEquals(watch.getStopInstant().toEpochMilli(), 
watch.getStopTime());
+    }
+
     /**
      * <p>
      * Creates a suspended StopWatch object which appears to have elapsed for 
the requested amount of time in nanoseconds.
@@ -338,6 +354,48 @@ void testMessage() {
         assertTrue(stopWatch.toSplitString().startsWith(MESSAGE), 
"stopWatch.toSplitString");
     }
 
+    @Test
+    void testResetAndStartClearsPreviousLabeledSplitState() {
+        final StopWatch watch = StopWatch.createStarted();
+        watch.split("previous session");
+        assertResetAndStartClearsPreviousStopState(watch);
+    }
+
+    @Test
+    void testResetAndStartClearsPreviousSplitState() {
+        final StopWatch watch = StopWatch.createStarted();
+        watch.split();
+        assertResetAndStartClearsPreviousStopState(watch);
+    }
+
+    @Test
+    void testResetAndStartClearsPreviousStopState() {
+        // A running watch that was never stopped has no stop time.
+        final StopWatch fresh = StopWatch.createStarted();
+        assertNull(fresh.getStopInstant());
+        assertEquals(0, fresh.getStopTime());
+        // A restarted watch must not report the stop time of its previous 
session.
+        final StopWatch watch = StopWatch.createStarted();
+        watch.stop();
+        assertResetAndStartClearsPreviousStopState(watch);
+    }
+
+    @Test
+    void testResetAndStartClearsPreviousSuspendedState() {
+        final StopWatch watch = StopWatch.createStarted();
+        watch.suspend();
+        assertResetAndStartClearsPreviousStopState(watch);
+    }
+
+    @Test
+    void testResetAndStartClearsPreviousSuspendedStopState() {
+        final StopWatch watch = StopWatch.createStarted();
+        watch.suspend();
+        assertNotNull(watch.getStopInstant());
+        watch.stop();
+        assertResetAndStartClearsPreviousStopState(watch);
+    }
+
     @Test
     void testRun() throws Throwable {
         final StopWatch watch = new StopWatch();

Reply via email to