Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions

 Copyright (c) Unterrainer Informatik OG.
 This source is subject to the Microsoft Public License.

 See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 All other rights reserved.

 (In other words you may copy, use, change and redistribute it without
 any restrictions except for not suing me because it broke something.)

 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
 PURPOSE.

 
/
 
 
 package info.unterrainer.java.tools.reporting;
 
 
 import lombok.Builder;
 import lombok.Getter;
 import lombok.Setter;
This class implements a stopWatch.

Additionally to the normal stopWatch-functionality it may be used to debug out split-times as well. It measures the split-times and keeps track of the overall times in a variable.
Don't be afraid to stop the watch. Stopping doesn't mean you loose any value whatsoever. Think of it as a real-life stopWatch where you may press the start-button at any time after previously pressing the stop-button.

This class provides useful overloads that allow writing to a PrintStream in a way that your measurement doesn't get compromised (the stopWatch is paused while writing to the stream). You may initialize it with a PrintStream so that you can use all the overloads that take a string-argument or System.out is used as a default.
All the write-operations are performed as a printLine-call, so you don't need to close your assigned text with a newline-character.

This class is automatically created using millisecond-precision. If you want to enable nanoseconds-precision albeit performance impacts, though the impact of this is very small indeed, you may do so after creating the stopWatch via the setIsNanoPrecision-Setter.

