[LANG-1505] Add StopWatch convenience APIs to format times and create a
simple instance.
This commit is contained in:
parent
de7b773087
commit
a914a268ba
|
@ -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">BooleanUtils Javadoc #469.</action>
|
||||||
<action type="update" dev="ggregory" due-to="Peter Verhas">Functions Javadoc #466.</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-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>
|
||||||
|
|
||||||
<release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">
|
<release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">
|
||||||
|
|
|
@ -160,6 +160,17 @@ public class StopWatch {
|
||||||
|
|
||||||
private static final long NANO_2_MILLIS = 1000000L;
|
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.
|
* Creates a started stopwatch for convenience.
|
||||||
*
|
*
|
||||||
|
@ -209,6 +220,26 @@ public class StopWatch {
|
||||||
super();
|
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>
|
* <p>
|
||||||
* Gets the time on the stopwatch in nanoseconds.
|
* Gets the time on the stopwatch in nanoseconds.
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package org.apache.commons.lang3.time;
|
package org.apache.commons.lang3.time;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
@ -144,6 +145,20 @@ public class StopWatchTest {
|
||||||
assertTrue(watch.isStopped());
|
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
|
@Test
|
||||||
public void testGetStartTime() {
|
public void testGetStartTime() {
|
||||||
final long beforeStopWatch = System.currentTimeMillis();
|
final long beforeStopWatch = System.currentTimeMillis();
|
||||||
|
|
Loading…
Reference in New Issue