You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/09/22 12:21:36 UTC

(commons-lang) branch master updated (31c7c5bde -> 4768e909e)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


    from 31c7c5bde Javadoc
     new da246244a Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier)
     new c4049be5f Remove trailing whitespace in StopWatch exception messages
     new ccaae5129 [LANG-1747]  Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier)
     new 4768e909e Javadoc

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 pom.xml                                            |   6 +
 src/changes/changes.xml                            |   2 +
 .../org/apache/commons/lang3/time/StopWatch.java   | 185 +++++++++++++++------
 .../apache/commons/lang3/time/StopWatchTest.java   | 109 ++++++++----
 4 files changed, 221 insertions(+), 81 deletions(-)


(commons-lang) 03/04: [LANG-1747] Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier)

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit ccaae51292cec152246bdf865df38a6e22e7bc6a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 22 08:15:04 2024 -0400

    [LANG-1747]  Add StopWatch.run([Failable]Runnable) and
    get([Failable]Supplier)
---
 src/changes/changes.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 647ec3a62..c4c4b7f59 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -55,7 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Remove trailing whitespace in StopWatch exception messages.</action>
     <!-- ADD -->
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
-    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>
+    <action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 75 #1267, #1277.</action>
   </release>


(commons-lang) 01/04: Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier)

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit da246244aebf91571b0689e3dee97fce0ad52b23
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 22 08:11:40 2024 -0400

    Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier)
---
 pom.xml                                            |   6 ++
 src/changes/changes.xml                            |   1 +
 .../org/apache/commons/lang3/time/StopWatch.java   | 102 +++++++++++++++++--
 .../apache/commons/lang3/time/StopWatchTest.java   | 109 +++++++++++++++------
 4 files changed, 179 insertions(+), 39 deletions(-)

diff --git a/pom.xml b/pom.xml
index cd18b14ca..496eff197 100644
--- a/pom.xml
+++ b/pom.xml
@@ -82,6 +82,12 @@
       <version>5.4.0</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>4.11.0</version>
+      <scope>test</scope>
+    </dependency>
     <!-- For Javadoc links -->
     <dependency>
       <groupId>org.apache.commons</groupId>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b9952c966..17afee8a0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="OSS-Fuzz, Gary Gregory">Rewrite ClassUtils.getClass(...) without recursion to avoid StackOverflowError on very long inputs. OSS-Fuzz Issue 42522972: apache-commons-text:StringSubstitutorInterpolatorFuzzer: Security exception in org.apache.commons.lang3.ClassUtils.getClass.</action>
     <!-- ADD -->
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 75 #1267, #1277.</action>
   </release>
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 6164c9932..5b81fa94c 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -21,10 +21,12 @@ import java.time.Duration;
 import java.time.Instant;
 import java.util.Objects;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.function.FailableConsumer;
 import org.apache.commons.lang3.function.FailableRunnable;
