Add StopWatch.getStopTime().

Also:
- Rename private instance variable to reflect scale.
- More precise Javadocs.
- Throw IllegalStateException instead of RuntimeException.
- Refactor common code in test for sleeping the current thread.
This commit is contained in:
Gary Gregory 2020-11-14 09:27:04 -05:00
parent 93d520a3e1
commit c9722131d5
3 changed files with 98 additions and 95 deletions

View File

@ -56,6 +56,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.booleanValues().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.primitiveValues().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StringUtils.containsAnyIgnoreCase(CharSequence, CharSequence...).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getStopTime().</action>
<action type="add" dev="ggregory" due-to="Edgar Asatryan">More test coverage for CharSequenceUtils. #631.</action>
<!-- UPDATES -->
<action type="update" dev="chtompki">Bump junit-jupiter from 5.6.2 to 5.7.0.</action>

View File

@ -205,21 +205,28 @@ public static StopWatch createStarted() {
private SplitState splitState = SplitState.UNSPLIT;
/**
* The start time.
* The start time in nanoseconds.
*/
private long startTime;
private long startTimeNanos;
/**
* The start time in Millis - nanoTime is only for elapsed time so we
* The start time in milliseconds - nanoTime is only for elapsed time so we
* need to also store the currentTimeMillis to maintain the old
* getStartTime API.
*/
private long startTimeMillis;
/**
* The stop time.
* The end time in milliseconds - nanoTime is only for elapsed time so we
* need to also store the currentTimeMillis to maintain the old
* getStartTime API.
*/
private long stopTime;
private long stopTimeMillis;
/**
* The stop time in nanoseconds.
*/
private long stopTimeNanos;
/**
* <p>
@ -273,7 +280,7 @@ public String getMessage() {
/**
* <p>
* Gets the time on the stopwatch in nanoseconds.
* Gets the <em>elapsed</em> time in nanoseconds.
* </p>
*
* <p>
@ -281,23 +288,24 @@ public String getMessage() {
* start and stop.
* </p>
*
* @return the time in nanoseconds
* @return the <em>elapsed</em> time in nanoseconds.
* @see System#nanoTime()
* @since 3.0
*/
public long getNanoTime() {
if (this.runningState == State.STOPPED || this.runningState == State.SUSPENDED) {
return this.stopTime - this.startTime;
return this.stopTimeNanos - this.startTimeNanos;
} else if (this.runningState == State.UNSTARTED) {
return 0;
} else if (this.runningState == State.RUNNING) {
return System.nanoTime() - this.startTime;
return System.nanoTime() - this.startTimeNanos;
}
throw new RuntimeException("Illegal running state has occurred.");
throw new IllegalStateException("Illegal running state has occurred.");
}
/**
* <p>
* Gets the split time on the stopwatch in nanoseconds.
* Gets the split time in nanoseconds.
* </p>
*
* <p>
@ -312,9 +320,9 @@ public long getNanoTime() {
*/
public long getSplitNanoTime() {
if (this.splitState != SplitState.SPLIT) {
throw new IllegalStateException("Stopwatch must be split to get the split time. ");
throw new IllegalStateException("Stopwatch must be split to get the split time.");
}
return this.stopTime - this.startTime;
return this.stopTimeNanos - this.startTimeNanos;
}
/**
@ -337,11 +345,12 @@ public long getSplitTime() {
}
/**
* Gets the time this stopwatch was started.
* 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
* @throws IllegalStateException
* if this StopWatch has not been started
* @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
* @since 2.4
*/
public long getStartTime() {
@ -352,6 +361,23 @@ public long getStartTime() {
return this.startTimeMillis;
}
/**
* Gets the time this stopwatch was stopped 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
* @since 3.12
*/
public long getStopTime() {
if (this.runningState == State.UNSTARTED) {
throw new IllegalStateException("Stopwatch has not been started");
}
// System.nanoTime is for elapsed time
return this.stopTimeMillis;
}
/**
* <p>
* Gets the time on the stopwatch.
@ -370,7 +396,7 @@ public long getTime() {
/**
* <p>
* Gets the time on the stopwatch in the specified TimeUnit.
* Gets the time in the specified TimeUnit.
* </p>
*
* <p>
@ -439,6 +465,7 @@ public void reset() {
this.runningState = State.UNSTARTED;
this.splitState = SplitState.UNSPLIT;
}
/**
* <p>
* Resumes the stopwatch after a suspend.
@ -456,7 +483,7 @@ public void resume() {
if (this.runningState != State.SUSPENDED) {
throw new IllegalStateException("Stopwatch must be suspended to resume. ");
}
this.startTime += System.nanoTime() - this.stopTime;
this.startTimeNanos += System.nanoTime() - this.stopTimeNanos;
this.runningState = State.RUNNING;
}
@ -477,7 +504,7 @@ public void split() {
if (this.runningState != State.RUNNING) {
throw new IllegalStateException("Stopwatch is not running. ");
}
this.stopTime = System.nanoTime();
this.stopTimeNanos = System.nanoTime();
this.splitState = SplitState.SPLIT;
}
@ -500,7 +527,7 @@ public void start() {
if (this.runningState != State.UNSTARTED) {
throw new IllegalStateException("Stopwatch already started. ");
}
this.startTime = System.nanoTime();
this.startTimeNanos = System.nanoTime();
this.startTimeMillis = System.currentTimeMillis();
this.runningState = State.RUNNING;
}
@ -522,7 +549,8 @@ public void stop() {
throw new IllegalStateException("Stopwatch is not running. ");
}
if (this.runningState == State.RUNNING) {
this.stopTime = System.nanoTime();
this.stopTimeNanos = System.nanoTime();
this.stopTimeMillis = System.currentTimeMillis();
}
this.runningState = State.STOPPED;
}
@ -544,7 +572,8 @@ public void suspend() {
if (this.runningState != State.RUNNING) {
throw new IllegalStateException("Stopwatch must be running to suspend. ");
}
this.stopTime = System.nanoTime();
this.stopTimeNanos = System.nanoTime();
this.stopTimeMillis = System.currentTimeMillis();
this.runningState = State.SUSPENDED;
}

View File

@ -61,14 +61,22 @@ private StopWatch createMockStopWatch(final long nanos) {
watch.suspend();
try {
final long currentNanos = System.nanoTime();
FieldUtils.writeField(watch, "startTime", currentNanos - nanos, true);
FieldUtils.writeField(watch, "stopTime", currentNanos, true);
FieldUtils.writeField(watch, "startTimeNanos", currentNanos - nanos, true);
FieldUtils.writeField(watch, "stopTimeNanos", currentNanos, true);
} catch (final IllegalAccessException e) {
return null;
}
return watch;
}
private void sleepQuietly(final int millis) {
try {
Thread.sleep(millis);
} catch (final InterruptedException ex) {
// ignore
}
}
// test bad states
@Test
public void testBadStates() {
@ -210,18 +218,10 @@ public void testGetStartTime() {
@Test
public void testLang315() {
final StopWatch watch = StopWatch.createStarted();
try {
Thread.sleep(200);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(200);
watch.suspend();
final long suspendTime = watch.getTime();
try {
Thread.sleep(200);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(200);
watch.stop();
final long totalTime = watch.getTime();
assertEquals(suspendTime, totalTime);
@ -256,11 +256,7 @@ public void testStopWatchGetWithTimeUnit() {
@Test
public void testStopWatchSimple() {
final StopWatch watch = StopWatch.createStarted();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.stop();
final long time = watch.getTime();
assertEquals(time, watch.getTime());
@ -272,6 +268,20 @@ public void testStopWatchSimple() {
assertEquals(0, watch.getTime());
}
@Test
public void testStopTimeSimple() {
final StopWatch watch = StopWatch.createStarted();
final long testStartMillis = System.currentTimeMillis();
sleepQuietly(550);
watch.stop();
final long testEndMillis = System.currentTimeMillis();
final long stopTime = watch.getStopTime();
assertEquals(stopTime, watch.getStopTime());
assertTrue(stopTime >= testStartMillis);
assertTrue(stopTime <= testEndMillis);
}
@Test
public void testStopWatchSimpleGet() {
final StopWatch watch = new StopWatch();
@ -279,36 +289,20 @@ public void testStopWatchSimpleGet() {
assertEquals(ZERO_TIME_ELAPSED, watch.toString());
watch.start();
try {
Thread.sleep(500);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(500);
assertTrue(watch.getTime() < 2000);
}
@Test
public void testStopWatchSplit() {
final StopWatch watch = StopWatch.createStarted();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.split();
final long splitTime = watch.getSplitTime();
final String splitStr = watch.toSplitString();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.unsplit();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.stop();
final long totalTime = watch.getTime();
@ -328,24 +322,19 @@ public void testStopWatchStatic() {
@Test
public void testStopWatchSuspend() {
final StopWatch watch = StopWatch.createStarted();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
final long testStartMillis = System.currentTimeMillis();
sleepQuietly(550);
watch.suspend();
final long testSuspendMillis = System.currentTimeMillis();
final long suspendTime = watch.getTime();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
final long stopTime = watch.getStopTime();
assertTrue(testStartMillis <= stopTime);
assertTrue(testSuspendMillis <= stopTime);
sleepQuietly(550);
watch.resume();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.stop();
final long totalTime = watch.getTime();
@ -358,11 +347,7 @@ public void testStopWatchSuspend() {
@Test
public void testToSplitString() {
final StopWatch watch = StopWatch.createStarted();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
@ -372,11 +357,7 @@ public void testToSplitString() {
public void testToSplitStringWithMessage() {
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(splitStr.length(), 12 + MESSAGE.length() + 1, "Formatted split string not the correct length");
@ -386,11 +367,7 @@ public void testToSplitStringWithMessage() {
public void testToString() {
//
final StopWatch watch = StopWatch.createStarted();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.split();
final String splitStr = watch.toString();
assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
@ -402,11 +379,7 @@ public void testToStringWithMessage() {
//
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
try {
Thread.sleep(550);
} catch (final InterruptedException ex) {
// ignore
}
sleepQuietly(550);
watch.split();
final String splitStr = watch.toString();
assertEquals(splitStr.length(), 12 + MESSAGE.length() + 1, "Formatted split string not the correct length");