package folioxml.utils; /** * Time the execution of any block of code. */ public final class Stopwatch { /** * Start the stopwatch. * * @throws IllegalStateException if the stopwatch is already running. */ public Stopwatch start() { if (fIsRunning) { throw new IllegalStateException("Must stop before calling start again."); } //reset both start and stop fStart = System.currentTimeMillis(); fStop = 0; fIsRunning = true; fHasBeenUsedOnce = true; return this; } /** * Stop the stopwatch. * * @throws IllegalStateException if the stopwatch is not already running. */ public Stopwatch stop() { if (!fIsRunning) { throw new IllegalStateException("Cannot stop if not currently running."); } fStop = System.currentTimeMillis(); elapsed += (fStop - fStart); fIsRunning = false; return this; } /** * Sets the elapsed time to zero. * * @return */ public Stopwatch reset() { elapsed = 0; return this; } /** * Express the "reading" on the stopwatch. * * @throws IllegalStateException if the Stopwatch has never been used, * or if the stopwatch is still running. */ @Override public String toString() { validateIsReadable(); StringBuilder result = new StringBuilder(); result.append(elapsed); result.append(" ms"); return result.toString(); } /** * Express the "reading" on the stopwatch as a numeric type. * milliseconds * * @throws IllegalStateException if the Stopwatch has never been used, * or if the stopwatch is still running. */ public long toValue() { validateIsReadable(); return fStop - fStart; } public boolean hasValue() { return (fHasBeenUsedOnce && !fIsRunning); } // PRIVATE //// private long fStart; private long fStop; private long elapsed = 0; private boolean fIsRunning; private boolean fHasBeenUsedOnce; /** * Throws IllegalStateException if the watch has never been started, * or if the watch is still running. */ private void validateIsReadable() { if (fIsRunning) { String message = "Cannot read a stopwatch which is still running."; throw new IllegalStateException(message); } if (!fHasBeenUsedOnce) { String message = "Cannot read a stopwatch which has never been started."; throw new IllegalStateException(message); } } }