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:
Henri Yandell 2009-03-01 21:18:18 +00:00
parent ef718fccdf
commit cb8a0d43ea
1 changed files with 52 additions and 7 deletions

View File

@ -56,6 +56,8 @@
*/
public class StopWatch {
private static final long NANO_2_MILLIS = 1000000L;
// running states
private static final int STATE_UNSTARTED = 0;
@ -85,6 +87,13 @@ public class StopWatch {
*/
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.
*/
@ -118,7 +127,8 @@ public void start() {
if (this.runningState != STATE_UNSTARTED) {
throw new IllegalStateException("Stopwatch already started. ");
}
this.startTime = System.currentTimeMillis();
this.startTime = System.nanoTime();
this.startTimeMillis = System.currentTimeMillis();
this.runningState = STATE_RUNNING;
}
@ -139,7 +149,7 @@ public void stop() {
throw new IllegalStateException("Stopwatch is not running. ");
}
if (this.runningState == STATE_RUNNING) {
this.stopTime = System.currentTimeMillis();
this.stopTime = System.nanoTime();
}
this.runningState = STATE_STOPPED;
}
@ -175,7 +185,7 @@ public void split() {
if (this.runningState != STATE_RUNNING) {
throw new IllegalStateException("Stopwatch is not running. ");
}
this.stopTime = System.currentTimeMillis();
this.stopTime = System.nanoTime();
this.splitState = STATE_SPLIT;
}
@ -216,7 +226,7 @@ public void suspend() {
if (this.runningState != STATE_RUNNING) {
throw new IllegalStateException("Stopwatch must be running to suspend. ");
}
this.stopTime = System.currentTimeMillis();
this.stopTime = System.nanoTime();
this.runningState = STATE_SUSPENDED;
}
@ -237,7 +247,7 @@ public void resume() {
if (this.runningState != STATE_SUSPENDED) {
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;
}
@ -254,12 +264,28 @@ public void resume() {
* @return the time in milliseconds
*/
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) {
return this.stopTime - this.startTime;
} else if (this.runningState == STATE_UNSTARTED) {
return 0;
} else if (this.runningState == STATE_RUNNING) {
return System.currentTimeMillis() - this.startTime;
return System.nanoTime() - this.startTime;
}
throw new RuntimeException("Illegal running state has occured. ");
}
@ -280,6 +306,24 @@ public long getTime() {
* @since 2.1
*/
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) {
throw new IllegalStateException("Stopwatch must be split to get the split time. ");
}
@ -298,7 +342,8 @@ public long getStartTime() {
if (this.runningState == STATE_UNSTARTED) {
throw new IllegalStateException("Stopwatch has not been started");
}
return this.startTime;
// System.nanoTime is for elapsed time
return this.startTimeMillis;
}
/**