All public methods within this class are synchronized so you may use it concurrently within many threads.
It has a property 'isActive' that defaults to true. When this is set to false all calls to this class are aborted within a single if-statement in the called method. This is a convenience function so that you may leave your logging-code in the production code.

 
 @Accessors(chain = true)
 public class SplitStopWatch {

This format string displays the total time, split-time and your text separated by a pipe.
 
 	public static final String FORMAT_STRING_TOTAL_SPLIT_OWN_MS = "| total: %10.0fms | since last split: %10.0fms | %s|[%s]";
 	public static final String FORMAT_STRING_TOTAL_SPLIT_OWN_NS = "| total: %15fms | since last split: %15fms | %s|[%s]";
 
 	private static final String COMMAND_START = "START";
 	private static final String COMMAND_START_NEW = "STARTNEW";
 	private static final String COMMAND_STOP = "STOP";
 	private static final String COMMAND_SPLIT = "SPLIT";
 	private static final String COMMAND_RESET = "RESET";
 	private static final String COMMAND_SPLIT_AND_STOP = "SPLITANDSTOP";
 
 	private long startTime;
 	private long time;
 	private long totalTime;
 	private boolean isRunning;
 
 	private final PrintStream printStream;
 
 	private Logger logger;
 	private Level logLevel;

When this is set to false all calls to this class are aborted within a single if-statement in the called method. This is a convenience function so that you may leave your logging-code in the production code.

Parameters:
isActive the new active
Returns:
true, or false.
 
 	private boolean active = true;

A flag indicating the precision of the means of measurement used. This may have a small performance impact.

Parameters:
isNanoPrecision the new nanosecond precision
Returns:
true if nanosecond-precision is used, false if millisecond-precision is used.
 
 	private boolean nanoPrecision = false;

The prefix is written every time an output is made (if this default behavior isn't turned off using setDisplayPrefix()).

Parameters:
prefixFormatString the new prefix format string
Returns:
The format string for a Formatter.
A flag indicating whether a given prefix (the default one or set one) is printed in front of the actual time or not.

Parameters:
displayPrefix the new display prefix
Returns:
true if the prefix is written, false otherwise.
	private boolean displayPrefix = true;

A flag indicating if the stream is flushed immediately after each write.

Parameters:
isFlushImmediately a boolean parameter indicating if flush is automatically called after each write-operation
Returns:
true if the stream is flushed immediately after each write, false otherwise.
	private boolean flushImmediately;

The string which is used when outputting one indent. The default value are three spaces per indent, so this string consists of three spaces per default.

Parameters:
indentString the new indent string
Returns:
The indentString currently used.
	private String indentString = "  ";
	private final float NANO_CONVERSION_QUOTIENT = 1000000f;

Initializes a new instance of the SplitStopwatch class. The default PrintStream is System.out.
	public SplitStopWatch() {
		 = null;
		 = null;
	}

Instantiates a new split stop watch.

Parameters:
logger java.util.logging.Logger the logger to use for output (default log-level is INFO)
	public SplitStopWatch(final Logger logger) {
		this. = logger;
		 = null;
	}

Instantiates a new split stop watch.

Parameters:
logger java.util.logging.Logger the logger to use for output (default log-level is INFO)
level java.util.logging.Level the log-level
	public SplitStopWatch(final Logger loggerfinal Level level) {
		this. = logger;
		 = level;
		 = null;
	}

Initializes a new instance of the SplitStopwatch class.

Parameters:
printStream The printStream to write to.
	public SplitStopWatch(final PrintStream printStream) {
		 = null;
		 = null;
		this. = printStream;
	}

Initializes a new instance of the SplitStopwatch class.

Parameters:
printStream The printStream to write to.
flushImmediately If set to true the writer is immediately flushed every time a write is done.
	public SplitStopWatch(final PrintStream printStreamfinal boolean flushImmediately) {
		 = null;
		 = null;
		this. = printStream;
		this. = flushImmediately;
	}
	public SplitStopWatch(PrintStream printStreamboolean flushImmediatelyLogger loggerLevel logLevelboolean activeboolean displayPrefix,
			String indentStringboolean nanoPrecisionString prefixFormatString) {
		this. = logger;
		this. = logLevel;
		this. = printStream;
		this. = flushImmediately;
		this. = active;
		this. = displayPrefix;
		this. = indentString;
		this. = nanoPrecision;
		this. = prefixFormatString;
	}
	public SplitStopWatch setLogger(Logger logger) {
		this. = logger;
		if ( == null) {
		}
		return this;
	}

Starts, or resumes measuring elapsed time for an interval. You may start after a stop as well.
	public synchronized SplitStopWatch start() {
		if (!) {
			return this;
		}
		start("");
		return this;
	}

Starts, or resumes measuring elapsed time for an interval. You may start after a stop as well.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
	public synchronized SplitStopWatch start(final String text) {
		if (!) {
			return this;
		}
		start(text, 0);
		return this;
	}

Starts, or resumes measuring elapsed time for an interval. You may start after a stop as well.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
indentLevel The indent level.
	public synchronized SplitStopWatch start(final String textfinal int indentLevel) {
		if (!) {
			return this;
		}
		writeText(textindentLevel);
		return this;
	}

Initializes a new instance, sets the elapsed time property to zero, and starts measuring elapsed time.
	public synchronized SplitStopWatch startNew() {
		if (!) {
			return this;
		}
		return this;
	}

Initializes a new instance, sets the elapsed time property to zero, and starts measuring elapsed time.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
	public synchronized SplitStopWatch startNew(final String text) {
		if (!) {
			return this;
		}
		startNew(text, 0);
		return this;
	}

Initializes a new instance, sets the elapsed time property to zero, and starts measuring elapsed time.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
indentLevel The indent level.
	public synchronized SplitStopWatch startNew(final String textfinal int indentLevel) {
		if (!) {
			return this;
		}
		long timeSinceLastSplit = timerElapsedTime();
		writeText(texttimeSinceLastSplitindentLevel);
		return this;
	}

Stops measuring elapsed time for an interval. You may stop and restart afterward resulting in a pause of the timer.
	public synchronized SplitStopWatch stop() {
		if (!) {
			return this;
		}
		stop("");
		return this;
	}

Stops measuring elapsed time for an interval. You may stop and restart afterward resulting in a pause of the timer.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
	public synchronized SplitStopWatch stop(final String text) {
		if (!) {
			return this;
		}
		stop(text, 0);
		return this;
	}

Stops measuring elapsed time for an interval. You may stop and restart afterward resulting in a pause of the timer.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
indentLevel The indent level.
	public synchronized SplitStopWatch stop(final String textfinal int indentLevel) {
		if (!) {
			return this;
		}
		long timeSinceLastSplit = timerElapsedTime();
		writeText(texttimeSinceLastSplitindentLevel);
		return this;
	}

Stops time interval measurement and resets the time to zero.
	public synchronized SplitStopWatch reset() {
		if (!) {
			return this;
		}
		reset("");
		return this;
	}

Stops time interval measurement and resets the time to zero.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
	public synchronized SplitStopWatch reset(final String text) {
		if (!) {
			return this;
		}
		reset(text, 0);
		return this;
	}

Stops time interval measurement and resets the time to zero.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
indentLevel The indent level.
	public synchronized SplitStopWatch reset(final String textfinal int indentLevel) {
		if (!) {
			return this;
		}
		long timeSinceLastSplit = splitAndStop();
		writeText(texttimeSinceLastSplitindentLevel);
		return this;
	}

Takes a split-time and restarts the timer.

Returns:
The split time in milliseconds.
	public synchronized long split() {
		if (!) {
			return 0;
		}
		return split("");
	}

Takes a split-time and restarts the timer.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
Returns:
The split time in milliseconds.
	public synchronized long split(final String text) {
		if (!) {
			return 0;
		}
		return split(text, 0);
	}

Takes a split-time and restarts the timer.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
indentLevel The indent level.
Returns:
The split time in milliseconds.
	public synchronized long split(final String textfinal int indentLevel) {
		if (!) {
			return 0;
		}
		// Split and stop.
		long timeSinceLastSplit = timerElapsedTime();
		 += timeSinceLastSplit;
		writeText(texttimeSinceLastSplitindentLevel);
		return timeSinceLastSplit;
	}

Takes a split-time and does not restart the timer. You may restart it with start again at any time.

Returns:
The split time in milliseconds.
	public synchronized long splitAndStop() {
		if (!) {
			return 0;
		}
		return splitAndStop("");
	}

Takes a split-time and does not restart the timer. You may restart it with start again at any time.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
Returns:
The split time in milliseconds.
	public synchronized long splitAndStop(final String text) {
		if (!) {
			return 0;
		}
		return splitAndStop(text, 0);
	}

Takes a split-time and does not restart the timer. You may restart it with start again at any time.

Parameters:
text The text to output on the PrintStream you specified when creating this instance or System.out as the default.
indentLevel The indent level.
Returns:
The split time in milliseconds.
	public synchronized long splitAndStop(final String textfinal int indentLevel) {
		if (!) {
			return 0;
		}
		long timeSinceLastSplit = timerElapsedTime();
		 += timeSinceLastSplit;
		writeText(texttimeSinceLastSplitindentLevel);
		return timeSinceLastSplit;
	}
	private void doReset() {
		 = 0;
	}

Gets a flag indicating if the internal watch is running at the moment.

Returns:
true if the watch is running at the moment, false otherwise.
	public boolean isRunning() {
		return  == 0;
	}

Gets the time that has passed since the start of the watch or the last split-time, if one has been taken, in milliseconds.

Returns:
The time in milliseconds.
	}

Gets the time that has passed since the start of the watch.

Returns:
The time in milliseconds.
	public synchronized long getTotalTime() {
	}

Gets the actual Time.

Parameters:
isNanoPrecision True, if nanosecond-precision is used, false otherwise.
Returns:
The actual time.
	private long getActualTime(final boolean isNanoPrecision) {
		if (isNanoPrecision) {
			return System.nanoTime();
		}
		return System.currentTimeMillis();
	}

Resets the timer and all according values.
		 = 0;
		 = 0;
		 = false;
		return this;
	}

