KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fri > util > TimeStopper


1 package fri.util;
2
3 /**
4     Stopwatch, for testing purposes. Provides suspend and resume methods.
5 */

6
7 public class TimeStopper
8 {
9     private long time1 = 0L, time2 = 0L;
10     private long timeSum = 0L;
11     private long lastIntervalStop = 0L;
12
13
14     /** Catch current time. Timer is running. This is a call to resume(). */
15     public TimeStopper() {
16         this(true);
17     }
18
19     /** If doStart is false, start() or resume() must be called explicitly. */
20     public TimeStopper(boolean doStart) {
21         if (doStart)
22             resume();
23     }
24
25     /** Returns true if the timer is running, else it has been stopped or suspended. */
26     public boolean isRunning() {
27         return time1 > 0L;
28     }
29     
30     /**
31         Interrupt timer. Time gets saved. This method works if the timer
32         was started or resumed before, else it does nothing.
33     */

34     public void suspend() {
35         if (isRunning()) {
36             time2 = System.currentTimeMillis();
37             timeSum += time2 - time1;
38             time1 = time2 = 0L;
39         }
40     }
41     
42     /**
43         Start again after interruption. Pause time will not be added.
44         This method works if the timer was suspended before, else it does nothing.
45     */

46     public void resume() {
47         if (isRunning() == false)
48             lastIntervalStop = time1 = time2 = System.currentTimeMillis();
49     }
50     
51     /** Stop the timer and return current time sum. */
52     public String JavaDoc stop() {
53         return getTime(true);
54     }
55
56     /** Start the timer after a stop(). The sum of time is forgotten. */
57     public void start() {
58         resume();
59         timeSum = 0L;
60     }
61     
62     /**
63         Returns current time representation "HH:MM:SS".
64         The timer will be stopped for calculation of time and then be resumed.
65     */

66     public String JavaDoc getTime() {
67         return getTime(false);
68     }
69     
70     private String JavaDoc getTime(boolean doStop) {
71         suspend();
72         long elapsed = timeSum / 1000L;
73         long sec = elapsed % 60L;
74         long min = elapsed / 60L;
75         if (min > 60L)
76             min = min % 60L;
77         long hours = elapsed / 3600L;
78         if (doStop == false)
79             resume();
80         return hours+":"+min+":"+sec;
81     }
82     
83     /**
84         Returns current time representation in millisconds.
85         The timer will be stopped for calculation and then be resumed.
86     */

87     public String JavaDoc getTimeMillis() {
88         suspend();
89         resume();
90         return new Long JavaDoc(timeSum).toString();
91     }
92     
93     /**
94         Stop the timer and return current time sum.
95     */

96     public String JavaDoc stopMillis() {
97         suspend();
98         return new Long JavaDoc(timeSum).toString();
99     }
100     
101     /**
102         Returns the time interval since start or the last getInterval() call.
103     */

104     public String JavaDoc getInterval() {
105         long time = System.currentTimeMillis();
106         long interval = time - lastIntervalStop;
107         lastIntervalStop = time;
108         return new Long JavaDoc(interval).toString();
109     }
110     
111     
112     
113     // test main
114

115     public static final void main(String JavaDoc [] args) {
116         TimeStopper timer = new TimeStopper();
117         try { Thread.sleep(2000); } catch (Exception JavaDoc e) {}
118         System.err.println(timer.stop());
119     }
120
121 }
Popular Tags