001: package fri.util;
002:
003: /**
004: Stopwatch, for testing purposes. Provides suspend and resume methods.
005: */
006:
007: public class TimeStopper {
008: private long time1 = 0L, time2 = 0L;
009: private long timeSum = 0L;
010: private long lastIntervalStop = 0L;
011:
012: /** Catch current time. Timer is running. This is a call to resume(). */
013: public TimeStopper() {
014: this (true);
015: }
016:
017: /** If doStart is false, start() or resume() must be called explicitly. */
018: public TimeStopper(boolean doStart) {
019: if (doStart)
020: resume();
021: }
022:
023: /** Returns true if the timer is running, else it has been stopped or suspended. */
024: public boolean isRunning() {
025: return time1 > 0L;
026: }
027:
028: /**
029: Interrupt timer. Time gets saved. This method works if the timer
030: was started or resumed before, else it does nothing.
031: */
032: public void suspend() {
033: if (isRunning()) {
034: time2 = System.currentTimeMillis();
035: timeSum += time2 - time1;
036: time1 = time2 = 0L;
037: }
038: }
039:
040: /**
041: Start again after interruption. Pause time will not be added.
042: This method works if the timer was suspended before, else it does nothing.
043: */
044: public void resume() {
045: if (isRunning() == false)
046: lastIntervalStop = time1 = time2 = System
047: .currentTimeMillis();
048: }
049:
050: /** Stop the timer and return current time sum. */
051: public String stop() {
052: return getTime(true);
053: }
054:
055: /** Start the timer after a stop(). The sum of time is forgotten. */
056: public void start() {
057: resume();
058: timeSum = 0L;
059: }
060:
061: /**
062: Returns current time representation "HH:MM:SS".
063: The timer will be stopped for calculation of time and then be resumed.
064: */
065: public String getTime() {
066: return getTime(false);
067: }
068:
069: private String getTime(boolean doStop) {
070: suspend();
071: long elapsed = timeSum / 1000L;
072: long sec = elapsed % 60L;
073: long min = elapsed / 60L;
074: if (min > 60L)
075: min = min % 60L;
076: long hours = elapsed / 3600L;
077: if (doStop == false)
078: resume();
079: return hours + ":" + min + ":" + sec;
080: }
081:
082: /**
083: Returns current time representation in millisconds.
084: The timer will be stopped for calculation and then be resumed.
085: */
086: public String getTimeMillis() {
087: suspend();
088: resume();
089: return new Long(timeSum).toString();
090: }
091:
092: /**
093: Stop the timer and return current time sum.
094: */
095: public String stopMillis() {
096: suspend();
097: return new Long(timeSum).toString();
098: }
099:
100: /**
101: Returns the time interval since start or the last getInterval() call.
102: */
103: public String getInterval() {
104: long time = System.currentTimeMillis();
105: long interval = time - lastIntervalStop;
106: lastIntervalStop = time;
107: return new Long(interval).toString();
108: }
109:
110: // test main
111:
112: public static final void main(String[] args) {
113: TimeStopper timer = new TimeStopper();
114: try {
115: Thread.sleep(2000);
116: } catch (Exception e) {
117: }
118: System.err.println(timer.stop());
119: }
120:
121: }
|