[LANG-1505] Add StopWatch convenience APIs to format times and create a

simple instance.
This commit is contained in:
Gary Gregory 2019-12-30 14:06:28 -05:00
parent de7b773087
commit a914a268ba
3 changed files with 47 additions and 0 deletions

View File

@ -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.">

View File

@ -160,6 +160,17 @@ boolean isSuspended() {
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.
*
@ -209,6 +220,26 @@ public StopWatch() {
super();
}
/**
* 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.

View File

@ -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;
@ -144,6 +145,20 @@ public void testBooleanStates() {
assertTrue(watch.isStopped());
}
@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();