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


The following commit(s) were added to refs/heads/master by this push:
     new a914a26  [LANG-1505] Add StopWatch convenience APIs to format times 
and create a simple instance.
a914a26 is described below

commit a914a268ba9c3ddc852bcfb0da680ae1103072dd
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Mon Dec 30 14:06:28 2019 -0500

    [LANG-1505] Add StopWatch convenience APIs to format times and create a
    simple instance.
---
 src/changes/changes.xml                            |  1 +
 .../org/apache/commons/lang3/time/StopWatch.java   | 31 ++++++++++++++++++++++
 .../apache/commons/lang3/time/StopWatchTest.java   | 15 +++++++++++
 3 files changed, 47 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b414aa8..d6983cc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -87,6 +87,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="update" dev="ggregory" due-to="Peter 
Verhas">BooleanUtils Javadoc #469.</action>
     <action                   type="update" dev="ggregory" due-to="Peter 
Verhas">Functions Javadoc #466.</action>
     <action issue="LANG-1503" type="add" dev="ggregory" due-to="XenoAmess, 
Gary Gregory">Add factory methods to Pair classes with Map.Entry input. 
#454.</action>
+    <action issue="LANG-1505" type="add" dev="ggregory" due-to="Gary 
Gregory">Add StopWatch convenience APIs to format times and create a simple 
instance.</action>
   </release>
 
   <release version="3.9" date="2019-04-09" description="New features and bug 
fixes. Requires Java 8, supports Java 9, 10, 11.">
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 3936b82..2b6a7ed 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -161,6 +161,17 @@ public class StopWatch {
     private static final long NANO_2_MILLIS = 1000000L;
 
     /**
+     * Creates a stopwatch for convenience.
+     *
+     * @return StopWatch a stopwatch.
+     *
+     * @since 3.10
+     */
+    public static StopWatch create() {
+        return new StopWatch();
+    }
+
+    /**
      * Creates a started stopwatch for convenience.
      *
      * @return StopWatch a stopwatch that's already been started.
@@ -210,6 +221,26 @@ public class StopWatch {
     }
 
     /**
+     * Returns the time formatted by {@link 
DurationFormatUtils#formatDurationHMS}.
+     *
+     * @return the time formatted by {@link 
DurationFormatUtils#formatDurationHMS}.
+     * @since 3.10
+     */
+    public String formatSplitTime() {
+        return DurationFormatUtils.formatDurationHMS(getSplitTime());
+    }
+
+    /**
+     * Returns the split time formatted by {@link 
DurationFormatUtils#formatDurationHMS}.
+     *
+     * @return the split time formatted by {@link 
DurationFormatUtils#formatDurationHMS}.
+     * @since 3.10
+     */
+    public String formatTime() {
+        return DurationFormatUtils.formatDurationHMS(getTime());
+    }
+
+    /**
      * <p>
      * Gets the time on the stopwatch in nanoseconds.
      * </p>
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 b3bf035..d88eca6 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -17,6 +17,7 @@
 package org.apache.commons.lang3.time;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -145,6 +146,20 @@ public class StopWatchTest {
     }
 
     @Test
+    public void testFormatSplitTime() throws InterruptedException {
+        final StopWatch watch = StopWatch.createStarted();
+        Thread.sleep(20);
+        watch.split();
+        assertNotEquals("00:00:00.000", watch.formatSplitTime());
+    }
+
+    @Test
+    public void testFormatTime() {
+        final StopWatch watch = StopWatch.create();
+        assertEquals("00:00:00.000", watch.formatTime());
+    }
+
+    @Test
     public void testGetStartTime() {
         final long beforeStopWatch = System.currentTimeMillis();
         final StopWatch watch = new StopWatch();

Reply via email to