diff --git a/src/java/org/apache/commons/lang/time/StopWatch.java b/src/java/org/apache/commons/lang/time/StopWatch.java index 0f0786a36..a91c67cef 100644 --- a/src/java/org/apache/commons/lang/time/StopWatch.java +++ b/src/java/org/apache/commons/lang/time/StopWatch.java @@ -14,35 +14,42 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.lang.time; /** - *
StopWatch
provides a convenient API for timings.
+ * StopWatch
provides a convenient API for timings.
+ *
To start the watch, call {@link #start()}. At this point you can:
+ *+ * To start the watch, call {@link #start()}. At this point you can: + *
*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.
- * - *NOTE: As from v2.1, the methods protect against inappropriate calls. - * Thus you cannot now call stop before start, resume before suspend or - * unsplit before split.
- * - *1. split(), suspend(), or stop() cannot be invoked twice
+ *
+ *
+ * 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. + *
+ * + *+ * NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start, + * resume before suspend or unsplit before split. + *
+ * + *
+ * 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 without calling reset()
Constructor.
+ *+ * Constructor. + *
*/ public StopWatch() { super(); } /** - *Start the stopwatch.
+ *+ * Start the stopwatch. + *
* - *This method starts a new timing session, clearing any previous values.
- * - * @throws IllegalStateException if the StopWatch is already running. + *+ * This method starts a new timing session, clearing any previous values. + *
+ * + * @throws IllegalStateException + * if the StopWatch is already running. */ public void start() { - if(this.runningState == STATE_STOPPED) { + if (this.runningState == STATE_STOPPED) { 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. "); } stopTime = -1; @@ -105,45 +124,58 @@ public class StopWatch { } /** - *Stop the stopwatch.
+ *+ * Stop the stopwatch. + *
* - *This method ends a new timing session, allowing the time to be retrieved.
- * - * @throws IllegalStateException if the StopWatch is not running. + *+ * This method ends a new timing session, allowing the time to be retrieved. + *
+ * + * @throws IllegalStateException + * if the StopWatch is not running. */ 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. "); } - if(this.runningState == STATE_RUNNING) { + if (this.runningState == STATE_RUNNING) { stopTime = System.currentTimeMillis(); } this.runningState = STATE_STOPPED; } /** - *Resets the stopwatch. Stops it if need be.
+ *+ * Resets the stopwatch. Stops it if need be. + *
* - *This method clears the internal values to allow the object to be reused.
+ *+ * This method clears the internal values to allow the object to be reused. + *
*/ public void reset() { this.runningState = STATE_UNSTARTED; - this.splitState = STATE_UNSPLIT; + this.splitState = STATE_UNSPLIT; startTime = -1; stopTime = -1; } /** - *Split the time.
+ *+ * Split the time. + *
* - *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.
- * - * @throws IllegalStateException if the StopWatch is not running. + *+ * 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. + *
+ * + * @throws IllegalStateException + * if the StopWatch is not running. */ public void split() { - if(this.runningState != STATE_RUNNING) { + if (this.runningState != STATE_RUNNING) { throw new IllegalStateException("Stopwatch is not running. "); } stopTime = System.currentTimeMillis(); @@ -151,15 +183,20 @@ public class StopWatch { } /** - *Remove a split.
+ *+ * Remove a split. + *
* - *This method clears the stop time. The start time is unaffected, enabling - * timing from the original start point to continue.
- * - * @throws IllegalStateException if the StopWatch has not been split. + *+ * This method clears the stop time. The start time is unaffected, enabling timing from the original start point to + * continue. + *
+ * + * @throws IllegalStateException + * if the StopWatch has not been split. */ public void unsplit() { - if(this.splitState != STATE_SPLIT) { + if (this.splitState != STATE_SPLIT) { throw new IllegalStateException("Stopwatch has not been split. "); } stopTime = -1; @@ -167,15 +204,20 @@ public class StopWatch { } /** - *Suspend the stopwatch for later resumption.
+ *+ * Suspend the stopwatch for later resumption. + *
* - *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.
- * - * @throws IllegalStateException if the StopWatch is not currently running. + *+ * 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. + *
+ * + * @throws IllegalStateException + * if the StopWatch is not currently running. */ public void suspend() { - if(this.runningState != STATE_RUNNING) { + if (this.runningState != STATE_RUNNING) { throw new IllegalStateException("Stopwatch must be running to suspend. "); } stopTime = System.currentTimeMillis(); @@ -183,15 +225,20 @@ public class StopWatch { } /** - *Resume the stopwatch after a suspend.
+ *+ * Resume the stopwatch after a suspend. + *
* - *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.
- * - * @throws IllegalStateException if the StopWatch has not been suspended. + *+ * 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. + *
+ * + * @throws IllegalStateException + * if the StopWatch has not been suspended. */ public void resume() { - if(this.runningState != STATE_SUSPENDED) { + if (this.runningState != STATE_SUSPENDED) { throw new IllegalStateException("Stopwatch must be suspended to resume. "); } startTime += (System.currentTimeMillis() - stopTime); @@ -200,48 +247,73 @@ public class StopWatch { } /** - *Get the time on the stopwatch.
+ *+ * Get the time on the stopwatch. + *
* - *This is either the time between the start and the moment this method - * is called, or the amount of time between start and stop.
+ *+ * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. + *
* * @return the time in milliseconds */ 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; - } else - if(this.runningState == STATE_UNSTARTED) { + } else if (this.runningState == STATE_UNSTARTED) { return 0; - } else - if(this.runningState == STATE_RUNNING) { + } else if (this.runningState == STATE_RUNNING) { return System.currentTimeMillis() - this.startTime; } throw new RuntimeException("Illegal running state has occured. "); } /** - *Get the split time on the stopwatch.
+ *+ * Get the split time on the stopwatch. + *
* - *This is the time between start and latest split.
+ *+ * This is the time between start and latest split. + *
* * @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 */ 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. "); } return this.stopTime - this.startTime; } /** - *Gets a summary of the time that the stopwatch recorded as a string.
+ * Returns the time this stopwatch was started. * - *The format used is ISO8601-like, - * hours:minutes:seconds.milliseconds.
+ * @return the time this stopwatch was started + * @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; + } + + /** + *+ * Gets a summary of the time that the stopwatch recorded as a string. + *
+ * + *+ * The format used is ISO8601-like, hours:minutes:seconds.milliseconds. + *
* * @return the time as a String */ @@ -250,10 +322,13 @@ public class StopWatch { } /** - *Gets a summary of the split time that the stopwatch recorded as a string.
+ *+ * Gets a summary of the split time that the stopwatch recorded as a string. + *
* - *The format used is ISO8601-like, - * hours:minutes:seconds.milliseconds.
+ *+ * The format used is ISO8601-like, hours:minutes:seconds.milliseconds. + *
* * @return the split time as a String * @since 2.1 diff --git a/src/test/org/apache/commons/lang/time/StopWatchTest.java b/src/test/org/apache/commons/lang/time/StopWatchTest.java index bb7b41244..cb693cc22 100644 --- a/src/test/org/apache/commons/lang/time/StopWatchTest.java +++ b/src/test/org/apache/commons/lang/time/StopWatchTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.lang.time; +import junit.framework.Assert; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -203,7 +204,31 @@ public class StopWatchTest extends TestCase { } catch(IllegalStateException ise) { // 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 + } } }