Starts the timer.
		if (!) {
			 = true;
		}
		return this;
	}

Stops the timer and doesn't reset the according values.
		if () {
			 = 0;
			 = false;
		}
		return this;
	}

Gets the elapsed time since the last start of the timer.

Returns:
The time since the last start of the timer.
	private long timerElapsedTime() {
		if () {
			return  + now - ;
		}
		return ;
	}

Writes the given text to the underlying PrintStream.

Parameters:
text The text to be written.
command The name of the command that triggered the output.
elapsedTimeSinceLastSplit The time elapsed since the last split or start.
indentLevel The level to indent to.
	private void writeText(final String textfinal String commandfinal long elapsedTimeSinceLastSplitfinal int indentLevel) {
		String indent = "";
		if (indentLevel > 0) {
			for (int i = 0; i < indentLeveli++) {
				indent += ;
			}
		}
			Formatter formatter = new Formatter();
			float total = getTotalTime();
			float elapsed = elapsedTimeSinceLastSplit;
			}
			}
			formatter.format(fstotalelapsedtextcommand);
			String output = formatter.out().toString();
			println(indent + output);
			formatter.close();
else {
			println(indent + text);
		}
		}
	}
	private void println(final String text) {
		if ( != null) {
			return;
		}
		if ( != null) {
			return;
		}
	}
	private void flush() {
		if ( != null) {
		}
	}
New to GrepCode? Check out our FAQ X