This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 58c29cf Sort methods.
58c29cf is described below
commit 58c29cf239ab8d9160b750aba5a32dfae9c9e511
Author: Gary Gregory <[email protected]>
AuthorDate: Tue May 21 08:34:07 2019 -0400
Sort methods.
---
.../org/apache/commons/lang3/time/StopWatch.java | 364 ++++++++++-----------
1 file changed, 182 insertions(+), 182 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 feb2388..6137928 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 @@ import java.util.concurrent.TimeUnit;
*/
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 @@ public class StopWatch {
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,278 +215,284 @@ public class StopWatch {
/**
* <p>
- * Start the stopwatch.
+ * Get the time on the stopwatch in nanoseconds.
* </p>
*
* <p>
- * 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.
* </p>
*
- * @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. ");
- }
- if (this.runningState != State.UNSTARTED) {
- throw new IllegalStateException("Stopwatch already started. ");
+ 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;
}
- this.startTime = System.nanoTime();
- this.startTimeMillis = System.currentTimeMillis();
- this.runningState = State.RUNNING;
+ throw new RuntimeException("Illegal running state has occurred.");
}
/**
* <p>
- * Stop the stopwatch.
+ * Get the split time on the stopwatch in nanoseconds.
* </p>
*
* <p>
- * This method ends a new timing session, allowing the time to be
retrieved.
+ * This is the time between start and latest split.
* </p>
*
+ * @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();
+ public long getSplitNanoTime() {
+ if (this.splitState != SplitState.SPLIT) {
+ throw new IllegalStateException("Stopwatch must be split to get
the split time. ");
}
- this.runningState = State.STOPPED;
+ return this.stopTime - this.startTime;
}
/**
* <p>
- * Resets the stopwatch. Stops it if need be.
+ * Get the split time on the stopwatch.
* </p>
*
* <p>
- * This method clears the internal values to allow the object to be reused.
+ * 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.
+ * @since 2.1
*/
- public void reset() {
- this.runningState = State.UNSTARTED;
- this.splitState = SplitState.UNSPLIT;
+ public long getSplitTime() {
+ return getSplitNanoTime() / NANO_2_MILLIS;
}
/**
- * <p>
- * Split the time.
- * </p>
- *
- * <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>
+ * Returns the time this stopwatch was started.
*
+ * @return the time this stopwatch was started
* @throws IllegalStateException
- * if the StopWatch is not running.
+ * if this StopWatch has not been started
+ * @since 2.4
*/
- public void split() {
- if (this.runningState != State.RUNNING) {
- throw new IllegalStateException("Stopwatch is not running. ");
+ public long getStartTime() {
+ if (this.runningState == State.UNSTARTED) {
+ throw new IllegalStateException("Stopwatch has not been started");
}
- this.stopTime = System.nanoTime();
- this.splitState = SplitState.SPLIT;
+ // System.nanoTime is for elapsed time
+ return this.startTimeMillis;
}
/**
* <p>
- * Remove a split.
+ * Get the time on the stopwatch.
* </p>
*
* <p>
- * This method clears the stop time. The start time is unaffected,
enabling timing from the original start point to
- * continue.
+ * 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>
*
- * @throws IllegalStateException
- * if the StopWatch has not been split.
+ * @return the time in milliseconds
*/
- public void unsplit() {
- if (this.splitState != SplitState.SPLIT) {
- throw new IllegalStateException("Stopwatch has not been split. ");
- }
- this.splitState = SplitState.UNSPLIT;
+ public long getTime() {
+ return getNanoTime() / NANO_2_MILLIS;
}
/**
* <p>
- * Suspend the stopwatch for later resumption.
+ * Get the time on the stopwatch in the specified TimeUnit.
* </p>
*
* <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.
+ * This is either the time between the start and the moment this method is
called, or the amount of time between
+ * start and stop. The resulting time will be expressed in the desired
TimeUnit with any remainder rounded down.
+ * For example, if the specified unit is {@code TimeUnit.HOURS} and the
stopwatch time is 59 minutes, then the
+ * result returned will be {@code 0}.
* </p>
*
- * @throws IllegalStateException
- * if the StopWatch is not currently running.
+ * @param timeUnit the unit of time, not null
+ * @return the time in the specified TimeUnit, rounded down
+ * @since 3.5
*/
- 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 getTime(final TimeUnit timeUnit) {
+ return timeUnit.convert(getNanoTime(), TimeUnit.NANOSECONDS);
}
/**
* <p>
- * Resume the stopwatch after a suspend.
- * </p>
- *
- * <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.
+ * The method is used to find out if the StopWatch is started. A suspended
+ * StopWatch is also started watch.
* </p>
*
- * @throws IllegalStateException
- * if the StopWatch has not been suspended.
+ * @return boolean
+ * If the StopWatch is started.
+ * @since 3.2
*/
- 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;
+ public boolean isStarted() {
+ return runningState.isStarted();
}
/**
* <p>
- * Get the 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.
* </p>
*
+ * @return boolean
+ * If the StopWatch is stopped.
+ * @since 3.2
+ */
+ public boolean isStopped() {
+ return runningState.isStopped();
+ }
+
+ /**
* <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.
+ * This method is used to find out whether the StopWatch is suspended.
* </p>
*
- * @return the time in milliseconds
+ * @return boolean
+ * If the StopWatch is suspended.
+ * @since 3.2
*/
- public long getTime() {
- return getNanoTime() / NANO_2_MILLIS;
+ public boolean isSuspended() {
+ return runningState.isSuspended();
}
/**
* <p>
- * Get the time on the stopwatch in the specified TimeUnit.
+ * Resets the stopwatch. Stops it if need be.
* </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. The resulting time will be expressed in the desired
TimeUnit with any remainder rounded down.
- * For example, if the specified unit is {@code TimeUnit.HOURS} and the
stopwatch time is 59 minutes, then the
- * result returned will be {@code 0}.
+ * This method clears the internal values to allow the object to be reused.
* </p>
- *
- * @param timeUnit the unit of time, not null
- * @return the time in the specified TimeUnit, rounded down
- * @since 3.5
*/
- public long getTime(final TimeUnit timeUnit) {
- return timeUnit.convert(getNanoTime(), TimeUnit.NANOSECONDS);
+ public void reset() {
+ this.runningState = State.UNSTARTED;
+ this.splitState = SplitState.UNSPLIT;
}
/**
* <p>
- * Get the time on the stopwatch in nanoseconds.
+ * Resume the stopwatch after a suspend.
* </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.
+ * 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>
*
- * @return the time in nanoseconds
- * @since 3.0
+ * @throws IllegalStateException
+ * if the StopWatch has not been suspended.
*/
- 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;
+ public void resume() {
+ if (this.runningState != State.SUSPENDED) {
+ throw new IllegalStateException("Stopwatch must be suspended to
resume. ");
}
- throw new RuntimeException("Illegal running state has occurred.");
+ this.startTime += System.nanoTime() - this.stopTime;
+ this.runningState = State.RUNNING;
}
-
/**
* <p>
- * Get the split time on the stopwatch.
+ * Split the time.
* </p>
*
* <p>
- * This is the time between start and latest split.
+ * 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>
*
- * @return the split time in milliseconds
- *
* @throws IllegalStateException
- * if the StopWatch has not yet been split.
- * @since 2.1
+ * if the StopWatch is not running.
*/
- public long getSplitTime() {
- return getSplitNanoTime() / NANO_2_MILLIS;
+ public void split() {
+ if (this.runningState != State.RUNNING) {
+ throw new IllegalStateException("Stopwatch is not running. ");
+ }
+ this.stopTime = System.nanoTime();
+ this.splitState = SplitState.SPLIT;
}
+
/**
* <p>
- * Get the split time on the stopwatch in nanoseconds.
+ * Start the stopwatch.
* </p>
*
* <p>
- * This is the time between start and latest split.
+ * This method starts a new timing session, clearing any previous values.
* </p>
*
- * @return the split time in nanoseconds
- *
* @throws IllegalStateException
- * if the StopWatch has not yet been split.
- * @since 3.0
+ * if the StopWatch is already running.
*/
- public long getSplitNanoTime() {
- if (this.splitState != SplitState.SPLIT) {
- throw new IllegalStateException("Stopwatch must be split to get
the split time. ");
+ public void start() {
+ if (this.runningState == State.STOPPED) {
+ throw new IllegalStateException("Stopwatch must be reset before
being restarted. ");
}
- return this.stopTime - 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;
}
/**
- * Returns the time this stopwatch was started.
+ * <p>
+ * Stop the stopwatch.
+ * </p>
+ *
+ * <p>
+ * This method ends a new timing session, allowing the time to be
retrieved.
+ * </p>
*
- * @return the time this stopwatch was started
* @throws IllegalStateException
- * if this StopWatch has not been started
- * @since 2.4
+ * if the StopWatch is not running.
*/
- public long getStartTime() {
- if (this.runningState == State.UNSTARTED) {
- throw new IllegalStateException("Stopwatch has not been started");
+ public void stop() {
+ if (this.runningState != State.RUNNING && this.runningState !=
State.SUSPENDED) {
+ throw new IllegalStateException("Stopwatch is not running. ");
}
- // System.nanoTime is for elapsed time
- return this.startTimeMillis;
+ if (this.runningState == State.RUNNING) {
+ this.stopTime = System.nanoTime();
+ }
+ this.runningState = State.STOPPED;
}
/**
* <p>
- * Gets a summary of the time that the stopwatch recorded as a string.
+ * Suspend the stopwatch for later resumption.
* </p>
*
* <p>
- * The format used is ISO 8601-like,
<i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
+ * 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>
*
- * @return the time as a String
+ * @throws IllegalStateException
+ * if the StopWatch is not currently running.
*/
- @Override
- public String toString() {
- return DurationFormatUtils.formatDurationHMS(getTime());
+ 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 class StopWatch {
/**
* <p>
- * 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.
* </p>
*
- * @return boolean
- * If the StopWatch is started.
- * @since 3.2
- */
- public boolean isStarted() {
- return runningState.isStarted();
- }
-
- /**
* <p>
- * This method is used to find out whether the StopWatch is suspended.
+ * The format used is ISO 8601-like,
<i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
* </p>
*
- * @return boolean
- * If the StopWatch is suspended.
- * @since 3.2
+ * @return the time as a String
*/
- public boolean isSuspended() {
- return runningState.isSuspended();
+ @Override
+ public String toString() {
+ return DurationFormatUtils.formatDurationHMS(getTime());
}
/**
* <p>
- * 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.
+ * Remove a split.
* </p>
*
- * @return boolean
- * If the StopWatch is stopped.
- * @since 3.2
+ * <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 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;
}
}