From 58c29cf239ab8d9160b750aba5a32dfae9c9e511 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 21 May 2019 08:34:07 -0400 Subject: [PATCH] Sort methods. --- .../apache/commons/lang3/time/StopWatch.java | 428 +++++++++--------- 1 file changed, 214 insertions(+), 214 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java b/src/main/java/org/apache/commons/lang3/time/StopWatch.java index feb2388df..6137928a9 100644 --- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java +++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java @@ -58,22 +58,15 @@ */ public class StopWatch { - private static final long NANO_2_MILLIS = 1000000L; - - /** - * Provides a started stopwatch for convenience. - * - * @return StopWatch a stopwatch that's already been started. - * - * @since 3.5 + * Enumeration type which indicates the split status of stopwatch. */ - public static StopWatch createStarted() { - final StopWatch sw = new StopWatch(); - sw.start(); - return sw; + private enum SplitState { + SPLIT, + UNSPLIT } + /** * Enumeration type which indicates the status of stopwatch. */ @@ -170,12 +163,19 @@ boolean isSuspended() { abstract boolean isSuspended(); } + private static final long NANO_2_MILLIS = 1000000L; + /** - * Enumeration type which indicates the split status of stopwatch. + * Provides a started stopwatch for convenience. + * + * @return StopWatch a stopwatch that's already been started. + * + * @since 3.5 */ - private enum SplitState { - SPLIT, - UNSPLIT + public static StopWatch createStarted() { + final StopWatch sw = new StopWatch(); + sw.start(); + return sw; } /** * The current running state of the StopWatch. @@ -215,146 +215,84 @@ public StopWatch() { /** *

- * Start the stopwatch. + * Get the time on the stopwatch in nanoseconds. *

* *

- * This method starts a new timing session, clearing any previous values. + * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. *

* - * @throws IllegalStateException - * if the StopWatch is already running. + * @return the time in nanoseconds + * @since 3.0 */ - public void start() { - if (this.runningState == State.STOPPED) { - throw new IllegalStateException("Stopwatch must be reset before being restarted. "); + 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.nanoTime() - this.startTime; } - if (this.runningState != State.UNSTARTED) { - throw new IllegalStateException("Stopwatch already started. "); - } - this.startTime = System.nanoTime(); - this.startTimeMillis = System.currentTimeMillis(); - this.runningState = State.RUNNING; + throw new RuntimeException("Illegal running state has occurred."); } /** *

- * Stop the stopwatch. + * Get the split time on the stopwatch in nanoseconds. *

* *

- * This method ends a new timing session, allowing the time to be retrieved. + * This is the time between start and latest split. *

* + * @return the split time in nanoseconds + * * @throws IllegalStateException - * if the StopWatch is not running. + * if the StopWatch has not yet been split. + * @since 3.0 */ - public void stop() { - if (this.runningState != State.RUNNING && this.runningState != State.SUSPENDED) { - throw new IllegalStateException("Stopwatch is not running. "); - } - if (this.runningState == State.RUNNING) { - this.stopTime = System.nanoTime(); - } - this.runningState = State.STOPPED; - } - - /** - *

- * Resets the stopwatch. Stops it if need be. - *

- * - *

- * This method clears the internal values to allow the object to be reused. - *

- */ - public void reset() { - this.runningState = State.UNSTARTED; - this.splitState = SplitState.UNSPLIT; - } - - /** - *

- * 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. - */ - public void split() { - if (this.runningState != State.RUNNING) { - throw new IllegalStateException("Stopwatch is not running. "); - } - this.stopTime = System.nanoTime(); - this.splitState = SplitState.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. - */ - public void unsplit() { + public long getSplitNanoTime() { if (this.splitState != SplitState.SPLIT) { - throw new IllegalStateException("Stopwatch has not been split. "); + throw new IllegalStateException("Stopwatch must be split to get the split time. "); } - this.splitState = SplitState.UNSPLIT; + return this.stopTime - this.startTime; } /** *

- * Suspend the stopwatch for later resumption. + * Get the split time on the stopwatch. *

* *

- * 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. + * This is the time between start and latest split. *

* + * @return the split time in milliseconds + * * @throws IllegalStateException - * if the StopWatch is not currently running. + * if the StopWatch has not yet been split. + * @since 2.1 */ - public void suspend() { - if (this.runningState != State.RUNNING) { - throw new IllegalStateException("Stopwatch must be running to suspend. "); - } - this.stopTime = System.nanoTime(); - this.runningState = State.SUSPENDED; + public long getSplitTime() { + return getSplitNanoTime() / NANO_2_MILLIS; } /** - *

- * 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. - *

+ * Returns the time this stopwatch was started. * + * @return the time this stopwatch was started * @throws IllegalStateException - * if the StopWatch has not been suspended. + * if this StopWatch has not been started + * @since 2.4 */ - public void resume() { - if (this.runningState != State.SUSPENDED) { - throw new IllegalStateException("Stopwatch must be suspended to resume. "); + public long getStartTime() { + if (this.runningState == State.UNSTARTED) { + throw new IllegalStateException("Stopwatch has not been started"); } - this.startTime += System.nanoTime() - this.stopTime; - this.runningState = State.RUNNING; + // System.nanoTime is for elapsed time + return this.startTimeMillis; } /** @@ -395,98 +333,166 @@ public long getTime(final TimeUnit timeUnit) { /** *

- * Get the time on the stopwatch in nanoseconds. + * The method is used to find out if the StopWatch is started. A suspended + * StopWatch is also started watch. *

* - *

- * 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 nanoseconds - * @since 3.0 + * @return boolean + * If the StopWatch is started. + * @since 3.2 */ - 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.nanoTime() - this.startTime; - } - throw new RuntimeException("Illegal running state has occurred."); + public boolean isStarted() { + return runningState.isStarted(); } /** *

- * Get the split time on the stopwatch. + * This method is used to find out whether the StopWatch is stopped. The + * stopwatch which's not yet started and explicitly stopped stopwatch is + * considered as stopped. *

* - *

- * 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. - * @since 2.1 + * @return boolean + * If the StopWatch is stopped. + * @since 3.2 */ - public long getSplitTime() { - return getSplitNanoTime() / NANO_2_MILLIS; - } - /** - *

- * Get the split time on the stopwatch in nanoseconds. - *

- * - *

- * This is the time between start and latest split. - *

- * - * @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 != SplitState.SPLIT) { - throw new IllegalStateException("Stopwatch must be split to get the split time. "); - } - return this.stopTime - this.startTime; - } - - /** - * Returns the time this stopwatch was started. - * - * @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"); - } - // System.nanoTime is for elapsed time - return this.startTimeMillis; + public boolean isStopped() { + return runningState.isStopped(); } /** *

- * Gets a summary of the time that the stopwatch recorded as a string. + * This method is used to find out whether the StopWatch is suspended. + *

+ * + * @return boolean + * If the StopWatch is suspended. + * @since 3.2 + */ + public boolean isSuspended() { + return runningState.isSuspended(); + } + + /** + *

+ * Resets the stopwatch. Stops it if need be. *

* *

- * The format used is ISO 8601-like, hours:minutes:seconds.milliseconds. + * This method clears the internal values to allow the object to be reused. + *

+ */ + public void reset() { + this.runningState = State.UNSTARTED; + this.splitState = SplitState.UNSPLIT; + } + + /** + *

+ * Resume the stopwatch after a suspend. *

* - * @return the time as a String + *

+ * 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. */ - @Override - public String toString() { - return DurationFormatUtils.formatDurationHMS(getTime()); + public void resume() { + if (this.runningState != State.SUSPENDED) { + throw new IllegalStateException("Stopwatch must be suspended to resume. "); + } + this.startTime += System.nanoTime() - this.stopTime; + this.runningState = State.RUNNING; + } + /** + *

+ * 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. + */ + public void split() { + if (this.runningState != State.RUNNING) { + throw new IllegalStateException("Stopwatch is not running. "); + } + this.stopTime = System.nanoTime(); + this.splitState = SplitState.SPLIT; + } + + /** + *

+ * Start the stopwatch. + *

+ * + *

+ * 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) { + throw new IllegalStateException("Stopwatch must be reset before being restarted. "); + } + if (this.runningState != State.UNSTARTED) { + throw new IllegalStateException("Stopwatch already started. "); + } + this.startTime = System.nanoTime(); + this.startTimeMillis = System.currentTimeMillis(); + this.runningState = State.RUNNING; + } + + /** + *

+ * Stop the stopwatch. + *

+ * + *

+ * 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) { + throw new IllegalStateException("Stopwatch is not running. "); + } + if (this.runningState == State.RUNNING) { + this.stopTime = System.nanoTime(); + } + this.runningState = State.STOPPED; + } + + /** + *

+ * 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. + */ + public void suspend() { + if (this.runningState != State.RUNNING) { + throw new IllegalStateException("Stopwatch must be running to suspend. "); + } + this.stopTime = System.nanoTime(); + this.runningState = State.SUSPENDED; } /** @@ -507,44 +513,38 @@ public String toSplitString() { /** *

- * The method is used to find out if the StopWatch is started. A suspended - * StopWatch is also started watch. + * Gets a summary of the time that the stopwatch recorded as a string. *

* - * @return boolean - * If the StopWatch is started. - * @since 3.2 + *

+ * The format used is ISO 8601-like, hours:minutes:seconds.milliseconds. + *

+ * + * @return the time as a String */ - public boolean isStarted() { - return runningState.isStarted(); + @Override + public String toString() { + return DurationFormatUtils.formatDurationHMS(getTime()); } /** *

- * This method is used to find out whether the StopWatch is suspended. + * Remove a split. *

* - * @return boolean - * If the StopWatch is suspended. - * @since 3.2 - */ - public boolean isSuspended() { - return runningState.isSuspended(); - } - - /** *

- * This method is used to find out whether the StopWatch is stopped. The - * stopwatch which's not yet started and explicitly stopped stopwatch is - * considered as stopped. + * This method clears the stop time. The start time is unaffected, enabling timing from the original start point to + * continue. *

* - * @return boolean - * If the StopWatch is stopped. - * @since 3.2 + * @throws IllegalStateException + * if the StopWatch has not been split. */ - public boolean isStopped() { - return runningState.isStopped(); + public void unsplit() { + if (this.splitState != SplitState.SPLIT) { + throw new IllegalStateException("Stopwatch has not been split. "); + } + this.splitState = SplitState.UNSPLIT; } }