Applying my patch from LANG-478 - moving StopWatch to using nanoTime under the hood.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@749114 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ef718fccdf
commit
cb8a0d43ea
|
@ -56,6 +56,8 @@ package org.apache.commons.lang.time;
|
||||||
*/
|
*/
|
||||||
public class StopWatch {
|
public class StopWatch {
|
||||||
|
|
||||||
|
private static final long NANO_2_MILLIS = 1000000L;
|
||||||
|
|
||||||
// running states
|
// running states
|
||||||
private static final int STATE_UNSTARTED = 0;
|
private static final int STATE_UNSTARTED = 0;
|
||||||
|
|
||||||
|
@ -85,6 +87,13 @@ public class StopWatch {
|
||||||
*/
|
*/
|
||||||
private long startTime;
|
private long startTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The start time in Millis - 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 stop time.
|
||||||
*/
|
*/
|
||||||
|
@ -118,7 +127,8 @@ public class StopWatch {
|
||||||
if (this.runningState != STATE_UNSTARTED) {
|
if (this.runningState != STATE_UNSTARTED) {
|
||||||
throw new IllegalStateException("Stopwatch already started. ");
|
throw new IllegalStateException("Stopwatch already started. ");
|
||||||
}
|
}
|
||||||
this.startTime = System.currentTimeMillis();
|
this.startTime = System.nanoTime();
|
||||||
|
this.startTimeMillis = System.currentTimeMillis();
|
||||||
this.runningState = STATE_RUNNING;
|
this.runningState = STATE_RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +149,7 @@ public class StopWatch {
|
||||||
throw new IllegalStateException("Stopwatch is not running. ");
|
throw new IllegalStateException("Stopwatch is not running. ");
|
||||||
}
|
}
|
||||||
if (this.runningState == STATE_RUNNING) {
|
if (this.runningState == STATE_RUNNING) {
|
||||||
this.stopTime = System.currentTimeMillis();
|
this.stopTime = System.nanoTime();
|
||||||
}
|
}
|
||||||
this.runningState = STATE_STOPPED;
|
this.runningState = STATE_STOPPED;
|
||||||
}
|
}
|
||||||
|
@ -175,7 +185,7 @@ public class StopWatch {
|
||||||
if (this.runningState != STATE_RUNNING) {
|
if (this.runningState != STATE_RUNNING) {
|
||||||
throw new IllegalStateException("Stopwatch is not running. ");
|
throw new IllegalStateException("Stopwatch is not running. ");
|
||||||
}
|
}
|
||||||
this.stopTime = System.currentTimeMillis();
|
this.stopTime = System.nanoTime();
|
||||||
this.splitState = STATE_SPLIT;
|
this.splitState = STATE_SPLIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +226,7 @@ public class StopWatch {
|
||||||
if (this.runningState != STATE_RUNNING) {
|
if (this.runningState != STATE_RUNNING) {
|
||||||
throw new IllegalStateException("Stopwatch must be running to suspend. ");
|
throw new IllegalStateException("Stopwatch must be running to suspend. ");
|
||||||
}
|
}
|
||||||
this.stopTime = System.currentTimeMillis();
|
this.stopTime = System.nanoTime();
|
||||||
this.runningState = STATE_SUSPENDED;
|
this.runningState = STATE_SUSPENDED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,7 +247,7 @@ public class StopWatch {
|
||||||
if (this.runningState != STATE_SUSPENDED) {
|
if (this.runningState != STATE_SUSPENDED) {
|
||||||
throw new IllegalStateException("Stopwatch must be suspended to resume. ");
|
throw new IllegalStateException("Stopwatch must be suspended to resume. ");
|
||||||
}
|
}
|
||||||
this.startTime += (System.currentTimeMillis() - this.stopTime);
|
this.startTime += (System.nanoTime() - this.stopTime);
|
||||||
this.runningState = STATE_RUNNING;
|
this.runningState = STATE_RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,12 +264,28 @@ public class StopWatch {
|
||||||
* @return the time in milliseconds
|
* @return the time in milliseconds
|
||||||
*/
|
*/
|
||||||
public long getTime() {
|
public long getTime() {
|
||||||
|
return getNanoTime() / NANO_2_MILLIS;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Get the time on the stopwatch in nanoseconds.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This is either the time between the start and the moment this method is called, or the amount of time between
|
||||||
|
* start and stop.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return the time in nanoseconds
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public long getNanoTime() {
|
||||||
if (this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) {
|
if (this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) {
|
||||||
return this.stopTime - this.startTime;
|
return this.stopTime - this.startTime;
|
||||||
} else if (this.runningState == STATE_UNSTARTED) {
|
} else if (this.runningState == STATE_UNSTARTED) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (this.runningState == STATE_RUNNING) {
|
} else if (this.runningState == STATE_RUNNING) {
|
||||||
return System.currentTimeMillis() - this.startTime;
|
return System.nanoTime() - this.startTime;
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Illegal running state has occured. ");
|
throw new RuntimeException("Illegal running state has occured. ");
|
||||||
}
|
}
|
||||||
|
@ -280,6 +306,24 @@ public class StopWatch {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public long getSplitTime() {
|
public long getSplitTime() {
|
||||||
|
return getSplitNanoTime() / NANO_2_MILLIS;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Get the split time on the stopwatch in nanoseconds.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This is the time between start and latest split.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @return the split time in nanoseconds
|
||||||
|
*
|
||||||
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch has not yet been split.
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public long getSplitNanoTime() {
|
||||||
if (this.splitState != STATE_SPLIT) {
|
if (this.splitState != STATE_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. ");
|
||||||
}
|
}
|
||||||
|
@ -298,7 +342,8 @@ public class StopWatch {
|
||||||
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");
|
||||||
}
|
}
|
||||||
return this.startTime;
|
// System.nanoTime is for elapsed time
|
||||||
|
return this.startTimeMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue