Back to project page RockFall-Android.
The source code is released under:
GNU Lesser General Public License
If you think the Android project RockFall-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package eu.raffprta.rockfall.app; // w w w .j ava 2 s . c om /** * @author Neil Speirs * A stopwatch accumulates time when it is running. You can * repeatedly start and stop the stopwatch. You can use a * stopwatch to measure the running time of a program. * * This class was distributed for use (without restrictions) as * part of one of my coursework assignments. */ public class StopWatch { /** * Constructs a stopwatch that is in the stopped state * and has no time accumulated. */ public StopWatch() { reset(); } /** * Starts the stopwatch. Time starts accumulating now. */ public void start() { if (isRunning) return; isRunning = true; startTime = System.nanoTime(); } /** * Stops the stopwatch. Time stops accumulating and is * is added to the elapsed time. */ public void stop() { if (!isRunning) return; isRunning = false; long endTime = System.nanoTime(); elapsedTime = elapsedTime + endTime - startTime; } /** * Returns the total elapsed time. * * @return the total elapsed time */ public long getElapsedTime() { if (isRunning) { long endTime = System.nanoTime(); elapsedTime = elapsedTime + endTime - startTime; startTime = endTime; } return elapsedTime; } /** * Stops the watch and resets the elapsed time to 0. */ public void reset() { elapsedTime = 0; isRunning = false; } private long elapsedTime; private long startTime; private boolean isRunning; }