+import org.apache.commons.lang3.function.FailableSupplier;
 
 /**
  * {@link StopWatch} provides a convenient API for timings.
@@ -286,6 +288,23 @@ public class StopWatch {
         return DurationFormatUtils.formatDurationHMS(getTime());
     }
 
+    /**
+     * Delegates to {@link Supplier#get()} while recording the duration of the call.
+     *
+     * @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.
+     * @since 3.18.0
+     */
+    public <T> T get(final Supplier<T> supplier) {
+        startResume();
+        try {
+            return supplier.get();
+        } finally {
+            suspend();
+        }
+    }
+
     /**
      * Gets the Duration on the StopWatch.
      *
@@ -322,14 +341,16 @@ public class StopWatch {
      * @since 3.0
      */
     public long getNanoTime() {
-        if (runningState == State.STOPPED || runningState == State.SUSPENDED) {
+        switch (runningState) {
+        case STOPPED:
+        case SUSPENDED:
             return stopTimeNanos - startTimeNanos;
-        }
-        if (runningState == State.UNSTARTED) {
+        case UNSTARTED:
             return 0;
-        }
-        if (runningState == State.RUNNING) {
+        case RUNNING:
             return System.nanoTime() - startTimeNanos;
+        default:
+            break;
         }
         throw new IllegalStateException("Illegal running state has occurred.");
     }
@@ -443,6 +464,25 @@ public class StopWatch {
         return stopInstant.toEpochMilli();
     }
 
+    /**
+     * Delegates to {@link FailableSupplier#get()} while recording the duration of the call.
+     *
+     * @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()}.
+     * @return a result from the given Supplier.
+     * @throws Throwable if the supplier fails.
+     * @since 3.18.0
+     */
+    public <T, E extends Throwable> T getT(final FailableSupplier<T, E> supplier) throws Throwable {
+        startResume();
+        try {
+            return supplier.get();
+        } finally {
+            suspend();
+        }
+    }
+
     /**
      * Gets the time on the StopWatch.
      *
@@ -544,6 +584,38 @@ public class StopWatch {
         runningState = State.RUNNING;
     }
 
+    /**
+     * Delegates to {@link Runnable#run()} while recording the duration of the call.
+     *
+     * @param runnable The runnable to {@link Runnable#run()}.
+     * @since 3.18.0
+     */
+    public void run(final Runnable runnable) {
+        startResume();
+        try {
+            runnable.run();
+        } finally {
+            suspend();
+        }
+    }
+
+    /**
+     * Delegates to {@link FailableRunnable#run()} while recording the duration of the call.
+     *
+     * @param <E>      The kind of {@link Throwable}.
+     * @param runnable The runnable to {@link FailableRunnable#run()}.
+     * @throws Throwable Thrown by {@link FailableRunnable#run()}.
+     * @since 3.18.0
+     */
+    public <E extends Throwable> void runT(final FailableRunnable<E> runnable) throws Throwable {
+        startResume();
+        try {
+            runnable.run();
+        } finally {
+            suspend();
+        }
+    }
+
     /**
      * Splits the time.
      *
@@ -563,7 +635,7 @@ public class StopWatch {
     }
 
     /**
-     * Starts the StopWatch.
+     * Starts this StopWatch.
      *
      * <p>
      * This method starts a new timing session, clearing any previous values.
@@ -584,7 +656,18 @@ public class StopWatch {
     }
 
     /**
-     * Stops the StopWatch.
+     * Starts or resumes this StopWatch.
+     */
+    private void startResume() {
+        if (isStopped()) {
+            start();
+        } else if (isSuspended()) {
+            resume();
+        }
+    }
+
+    /**
+     * Stops this StopWatch.
      *
      * <p>
      * This method ends a new timing session, allowing the time to be retrieved.
@@ -604,7 +687,7 @@ public class StopWatch {
     }
 
     /**
-     * Suspends the StopWatch for later resumption.
+     * Suspends this StopWatch for later resumption.
      *
      * <p>
      * This method suspends the watch until it is resumed. The watch will not include time between the suspend and resume calls in the total time.
@@ -656,7 +739,7 @@ public class StopWatch {
     }
 
     /**
-     * Removes a split.
+     * Removes the split.
      *
      * <p>
      * This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.
@@ -671,4 +754,5 @@ public class StopWatch {
         splitState = SplitState.UNSPLIT;
     }
 
+
 }
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 04986fba3..48f06ed44 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -27,9 +27,11 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.IOException;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.commons.lang3.AbstractLangTest;
 import org.apache.commons.lang3.ThreadUtils;
@@ -50,17 +52,13 @@ public class StopWatchTest extends AbstractLangTest {
 
     /**
      * <p>
-     * Creates a suspended StopWatch object which appears to have elapsed for the requested amount of time in
-     * nanoseconds.
+     * Creates a suspended StopWatch object which appears to have elapsed for the requested amount of time in nanoseconds.
      * <p>
      * <p>
      *
      * <pre>
      * // Create a mock StopWatch with a time of 2:59:01.999
-     * final long nanos = TimeUnit.HOURS.toNanos(2)
-     *         + TimeUnit.MINUTES.toNanos(59)
-     *         + TimeUnit.SECONDS.toNanos(1)
-     *         + TimeUnit.MILLISECONDS.toNanos(999);
+     * final long nanos = TimeUnit.HOURS.toNanos(2) + TimeUnit.MINUTES.toNanos(59) + TimeUnit.SECONDS.toNanos(1) + TimeUnit.MILLISECONDS.toNanos(999);
      * final StopWatch watch = createMockStopWatch(nanos);
      * </pre>
      *
@@ -100,42 +98,31 @@ public class StopWatchTest extends AbstractLangTest {
     @Test
     public void testBadStates() {
         final StopWatch watch = new StopWatch();
-        assertThrows(IllegalStateException.class, watch::stop,
-            "Calling stop on an unstarted StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::stop, "Calling stop on an unstarted StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::suspend,
-            "Calling suspend on an unstarted StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::suspend, "Calling suspend on an unstarted StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::split,
-            "Calling split on a non-running StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::split, "Calling split on a non-running StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::unsplit,
-            "Calling unsplit on an unsplit StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::unsplit, "Calling unsplit on an unsplit StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::resume,
-            "Calling resume on an unsuspended StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::resume, "Calling resume on an unsuspended StopWatch should throw an exception. ");
 
         watch.start();
 
-        assertThrows(IllegalStateException.class, watch::start,
-            "Calling start on a started StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::start, "Calling start on a started StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::unsplit,
-            "Calling unsplit on an unsplit StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::unsplit, "Calling unsplit on an unsplit StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::getSplitTime,
-                "Calling getSplitTime on an unsplit StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::getSplitTime, "Calling getSplitTime on an unsplit StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::getSplitDuration,
-                "Calling getSplitTime on an unsplit StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::getSplitDuration, "Calling getSplitTime on an unsplit StopWatch should throw an exception. ");
 
-        assertThrows(IllegalStateException.class, watch::resume,
-            "Calling resume on an unsuspended StopWatch should throw an exception. ");
+        assertThrows(IllegalStateException.class, watch::resume, "Calling resume on an unsuspended StopWatch should throw an exception. ");
 
         watch.stop();
 
-        assertThrows(IllegalStateException.class, watch::start,
-            "Calling start on a stopped StopWatch should throw an exception as it needs to be reset. ");
+        assertThrows(IllegalStateException.class, watch::start, "Calling start on a stopped StopWatch should throw an exception as it needs to be reset. ");
     }
 
     @Test
@@ -197,6 +184,33 @@ public class StopWatchTest extends AbstractLangTest {
         assertThat("formatTime", formatTime, not(startsWith(MESSAGE)));
     }
 
+    @Test
+    public void testGet() throws Throwable {
+        final StopWatch watch = new StopWatch();
+        final AtomicInteger i = new AtomicInteger();
+        assertEquals(1, watch.get(i::incrementAndGet));
+        assertEquals(2, watch.getT(i::incrementAndGet));
+        final IOException e = assertThrows(IOException.class, () -> watch.getT(this::throwIOException));
+        assertEquals("A", e.getMessage());
+        // test state
+        assertTrue(watch.isSuspended());
+        assertEquals(3, watch.get(() -> {
+            assertTrue(watch.isStarted());
+            return i.incrementAndGet();
+        }));
+        assertTrue(watch.isSuspended());
+        final long nanos1 = watch.getDuration().toNanos();
+        assertTrue(nanos1 >= 0);
+        // test state
+        assertTrue(watch.isSuspended());
+        assertEquals(4, watch.getT(() -> {
+            assertTrue(watch.isStarted());
+            return i.incrementAndGet();
+        }));
+        assertTrue(watch.isSuspended());
+        assertTrue(watch.getDuration().toNanos() >= nanos1);
+    }
+
     @Test
     public void testGetDuration() throws InterruptedException {
         final StopWatch watch = new StopWatch();
@@ -301,6 +315,37 @@ public class StopWatchTest extends AbstractLangTest {
         assertThat("stopWatch.toSplitString", stopWatch.toSplitString(), startsWith(MESSAGE));
     }
 
+    @Test
+    public void testRun() throws Throwable {
+        final StopWatch watch = new StopWatch();
+        final AtomicInteger i = new AtomicInteger();
+        watch.run(i::incrementAndGet);
+        assertEquals(1, i.get());
+        watch.runT(i::incrementAndGet);
+        assertEquals(2, i.get());
+        final IOException e = assertThrows(IOException.class, () -> watch.runT(this::throwIOException));
+        assertEquals("A", e.getMessage());
+        // test state
+        assertTrue(watch.isSuspended());
+        watch.run(() -> {
+            assertTrue(watch.isStarted());
+            i.incrementAndGet();
+        });
+        assertEquals(3, i.get());
+        assertTrue(watch.isSuspended());
+        final long nanos1 = watch.getDuration().toNanos();
+        assertTrue(nanos1 > 0);
+        // test state
+        assertTrue(watch.isSuspended());
+        watch.runT(() -> {
+            assertTrue(watch.isStarted());
+            i.incrementAndGet();
+        });
+        assertEquals(4, i.get());
+        assertTrue(watch.isSuspended());
+        assertTrue(watch.getDuration().toNanos() >= nanos1);
+    }
+
     @Test
     public void testSimple() throws InterruptedException {
         final StopWatch watch = StopWatch.createStarted();
@@ -421,14 +466,14 @@ public class StopWatchTest extends AbstractLangTest {
         assertTrue(totalTimeFromNanos >= sleepMillisX2, () -> String.format("totalTimeFromNanos %s >= sleepMillisX2 %s", totalTimeFromNanos, sleepMillisX2));
         assertTrue(totalDuration.compareTo(Duration.ofMillis(sleepMillisX2)) >= 0,
                 () -> String.format("totalDuration >= sleepMillisX2", totalDuration, sleepMillisX2));
-        ;
+
         // Be lenient for slow running builds
         final long testTooLongMillis = sleepMillis * 100;
         assertTrue(totalTimeFromNanos < testTooLongMillis,
                 () -> String.format("totalTimeFromNanos %s < testTooLongMillis %s", totalTimeFromNanos, testTooLongMillis));
         assertTrue(totalDuration.compareTo(Duration.ofMillis(testTooLongMillis)) < 0,
                 () -> String.format("totalDuration %s < testTooLongMillis %s", totalDuration, testTooLongMillis));
-        ;
+
     }
 
     @Test
@@ -471,4 +516,8 @@ public class StopWatchTest extends AbstractLangTest {
         final String splitStr = watch.toString();
         assertEquals(SPLIT_CLOCK_STR_LEN + MESSAGE.length() + 1, splitStr.length(), "Formatted split string not the correct length");
     }
+
+    private int throwIOException() throws IOException {
+        throw new IOException("A");
+    }
 }


(commons-lang) 04/04: Javadoc

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 4768e909e3652f5360d01843788d0423fa792ff7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 22 08:21:17 2024 -0400

    Javadoc
---
 .../org/apache/commons/lang3/time/StopWatch.java   | 71 +++++++++++-----------
 1 file changed, 35 insertions(+), 36 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 86eea98ca..fefb1e4f6 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -151,23 +151,23 @@ public class StopWatch {
         };
 
         /**
-         * Tests whether the StopWatch is started. A suspended StopWatch is also started.
+         * Tests whether this StopWatch is started. A suspended StopWatch is also started.
          *
-         * @return boolean If the StopWatch is started.
+         * @return boolean If this StopWatch is started.
          */
         abstract boolean isStarted();
 
         /**
-         * Tests whether the StopWatch is stopped. A StopWatch which is not yet started and explicitly stopped is considered stopped.
+         * Tests whether this StopWatch is stopped. A StopWatch which is not yet started and explicitly stopped is considered stopped.
          *
-         * @return boolean If the StopWatch is stopped.
+         * @return boolean If this StopWatch is stopped.
          */
         abstract boolean isStopped();
 
         /**
-         * Tests whether the StopWatch is suspended.
+         * Tests whether this StopWatch is suspended.
          *
-         * @return boolean If the StopWatch is suspended.
+         * @return boolean If this StopWatch is suspended.
          */
         abstract boolean isSuspended();
     }
@@ -206,12 +206,12 @@ public class StopWatch {
     private final String message;
 
     /**
-     * The current running state of the StopWatch.
+     * The current running state of this StopWatch.
      */
     private State runningState = State.UNSTARTED;
 
     /**
-     * Whether the StopWatch has a split time recorded.
+     * Whether this StopWatch has a split time recorded.
      */
     private SplitState splitState = SplitState.UNSPLIT;
 
@@ -228,7 +228,7 @@ public class StopWatch {
      * 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, only later versions use nanoseconds.
+     * On Java 8, Instant has millisecond precision, later versions use nanoseconds.
      * </p>
      */
     private Instant startInstant;
@@ -239,7 +239,7 @@ public class StopWatch {
      * 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, only later versions use nanoseconds.
+     * On Java 8, Instant has millisecond precision, later versions use nanoseconds.
      * </p>
      */
     private Instant stopInstant;
@@ -306,7 +306,7 @@ public class StopWatch {
     }
 
     /**
-     * Gets the Duration on the StopWatch.
+     * Gets the 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.
@@ -356,7 +356,7 @@ public class StopWatch {
     }
 
     /**
-     * Gets the split Duration on the StopWatch.
+     * Gets the split Duration on this StopWatch.
      *
      * <p>
      * This is the Duration between start and latest split.
@@ -364,7 +364,7 @@ public class StopWatch {
      *
      * @return the split Duration
      *
-     * @throws IllegalStateException if the StopWatch has not yet been split.
+     * @throws IllegalStateException if this StopWatch has not yet been split.
      * @since 3.16.0
      */
     public Duration getSplitDuration() {
@@ -380,7 +380,7 @@ public class StopWatch {
      *
      * @return the split time in nanoseconds
      *
-     * @throws IllegalStateException if the StopWatch has not yet been split.
+     * @throws IllegalStateException if this StopWatch has not yet been split.
      * @since 3.0
      */
     public long getSplitNanoTime() {
@@ -391,7 +391,7 @@ public class StopWatch {
     }
 
     /**
-     * Gets the split time on the StopWatch.
+     * Gets the split time on this StopWatch.
      *
      * <p>
      * This is the time between start and latest split.
@@ -399,7 +399,7 @@ public class StopWatch {
      *
      * @return the split time in milliseconds
      *
-     * @throws IllegalStateException if the StopWatch has not yet been split.
+     * @throws IllegalStateException if this StopWatch has not yet been split.
      * @since 2.1
      * @deprecated Use {@link #getSplitDuration()}.
      */
@@ -484,7 +484,7 @@ public class StopWatch {
     }
 
     /**
-     * Gets the time on the StopWatch.
+     * Gets the time on this StopWatch.
      *
      * <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.
@@ -503,8 +503,8 @@ public class StopWatch {
      *
      * <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 the StopWatch time is
-     * 59 minutes, then the result returned will be {@code 0}.
+     * 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
@@ -516,9 +516,9 @@ public class StopWatch {
     }
 
     /**
-     * Tests whether the StopWatch is started. A suspended StopWatch is also started watch.
+     * Tests whether this StopWatch is started. A suspended StopWatch is also started watch.
      *
-     * @return boolean If the StopWatch is started.
+     * @return boolean If this StopWatch is started.
      * @since 3.2
      */
     public boolean isStarted() {
@@ -526,9 +526,9 @@ public class StopWatch {
     }
 
     /**
-     * Tests whether StopWatch is stopped. The StopWatch which's not yet started and explicitly stopped StopWatch is considered as stopped.
+     * Tests whether StopWatch is stopped. this StopWatch which's not yet started and explicitly stopped StopWatch is considered as stopped.
      *
-     * @return boolean If the StopWatch is stopped.
+     * @return boolean If this StopWatch is stopped.
      * @since 3.2
      */
     public boolean isStopped() {
@@ -536,9 +536,9 @@ public class StopWatch {
     }
 
     /**
-     * Tests whether the StopWatch is suspended.
+     * Tests whether this StopWatch is suspended.
      *
-     * @return boolean If the StopWatch is suspended.
+     * @return boolean If this StopWatch is suspended.
      * @since 3.2
      */
     public boolean isSuspended() {
@@ -556,7 +556,7 @@ public class StopWatch {
     }
 
     /**
-     * Resets the StopWatch. Stops it if need be.
+     * Resets this StopWatch. Stops it if need be.
      *
      * <p>
      * This method clears the internal values to allow the object to be reused.
@@ -568,13 +568,13 @@ public class StopWatch {
     }
 
     /**
-     * Resumes the StopWatch after a suspend.
+     * Resumes this StopWatch after a suspend.
      *
      * <p>
      * 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 if the StopWatch has not been suspended.
+     * @throws IllegalStateException if this StopWatch has not been suspended.
      */
     public void resume() {
         if (runningState != State.SUSPENDED) {
@@ -624,7 +624,7 @@ public class StopWatch {
      * timing from the original start point.
      * </p>
      *
-     * @throws IllegalStateException if the StopWatch is not running.
+     * @throws IllegalStateException if this StopWatch is not running.
      */
     public void split() {
         if (runningState != State.RUNNING) {
@@ -641,7 +641,7 @@ public class StopWatch {
      * This method starts a new timing session, clearing any previous values.
      * </p>
      *
-     * @throws IllegalStateException if the StopWatch is already running.
+     * @throws IllegalStateException if this StopWatch is already running.
      */
     public void start() {
         if (runningState == State.STOPPED) {
@@ -673,7 +673,7 @@ public class StopWatch {
      * This method ends a new timing session, allowing the time to be retrieved.
      * </p>
      *
-     * @throws IllegalStateException if the StopWatch is not running.
+     * @throws IllegalStateException if this StopWatch is not running.
      */
     public void stop() {
         if (runningState != State.RUNNING && runningState != State.SUSPENDED) {
@@ -693,7 +693,7 @@ public class StopWatch {
      * This method suspends the watch until it is resumed. The watch will not include time between the suspend and resume calls in the total time.
      * </p>
      *
-     * @throws IllegalStateException if the StopWatch is not currently running.
+     * @throws IllegalStateException if this StopWatch is not currently running.
      */
     public void suspend() {
         if (runningState != State.RUNNING) {
@@ -705,7 +705,7 @@ public class StopWatch {
     }
 
     /**
-     * Gets a summary of the split time that the StopWatch recorded as a string.
+     * Gets a summary of the split time that this StopWatch recorded as a string.
      *
      * <p>
      * The format used is ISO 8601-like, [<i>message</i> ]<i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
@@ -722,7 +722,7 @@ public class StopWatch {
     }
 
     /**
-     * Gets a summary of the time that the StopWatch recorded as a string.
+     * Gets a summary of the time that this StopWatch recorded as a string.
      *
      * <p>
      * The format used is ISO 8601-like, [<i>message</i> ]<i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
@@ -745,7 +745,7 @@ public class StopWatch {
      * This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.
      * </p>
      *
-     * @throws IllegalStateException if the StopWatch has not been split.
+     * @throws IllegalStateException if this StopWatch has not been split.
      */
     public void unsplit() {
         if (splitState != SplitState.SPLIT) {
@@ -754,5 +754,4 @@ public class StopWatch {
         splitState = SplitState.UNSPLIT;
     }
 
-
 }


(commons-lang) 02/04: Remove trailing whitespace in StopWatch exception messages

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit c4049be5f7e7ab076ae99793d647a0e0926c843c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 22 08:12:54 2024 -0400

    Remove trailing whitespace in StopWatch exception messages
---
 src/changes/changes.xml                                    |  1 +
 src/main/java/org/apache/commons/lang3/time/StopWatch.java | 14 +++++++-------
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 17afee8a0..647ec3a62 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -52,6 +52,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Speed up and sanitize StopWatchTest.</action>
     <action                   type="fix" dev="ggregory" due-to="Fabrice Benhamouda">Fix handling of non-ASCII letters and numbers in RandomStringUtils #1273.</action>
     <action                   type="fix" dev="ggregory" due-to="OSS-Fuzz, Gary Gregory">Rewrite ClassUtils.getClass(...) without recursion to avoid StackOverflowError on very long inputs. OSS-Fuzz Issue 42522972: apache-commons-text:StringSubstitutorInterpolatorFuzzer: Security exception in org.apache.commons.lang3.ClassUtils.getClass.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Remove trailing whitespace in StopWatch exception messages.</action>
     <!-- ADD -->
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>
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 5b81fa94c..86eea98ca 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -578,7 +578,7 @@ public class StopWatch {
      */
     public void resume() {
         if (runningState != State.SUSPENDED) {
-            throw new IllegalStateException("Stopwatch must be suspended to resume. ");
+            throw new IllegalStateException("Stopwatch must be suspended to resume.");
         }
         startTimeNanos += System.nanoTime() - stopTimeNanos;
         runningState = State.RUNNING;
@@ -628,7 +628,7 @@ public class StopWatch {
      */
     public void split() {
         if (runningState != State.RUNNING) {
-            throw new IllegalStateException("Stopwatch is not running. ");
+            throw new IllegalStateException("Stopwatch is not running.");
         }
         stopTimeNanos = System.nanoTime();
         splitState = SplitState.SPLIT;
@@ -645,10 +645,10 @@ public class StopWatch {
      */
     public void start() {
         if (runningState == State.STOPPED) {
-            throw new IllegalStateException("Stopwatch must be reset before being restarted. ");
+            throw new IllegalStateException("Stopwatch must be reset before being restarted.");
         }
         if (runningState != State.UNSTARTED) {
-            throw new IllegalStateException("Stopwatch already started. ");
+            throw new IllegalStateException("Stopwatch already started.");
         }
         startTimeNanos = System.nanoTime();
         startInstant = Instant.now();
@@ -677,7 +677,7 @@ public class StopWatch {
      */
     public void stop() {
         if (runningState != State.RUNNING && runningState != State.SUSPENDED) {
-            throw new IllegalStateException("Stopwatch is not running. ");
+            throw new IllegalStateException("Stopwatch is not running.");
         }
         if (runningState == State.RUNNING) {
             stopTimeNanos = System.nanoTime();
@@ -697,7 +697,7 @@ public class StopWatch {
      */
     public void suspend() {
         if (runningState != State.RUNNING) {
-            throw new IllegalStateException("Stopwatch must be running to suspend. ");
+            throw new IllegalStateException("Stopwatch must be running to suspend.");
         }
         stopTimeNanos = System.nanoTime();
         stopInstant = Instant.now();
@@ -749,7 +749,7 @@ public class StopWatch {
      */
     public void unsplit() {
         if (splitState != SplitState.SPLIT) {
-            throw new IllegalStateException("Stopwatch has not been split. ");
+            throw new IllegalStateException("Stopwatch has not been split.");
         }
         splitState = SplitState.UNSPLIT;
     }