[LANG-356] Add getStartTime to StopWatch.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@583605 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
60fe05eb7a
commit
15296e0664
|
@ -14,34 +14,41 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.apache.commons.lang.time;
|
package org.apache.commons.lang.time;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p><code>StopWatch</code> provides a convenient API for timings.</p>
|
* <p>
|
||||||
|
* <code>StopWatch</code> provides a convenient API for timings.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>To start the watch, call {@link #start()}. At this point you can:</p>
|
* <p>
|
||||||
|
* To start the watch, call {@link #start()}. At this point you can:
|
||||||
|
* </p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>{@link #split()} the watch to get the time whilst the watch continues in the
|
* <li>{@link #split()} the watch to get the time whilst the watch continues in the background. {@link #unsplit()} will
|
||||||
* background. {@link #unsplit()} will remove the effect of the split. At this point,
|
* remove the effect of the split. At this point, these three options are available again.</li>
|
||||||
* these three options are available again.</li>
|
* <li>{@link #suspend()} the watch to pause it. {@link #resume()} allows the watch to continue. Any time between the
|
||||||
* <li>{@link #suspend()} the watch to pause it. {@link #resume()} allows the watch
|
* suspend and resume will not be counted in the total. At this point, these three options are available again.</li>
|
||||||
* to continue. Any time between the suspend and resume will not be counted in
|
* <li>{@link #stop()} the watch to complete the timing session.</li>
|
||||||
* the total. At this point, these three options are available again.</li>
|
|
||||||
* <li>{@link #stop()} the watch to complete the timing session.</li>
|
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <p>It is intended that the output methods {@link #toString()} and {@link #getTime()}
|
* <p>
|
||||||
* should only be called after stop, split or suspend, however a suitable result will
|
* It is intended that the output methods {@link #toString()} and {@link #getTime()} should only be called after stop,
|
||||||
* be returned at other points.</p>
|
* split or suspend, however a suitable result will be returned at other points.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>NOTE: As from v2.1, the methods protect against inappropriate calls.
|
* <p>
|
||||||
* Thus you cannot now call stop before start, resume before suspend or
|
* NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start,
|
||||||
* unsplit before split.</p>
|
* resume before suspend or unsplit before split.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>1. split(), suspend(), or stop() cannot be invoked twice<br />
|
* <p>
|
||||||
|
* 1. split(), suspend(), or stop() cannot be invoked twice<br />
|
||||||
* 2. unsplit() may only be called if the watch has been split()<br />
|
* 2. unsplit() may only be called if the watch has been split()<br />
|
||||||
* 3. resume() may only be called if the watch has been suspend()<br />
|
* 3. resume() may only be called if the watch has been suspend()<br />
|
||||||
* 4. start() cannot be called twice without calling reset()</p>
|
* 4. start() cannot be called twice without calling reset()
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
|
@ -51,52 +58,64 @@ public class StopWatch {
|
||||||
|
|
||||||
// running states
|
// running states
|
||||||
private static final int STATE_UNSTARTED = 0;
|
private static final int STATE_UNSTARTED = 0;
|
||||||
private static final int STATE_RUNNING = 1;
|
|
||||||
private static final int STATE_STOPPED = 2;
|
private static final int STATE_RUNNING = 1;
|
||||||
|
|
||||||
|
private static final int STATE_STOPPED = 2;
|
||||||
|
|
||||||
private static final int STATE_SUSPENDED = 3;
|
private static final int STATE_SUSPENDED = 3;
|
||||||
|
|
||||||
// split state
|
// split state
|
||||||
private static final int STATE_UNSPLIT = 10;
|
private static final int STATE_UNSPLIT = 10;
|
||||||
private static final int STATE_SPLIT = 11;
|
|
||||||
|
private static final int STATE_SPLIT = 11;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current running state of the StopWatch.
|
* The current running state of the StopWatch.
|
||||||
*/
|
*/
|
||||||
private int runningState = STATE_UNSTARTED;
|
private int runningState = STATE_UNSTARTED;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the stopwatch has a split time recorded.
|
* Whether the stopwatch has a split time recorded.
|
||||||
*/
|
*/
|
||||||
private int splitState = STATE_UNSPLIT;
|
private int splitState = STATE_UNSPLIT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The start time.
|
* The start time.
|
||||||
*/
|
*/
|
||||||
private long startTime = -1;
|
private long startTime = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The stop time.
|
* The stop time.
|
||||||
*/
|
*/
|
||||||
private long stopTime = -1;
|
private long stopTime = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Constructor.</p>
|
* <p>
|
||||||
|
* Constructor.
|
||||||
|
* </p>
|
||||||
*/
|
*/
|
||||||
public StopWatch() {
|
public StopWatch() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Start the stopwatch.</p>
|
* <p>
|
||||||
|
* Start the stopwatch.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This method starts a new timing session, clearing any previous values.</p>
|
* <p>
|
||||||
|
* This method starts a new timing session, clearing any previous values.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if the StopWatch is already running.
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch is already running.
|
||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
if(this.runningState == STATE_STOPPED) {
|
if (this.runningState == STATE_STOPPED) {
|
||||||
throw new IllegalStateException("Stopwatch must be reset before being restarted. ");
|
throw new IllegalStateException("Stopwatch must be reset before being restarted. ");
|
||||||
}
|
}
|
||||||
if(this.runningState != STATE_UNSTARTED) {
|
if (this.runningState != STATE_UNSTARTED) {
|
||||||
throw new IllegalStateException("Stopwatch already started. ");
|
throw new IllegalStateException("Stopwatch already started. ");
|
||||||
}
|
}
|
||||||
stopTime = -1;
|
stopTime = -1;
|
||||||
|
@ -105,45 +124,58 @@ public class StopWatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Stop the stopwatch.</p>
|
* <p>
|
||||||
|
* Stop the stopwatch.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This method ends a new timing session, allowing the time to be retrieved.</p>
|
* <p>
|
||||||
|
* This method ends a new timing session, allowing the time to be retrieved.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if the StopWatch is not running.
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch is not running.
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if(this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) {
|
if (this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) {
|
||||||
throw new IllegalStateException("Stopwatch is not running. ");
|
throw new IllegalStateException("Stopwatch is not running. ");
|
||||||
}
|
}
|
||||||
if(this.runningState == STATE_RUNNING) {
|
if (this.runningState == STATE_RUNNING) {
|
||||||
stopTime = System.currentTimeMillis();
|
stopTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
this.runningState = STATE_STOPPED;
|
this.runningState = STATE_STOPPED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Resets the stopwatch. Stops it if need be. </p>
|
* <p>
|
||||||
|
* Resets the stopwatch. Stops it if need be.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This method clears the internal values to allow the object to be reused.</p>
|
* <p>
|
||||||
|
* This method clears the internal values to allow the object to be reused.
|
||||||
|
* </p>
|
||||||
*/
|
*/
|
||||||
public void reset() {
|
public void reset() {
|
||||||
this.runningState = STATE_UNSTARTED;
|
this.runningState = STATE_UNSTARTED;
|
||||||
this.splitState = STATE_UNSPLIT;
|
this.splitState = STATE_UNSPLIT;
|
||||||
startTime = -1;
|
startTime = -1;
|
||||||
stopTime = -1;
|
stopTime = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Split the time.</p>
|
* <p>
|
||||||
|
* Split the time.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This method sets the stop time of the watch to allow a time to be extracted.
|
* <p>
|
||||||
* The start time is unaffected, enabling {@link #unsplit()} to continue the
|
* This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected,
|
||||||
* timing from the original start point.</p>
|
* enabling {@link #unsplit()} to continue the timing from the original start point.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if the StopWatch is not running.
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch is not running.
|
||||||
*/
|
*/
|
||||||
public void split() {
|
public void split() {
|
||||||
if(this.runningState != STATE_RUNNING) {
|
if (this.runningState != STATE_RUNNING) {
|
||||||
throw new IllegalStateException("Stopwatch is not running. ");
|
throw new IllegalStateException("Stopwatch is not running. ");
|
||||||
}
|
}
|
||||||
stopTime = System.currentTimeMillis();
|
stopTime = System.currentTimeMillis();
|
||||||
|
@ -151,15 +183,20 @@ public class StopWatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Remove a split.</p>
|
* <p>
|
||||||
|
* Remove a split.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This method clears the stop time. The start time is unaffected, enabling
|
* <p>
|
||||||
* timing from the original start point to continue.</p>
|
* This method clears the stop time. The start time is unaffected, enabling timing from the original start point to
|
||||||
|
* continue.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if the StopWatch has not been split.
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch has not been split.
|
||||||
*/
|
*/
|
||||||
public void unsplit() {
|
public void unsplit() {
|
||||||
if(this.splitState != STATE_SPLIT) {
|
if (this.splitState != STATE_SPLIT) {
|
||||||
throw new IllegalStateException("Stopwatch has not been split. ");
|
throw new IllegalStateException("Stopwatch has not been split. ");
|
||||||
}
|
}
|
||||||
stopTime = -1;
|
stopTime = -1;
|
||||||
|
@ -167,15 +204,20 @@ public class StopWatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Suspend the stopwatch for later resumption.</p>
|
* <p>
|
||||||
|
* Suspend the stopwatch for later resumption.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This method suspends the watch until it is resumed. The watch will not include
|
* <p>
|
||||||
* time between the suspend and resume calls in the total time.</p>
|
* This method suspends the watch until it is resumed. The watch will not include time between the suspend and
|
||||||
|
* resume calls in the total time.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if the StopWatch is not currently running.
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch is not currently running.
|
||||||
*/
|
*/
|
||||||
public void suspend() {
|
public void suspend() {
|
||||||
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. ");
|
||||||
}
|
}
|
||||||
stopTime = System.currentTimeMillis();
|
stopTime = System.currentTimeMillis();
|
||||||
|
@ -183,15 +225,20 @@ public class StopWatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Resume the stopwatch after a suspend.</p>
|
* <p>
|
||||||
|
* Resume the stopwatch after a suspend.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This method resumes the watch after it was suspended. The watch will not include
|
* <p>
|
||||||
* time between the suspend and resume calls in the total time.</p>
|
* This method resumes the watch after it was suspended. The watch will not include time between the suspend and
|
||||||
|
* resume calls in the total time.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if the StopWatch has not been suspended.
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch has not been suspended.
|
||||||
*/
|
*/
|
||||||
public void resume() {
|
public void resume() {
|
||||||
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. ");
|
||||||
}
|
}
|
||||||
startTime += (System.currentTimeMillis() - stopTime);
|
startTime += (System.currentTimeMillis() - stopTime);
|
||||||
|
@ -200,48 +247,73 @@ public class StopWatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Get the time on the stopwatch.</p>
|
* <p>
|
||||||
|
* Get the time on the stopwatch.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This is either the time between the start and the moment this method
|
* <p>
|
||||||
* is called, or the amount of time between start and stop.</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 milliseconds
|
* @return the time in milliseconds
|
||||||
*/
|
*/
|
||||||
public long getTime() {
|
public long getTime() {
|
||||||
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
|
} else if (this.runningState == STATE_UNSTARTED) {
|
||||||
if(this.runningState == STATE_UNSTARTED) {
|
|
||||||
return 0;
|
return 0;
|
||||||
} else
|
} else if (this.runningState == STATE_RUNNING) {
|
||||||
if(this.runningState == STATE_RUNNING) {
|
|
||||||
return System.currentTimeMillis() - this.startTime;
|
return System.currentTimeMillis() - this.startTime;
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Illegal running state has occured. ");
|
throw new RuntimeException("Illegal running state has occured. ");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Get the split time on the stopwatch.</p>
|
* <p>
|
||||||
|
* Get the split time on the stopwatch.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>This is the time between start and latest split. </p>
|
* <p>
|
||||||
|
* This is the time between start and latest split.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @return the split time in milliseconds
|
* @return the split time in milliseconds
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if the StopWatch has not yet been split.
|
* @throws IllegalStateException
|
||||||
|
* if the StopWatch has not yet been split.
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public long getSplitTime() {
|
public long getSplitTime() {
|
||||||
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. ");
|
||||||
}
|
}
|
||||||
return this.stopTime - this.startTime;
|
return this.stopTime - this.startTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets a summary of the time that the stopwatch recorded as a string.</p>
|
* Returns the time this stopwatch was started.
|
||||||
*
|
*
|
||||||
* <p>The format used is ISO8601-like,
|
* @return the time this stopwatch was started
|
||||||
* <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.</p>
|
* @throws IllegalStateException
|
||||||
|
* if this StopWatch has not been started
|
||||||
|
* @since 2.4
|
||||||
|
*/
|
||||||
|
public long getStartTime() {
|
||||||
|
if (this.runningState == STATE_UNSTARTED) {
|
||||||
|
throw new IllegalStateException("Stopwatch has not been started");
|
||||||
|
}
|
||||||
|
return this.startTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Gets a summary of the time that the stopwatch recorded as a string.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* The format used is ISO8601-like, <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @return the time as a String
|
* @return the time as a String
|
||||||
*/
|
*/
|
||||||
|
@ -250,10 +322,13 @@ public class StopWatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Gets a summary of the split time that the stopwatch recorded as a string.</p>
|
* <p>
|
||||||
|
* Gets a summary of the split time that the stopwatch recorded as a string.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>The format used is ISO8601-like,
|
* <p>
|
||||||
* <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.</p>
|
* The format used is ISO8601-like, <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @return the split time as a String
|
* @return the split time as a String
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.lang.time;
|
package org.apache.commons.lang.time;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
@ -203,7 +204,31 @@ public class StopWatchTest extends TestCase {
|
||||||
} catch(IllegalStateException ise) {
|
} catch(IllegalStateException ise) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetStartTime() {
|
||||||
|
long beforeStopWatch = System.currentTimeMillis();
|
||||||
|
StopWatch watch = new StopWatch();
|
||||||
|
try {
|
||||||
|
watch.getStartTime();
|
||||||
|
fail("Calling getStartTime on an unstarted StopWatch should throw an exception");
|
||||||
|
} catch (IllegalStateException expected) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
watch.start();
|
||||||
|
try {
|
||||||
|
watch.getStartTime();
|
||||||
|
Assert.assertTrue(watch.getStartTime() >= beforeStopWatch);
|
||||||
|
} catch (IllegalStateException ex) {
|
||||||
|
fail("Start time should be available: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
watch.reset();
|
||||||
|
try {
|
||||||
|
watch.getStartTime();
|
||||||
|
fail("Calling getStartTime on a reset, but unstarted StopWatch should throw an exception");
|
||||||
|
} catch (IllegalStateException expected) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue