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 2875a65756da4c72e8d188555b9639857e73684a Author: Gary Gregory <[email protected]> AuthorDate: Thu Jul 18 14:24:24 2024 -0400 Add StopWatch.getSplitDuration() and deprecate getSplitTime() --- src/changes/changes.xml | 3 ++- .../org/apache/commons/lang3/time/StopWatch.java | 21 ++++++++++++++++- .../apache/commons/lang3/time/StopWatchTest.java | 26 +++++++++++++++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1fd5e7377..ab34f5567 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -45,9 +45,10 @@ The <action> type attribute can be add,update,fix,remove. <title>Apache Commons Lang Release Notes</title> </properties> <body> - <release version="3.15.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required."> + <release version="3.16.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required."> <!-- FIX --> <!-- ADD --> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getSplitDuration() and deprecate getSplitTime().</action> <!-- UPDATE --> </release> <release version="3.15.0" date="2024-07-13" description="New features and bug fixes (Java 8 or above)."> 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 c4587c488..a811862fc 100644 --- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java +++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.time; +import java.time.Duration; import java.util.Objects; import java.util.concurrent.TimeUnit; @@ -256,7 +257,7 @@ public class StopWatch { * @since 3.10 */ public String formatSplitTime() { - return DurationFormatUtils.formatDurationHMS(getSplitTime()); + return DurationFormatUtils.formatDurationHMS(getSplitDuration().toMillis()); } /** @@ -303,6 +304,22 @@ public class StopWatch { throw new IllegalStateException("Illegal running state has occurred."); } + /** + * Gets the split Duration on the StopWatch. + * + * <p> + * This is the Duration between start and latest split. + * </p> + * + * @return the split Duration + * + * @throws IllegalStateException if the StopWatch has not yet been split. + * @since 3.16.0 + */ + public Duration getSplitDuration() { + return Duration.ofNanos(getSplitNanoTime()); + } + /** * Gets the split time in nanoseconds. * @@ -333,7 +350,9 @@ public class StopWatch { * * @throws IllegalStateException if the StopWatch has not yet been split. * @since 2.1 + * @deprecated Use {@link #getSplitDuration()}. */ + @Deprecated public long getSplitTime() { return nanosToMillis(getSplitNanoTime()); } 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 498c6f063..295ff1261 100644 --- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java +++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java @@ -72,6 +72,10 @@ public class StopWatchTest extends AbstractLangTest { private StopWatch createMockStopWatch(final long nanos) { final StopWatch watch = StopWatch.createStarted(); watch.suspend(); + return set(watch, nanos); + } + + private StopWatch set(final StopWatch watch, final long nanos) { try { final long currentNanos = System.nanoTime(); FieldUtils.writeField(watch, "startTimeNanos", currentNanos - nanos, true); @@ -86,7 +90,9 @@ public class StopWatchTest extends AbstractLangTest { ThreadUtils.sleep(duration); } - // test bad states + /** + * Tests bad states. + */ @Test public void testBadStates() { final StopWatch watch = new StopWatch(); @@ -114,7 +120,10 @@ public class StopWatchTest extends AbstractLangTest { "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. "); + "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. "); @@ -184,6 +193,17 @@ public class StopWatchTest extends AbstractLangTest { assertThat("formatTime", formatTime, not(startsWith(MESSAGE))); } + @Test + public void testGetSplitDuration() { + // Create a mock StopWatch with a time of 2:59:01.999 + // @formatter:off + final StopWatch watch = StopWatch.createStarted(); + watch.split(); + set(watch, 123456); + // @formatter:on + assertEquals(Duration.ofNanos(123456), watch.getSplitDuration()); + } + @Test public void testGetStartTime() { final long beforeStopWatchMillis = System.currentTimeMillis(); @@ -246,7 +266,6 @@ public class StopWatchTest extends AbstractLangTest { + TimeUnit.SECONDS.toNanos(1) + TimeUnit.MILLISECONDS.toNanos(999)); // @formatter:on - assertEquals(2L, watch.getTime(TimeUnit.HOURS)); assertEquals(179L, watch.getTime(TimeUnit.MINUTES)); assertEquals(10741L, watch.getTime(TimeUnit.SECONDS)); @@ -285,6 +304,7 @@ public class StopWatchTest extends AbstractLangTest { // slept ~550 millis watch.split(); final long splitTime = watch.getSplitTime(); + assertEquals(splitTime, watch.getSplitDuration().toMillis()); final String splitStr = watch.toSplitString(); sleep(MILLIS_550); // slept ~1100 millis
