You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2004/07/25 09:31:36 UTC
cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/time StopWatch.java
bayard 2004/07/25 00:31:36
Modified: lang/src/java/org/apache/commons/lang/time StopWatch.java
Log:
An attempt to put together an implementation for bugzilla issue 29163. Tempted to think that split() should not be using the stopTime variable.
Revision Changes Path
1.9 +107 -9 jakarta-commons/lang/src/java/org/apache/commons/lang/time/StopWatch.java
Index: StopWatch.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/time/StopWatch.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- StopWatch.java 18 Feb 2004 22:56:42 -0000 1.8
+++ StopWatch.java 25 Jul 2004 07:31:36 -0000 1.9
@@ -36,6 +36,12 @@
* <p>It is intended that the output methods {@link #toString()} and {@link #getTime()}
* should only be called after stop, split or suspend, however a suitable result will
* be returned at other points.</p>
+
+1. split(), suspend(), or stop() cannot be invoked twice.
+2. unsplit() may only be called if the watch has been split()
+3. resume() may only be called if the watch has been suspend()
+4. start() cannot be called twice.
+
*
* @author Henri Yandell
* @author Stephen Colebourne
@@ -43,7 +49,27 @@
* @version $Id$
*/
public class StopWatch {
-
+
+ // running states
+ 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_SUSPENDED = 3;
+
+ // split state
+ private static final int STATE_UNSPLIT = 10;
+ private static final int STATE_SPLIT = 11;
+
+ /**
+ * The current running state of the StopWatch.
+ */
+ private int runningState = STATE_UNSTARTED;
+
+ /**
+ * Whether the stopwatch has a split time recorded.
+ */
+ private int splitState = STATE_UNSPLIT;
+
/**
* The start time.
*/
@@ -63,27 +89,44 @@
* <p>Start the stopwatch.</p>
*
* <p>This method starts a new timing session, clearing any previous values.</p>
+ *
+ * @throws IllegalStateException if the StopWatch is already running.
*/
public void start() {
+ if(this.runningState == STATE_STOPPED) {
+ throw new IllegalStateException("Stopwatch must be reset before being restarted. ");
+ }
+ if(this.runningState != STATE_UNSTARTED) {
+ throw new IllegalStateException("Stopwatch already started. ");
+ }
stopTime = -1;
startTime = System.currentTimeMillis();
+ this.runningState = STATE_RUNNING;
}
/**
* <p>Stop the stopwatch.</p>
*
* <p>This method ends a new timing session, allowing the time to be retrieved.</p>
+ *
+ * @throws IllegalStateException if the StopWatch is not running.
*/
public void stop() {
+ if(this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) {
+ throw new IllegalStateException("Stopwatch is not running. ");
+ }
stopTime = System.currentTimeMillis();
+ this.runningState = STATE_STOPPED;
}
/**
- * <p>Reset the stopwatch.</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>
*/
public void reset() {
+ this.runningState = STATE_UNSTARTED;
+ this.splitState = STATE_UNSPLIT;
startTime = -1;
stopTime = -1;
}
@@ -94,9 +137,15 @@
* <p>This method sets the stop time of the watch to allow a time to be extracted.
* The start time is unaffected, enabling {@link #unsplit()} to continue the
* timing from the original start point.</p>
+ *
+ * @throws IllegalStateException if the StopWatch is not running.
*/
public void split() {
+ if(this.runningState != STATE_RUNNING) {
+ throw new IllegalStateException("Stopwatch is not running. ");
+ }
stopTime = System.currentTimeMillis();
+ this.splitState = STATE_SPLIT;
}
/**
@@ -104,9 +153,15 @@
*
* <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.
*/
public void unsplit() {
+ if(this.splitState != STATE_SPLIT) {
+ throw new IllegalStateException("Stopwatch has not been split. ");
+ }
stopTime = -1;
+ this.splitState = STATE_UNSPLIT;
}
/**
@@ -114,9 +169,15 @@
*
* <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.
*/
public void suspend() {
+ if(this.runningState != STATE_RUNNING) {
+ throw new IllegalStateException("Stopwatch must be running to suspend. ");
+ }
stopTime = System.currentTimeMillis();
+ this.runningState = STATE_SUSPENDED;
}
/**
@@ -124,27 +185,52 @@
*
* <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.
*/
public void resume() {
+ if(this.runningState != STATE_SUSPENDED) {
+ throw new IllegalStateException("Stopwatch must be suspended to resume. ");
+ }
startTime += (System.currentTimeMillis() - stopTime);
stopTime = -1;
+ this.runningState = STATE_RUNNING;
}
/**
* <p>Get the time on the stopwatch.</p>
*
- * <p>This is either the time between start and latest split, between start
- * and stop, or the time between the start and the moment this method is called.</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 milliseconds
*/
public long getTime() {
- if (stopTime == -1) {
- if (startTime == -1) {
- return 0;
- }
+ 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);
}
+ throw new RuntimeException("Illegal running state has occured. ");
+ }
+
+ /**
+ * <p>Get the split time on the stopwatch.</p>
+ *
+ * <p>This is the time between start and latest split. </p>
+ *
+ * @return the split time in milliseconds
+ *
+ * @throws IllegalStateException if the StopWatch has not yet been split.
+ */
+ public long getSplitTime() {
+ if(this.splitState != STATE_SPLIT) {
+ throw new IllegalStateException("Stopwatch must be split to get the split time. ");
+ }
return (this.stopTime - this.startTime);
}
@@ -158,6 +244,18 @@
*/
public String toString() {
return DurationFormatUtils.formatISO(getTime());
+ }
+
+ /**
+ * <p>Gets a summary of the split 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 split time as a String
+ */
+ public String toSplitString() {
+ return DurationFormatUtils.formatISO(getSplitTime());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org