Sort methods.

This commit is contained in:
Gary Gregory 2019-05-21 08:34:07 -04:00
parent bc82541c4e
commit 58c29cf239
1 changed files with 214 additions and 214 deletions

View File

@ -58,22 +58,15 @@
*/ */
public class StopWatch { public class StopWatch {
private static final long NANO_2_MILLIS = 1000000L;
/** /**
* Provides a started stopwatch for convenience. * Enumeration type which indicates the split status of stopwatch.
*
* @return StopWatch a stopwatch that's already been started.
*
* @since 3.5
*/ */
public static StopWatch createStarted() { private enum SplitState {
final StopWatch sw = new StopWatch(); SPLIT,
sw.start(); UNSPLIT
return sw;
} }
/** /**
* Enumeration type which indicates the status of stopwatch. * Enumeration type which indicates the status of stopwatch.
*/ */
@ -170,12 +163,19 @@ boolean isSuspended() {
abstract 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 { public static StopWatch createStarted() {
SPLIT, final StopWatch sw = new StopWatch();
UNSPLIT sw.start();
return sw;
} }
/** /**
* The current running state of the StopWatch. * The current running state of the StopWatch.
@ -215,146 +215,84 @@ public StopWatch() {
/** /**
* <p> * <p>
* Start the stopwatch. * Get the time on the stopwatch in nanoseconds.
* </p> * </p>
* *
* <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> * </p>
* *
* @throws IllegalStateException * @return the time in nanoseconds
* if the StopWatch is already running. * @since 3.0
*/ */
public void start() { public long getNanoTime() {
if (this.runningState == State.STOPPED) { if (this.runningState == State.STOPPED || this.runningState == State.SUSPENDED) {
throw new IllegalStateException("Stopwatch must be reset before being restarted. "); 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 RuntimeException("Illegal running state has occurred.");
throw new IllegalStateException("Stopwatch already started. ");
}
this.startTime = System.nanoTime();
this.startTimeMillis = System.currentTimeMillis();
this.runningState = State.RUNNING;
} }
/** /**
* <p> * <p>
* Stop the stopwatch. * Get the split time on the stopwatch in nanoseconds.
* </p> * </p>
* *
* <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> * </p>
* *
* @return the split time in nanoseconds
*
* @throws IllegalStateException * @throws IllegalStateException
* if the StopWatch is not running. * if the StopWatch has not yet been split.
* @since 3.0
*/ */
public void stop() { public long getSplitNanoTime() {
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;
}
/**
* <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 = SplitState.UNSPLIT;
}
/**
* <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>
*
* @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;
}
/**
* <p>
* Remove a split.
* </p>
*
* <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 != SplitState.SPLIT) { 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;
} }
/** /**
* <p> * <p>
* Suspend the stopwatch for later resumption. * Get the split time on the stopwatch.
* </p> * </p>
* *
* <p> * <p>
* This method suspends the watch until it is resumed. The watch will not include time between the suspend and * This is the time between start and latest split.
* resume calls in the total time.
* </p> * </p>
* *
* @return the split time in milliseconds
*
* @throws IllegalStateException * @throws IllegalStateException
* if the StopWatch is not currently running. * if the StopWatch has not yet been split.
* @since 2.1
*/ */
public void suspend() { public long getSplitTime() {
if (this.runningState != State.RUNNING) { return getSplitNanoTime() / NANO_2_MILLIS;
throw new IllegalStateException("Stopwatch must be running to suspend. ");
}
this.stopTime = System.nanoTime();
this.runningState = State.SUSPENDED;
} }
/** /**
* <p> * Returns the time this stopwatch was started.
* 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.
* </p>
* *
* @return the time this stopwatch was started
* @throws IllegalStateException * @throws IllegalStateException
* if the StopWatch has not been suspended. * if this StopWatch has not been started
* @since 2.4
*/ */
public void resume() { public long getStartTime() {
if (this.runningState != State.SUSPENDED) { if (this.runningState == State.UNSTARTED) {
throw new IllegalStateException("Stopwatch must be suspended to resume. "); throw new IllegalStateException("Stopwatch has not been started");
} }
this.startTime += System.nanoTime() - this.stopTime; // System.nanoTime is for elapsed time
this.runningState = State.RUNNING; return this.startTimeMillis;
} }
/** /**
@ -395,98 +333,166 @@ public long getTime(final TimeUnit timeUnit) {
/** /**
* <p> * <p>
* 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.
* </p> * </p>
* *
* <p> * @return boolean
* This is either the time between the start and the moment this method is called, or the amount of time between * If the StopWatch is started.
* start and stop. * @since 3.2
* </p>
*
* @return the time in nanoseconds
* @since 3.0
*/ */
public long getNanoTime() { public boolean isStarted() {
if (this.runningState == State.STOPPED || this.runningState == State.SUSPENDED) { return runningState.isStarted();
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.");
} }
/** /**
* <p> * <p>
* 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.
* </p> * </p>
* *
* <p> * @return boolean
* This is the time between start and latest split. * If the StopWatch is stopped.
* </p> * @since 3.2
*
* @return the split time in milliseconds
*
* @throws IllegalStateException
* if the StopWatch has not yet been split.
* @since 2.1
*/ */
public long getSplitTime() { public boolean isStopped() {
return getSplitNanoTime() / NANO_2_MILLIS; return runningState.isStopped();
}
/**
* <p>
* Get the split time on the stopwatch in nanoseconds.
* </p>
*
* <p>
* This is the time between start and latest split.
* </p>
*
* @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;
} }
/** /**
* <p> * <p>
* 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.
* </p>
*
* @return boolean
* If the StopWatch is suspended.
* @since 3.2
*/
public boolean isSuspended() {
return runningState.isSuspended();
}
/**
* <p>
* Resets the stopwatch. Stops it if need be.
* </p> * </p>
* *
* <p> * <p>
* The format used is ISO 8601-like, <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>. * This method clears the internal values to allow the object to be reused.
* </p>
*/
public void reset() {
this.runningState = State.UNSTARTED;
this.splitState = SplitState.UNSPLIT;
}
/**
* <p>
* Resume the stopwatch after a suspend.
* </p> * </p>
* *
* @return the time as a String * <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.
*/ */
@Override public void resume() {
public String toString() { if (this.runningState != State.SUSPENDED) {
return DurationFormatUtils.formatDurationHMS(getTime()); throw new IllegalStateException("Stopwatch must be suspended to resume. ");
}
this.startTime += System.nanoTime() - this.stopTime;
this.runningState = State.RUNNING;
}
/**
* <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>
*
* @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;
}
/**
* <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. ");
}
this.startTime = System.nanoTime();
this.startTimeMillis = 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. ");
}
if (this.runningState == State.RUNNING) {
this.stopTime = System.nanoTime();
}
this.runningState = State.STOPPED;
}
/**
* <p>
* Suspend the stopwatch for later resumption.
* </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.
* </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. ");
}
this.stopTime = System.nanoTime();
this.runningState = State.SUSPENDED;
} }
/** /**
@ -507,44 +513,38 @@ public String toSplitString() {
/** /**
* <p> * <p>
* The method is used to find out if the StopWatch is started. A suspended * Gets a summary of the time that the stopwatch recorded as a string.
* StopWatch is also started watch.
* </p> * </p>
* *
* @return boolean * <p>
* If the StopWatch is started. * The format used is ISO 8601-like, <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
* @since 3.2 * </p>
*
* @return the time as a String
*/ */
public boolean isStarted() { @Override
return runningState.isStarted(); public String toString() {
return DurationFormatUtils.formatDurationHMS(getTime());
} }
/** /**
* <p> * <p>
* This method is used to find out whether the StopWatch is suspended. * Remove a split.
* </p> * </p>
* *
* @return boolean
* If the StopWatch is suspended.
* @since 3.2
*/
public boolean isSuspended() {
return runningState.isSuspended();
}
/**
* <p> * <p>
* This method is used to find out whether the StopWatch is stopped. The * This method clears the stop time. The start time is unaffected, enabling timing from the original start point to
* stopwatch which's not yet started and explicitly stopped stopwatch is * continue.
* considered as stopped.
* </p> * </p>
* *
* @return boolean * @throws IllegalStateException
* If the StopWatch is stopped. * if the StopWatch has not been split.
* @since 3.2
*/ */
public boolean isStopped() { public void unsplit() {
return runningState.isStopped(); if (this.splitState != SplitState.SPLIT) {
throw new IllegalStateException("Stopwatch has not been split. ");
}
this.splitState = SplitState.UNSPLIT;
} }
} }