Add StopWatch.getStartInstant() and deprecate getStartTime()
This commit is contained in:
parent
2875a65756
commit
f1aed3eacb
|
@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
|
||||||
<!-- FIX -->
|
<!-- FIX -->
|
||||||
<!-- ADD -->
|
<!-- ADD -->
|
||||||
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getSplitDuration() and deprecate getSplitTime().</action>
|
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getSplitDuration() and deprecate getSplitTime().</action>
|
||||||
|
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getStartInstant() and deprecate getStartTime().</action>
|
||||||
<!-- UPDATE -->
|
<!-- UPDATE -->
|
||||||
</release>
|
</release>
|
||||||
<release version="3.15.0" date="2024-07-13" description="New features and bug fixes (Java 8 or above).">
|
<release version="3.15.0" date="2024-07-13" description="New features and bug fixes (Java 8 or above).">
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
package org.apache.commons.lang3.time;
|
package org.apache.commons.lang3.time;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -357,13 +358,26 @@ public class StopWatch {
|
||||||
return nanosToMillis(getSplitNanoTime());
|
return nanosToMillis(getSplitNanoTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
|
||||||
|
*
|
||||||
|
* @return the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
|
||||||
|
* @throws IllegalStateException if this StopWatch has not been started
|
||||||
|
* @since 3.16.0
|
||||||
|
*/
|
||||||
|
public Instant getStartInstant() {
|
||||||
|
return Instant.ofEpochMilli(getStartTime());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
|
* Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
|
||||||
*
|
*
|
||||||
* @return the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
|
* @return the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
|
||||||
* @throws IllegalStateException if this StopWatch has not been started
|
* @throws IllegalStateException if this StopWatch has not been started
|
||||||
* @since 2.4
|
* @since 2.4
|
||||||
|
* @deprecated Use {@link #getStartInstant()}.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public long getStartTime() {
|
public long getStartTime() {
|
||||||
if (this.runningState == State.UNSTARTED) {
|
if (this.runningState == State.UNSTARTED) {
|
||||||
throw new IllegalStateException("Stopwatch has not been started");
|
throw new IllegalStateException("Stopwatch has not been started");
|
||||||
|
|
|
@ -31,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.apache.commons.lang3.AbstractLangTest;
|
import org.apache.commons.lang3.AbstractLangTest;
|
||||||
|
@ -204,20 +205,33 @@ public class StopWatchTest extends AbstractLangTest {
|
||||||
assertEquals(Duration.ofNanos(123456), watch.getSplitDuration());
|
assertEquals(Duration.ofNanos(123456), watch.getSplitDuration());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetStartInstant() {
|
||||||
|
final long beforeStopWatchMillis = System.currentTimeMillis();
|
||||||
|
final StopWatch watch = new StopWatch();
|
||||||
|
assertThrows(IllegalStateException.class, watch::getStartInstant, "Calling getStartInstant on an unstarted StopWatch should throw an exception");
|
||||||
|
watch.start();
|
||||||
|
|
||||||
|
watch.getStartInstant();
|
||||||
|
assertThat("getStartInstant", watch.getStartInstant(), greaterThanOrEqualTo(Instant.ofEpochMilli(beforeStopWatchMillis)));
|
||||||
|
|
||||||
|
watch.reset();
|
||||||
|
assertThrows(IllegalStateException.class, watch::getStartInstant,
|
||||||
|
"Calling getStartInstant on a reset, but unstarted StopWatch should throw an exception");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetStartTime() {
|
public void testGetStartTime() {
|
||||||
final long beforeStopWatchMillis = System.currentTimeMillis();
|
final long beforeStopWatchMillis = System.currentTimeMillis();
|
||||||
final StopWatch watch = new StopWatch();
|
final StopWatch watch = new StopWatch();
|
||||||
assertThrows(IllegalStateException.class, watch::getStartTime,
|
assertThrows(IllegalStateException.class, watch::getStartTime, "Calling getStartTime on an unstarted StopWatch should throw an exception");
|
||||||
"Calling getStartTime on an unstarted StopWatch should throw an exception");
|
|
||||||
watch.start();
|
watch.start();
|
||||||
|
|
||||||
watch.getStartTime();
|
watch.getStartTime();
|
||||||
assertThat("getStartTime", watch.getStartTime(), greaterThanOrEqualTo(beforeStopWatchMillis));
|
assertThat("getStartTime", watch.getStartTime(), greaterThanOrEqualTo(beforeStopWatchMillis));
|
||||||
|
|
||||||
watch.reset();
|
watch.reset();
|
||||||
assertThrows(IllegalStateException.class, watch::getStartTime,
|
assertThrows(IllegalStateException.class, watch::getStartTime, "Calling getStartTime on a reset, but unstarted StopWatch should throw an exception");
|
||||||
"Calling getStartTime on a reset, but unstarted StopWatch should throw an exception");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue