summaryrefslogtreecommitdiff
path: root/java/gjt/Stopwatch.java
blob: fc8963e88a1c0da68e1baa0205b3a1a8d6bab571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gjt;

import java.awt.*;

/**
 * A Thread that acts as a stopwatch.<p>
 *
 * Stopwatch starts running when it is constructed, and may be 
 * reset by the reset() method.  getHour(), getMinute(), 
 * getSecond(), and getMillisecond() are used to get the 
 * elapsed time since construction, or since the last reset.<p>
 *
 * toString() returns the elapsed time in the form of 
 * HH:MM:SS:mm, where HH == hours, MM == minutes, SS == seconds 
 * and mm == milliseconds.<p>
 *
 * Each Stopwatch may have a StopwatchClient associated with it.
 * If the StopwatchClient is non-null, the StopwatchClients' 
 * tick() method is invoked every 50 milliseconds.<p>
 *
 * @version 1.0, Apr 21 1996
 * @author  David Geary
 * @see     StopwatchClient
 * @see     gjt.animation.Sequence
 * @see     gjt.animation.Sprite
 */
public class Stopwatch extends Thread {
    private StopwatchClient client;
    private long            start, now, elapsed;
    private long            hour, minute, second, millisecond;

    public Stopwatch() {
        this(null);
    }
    public Stopwatch(StopwatchClient client) {
        start       = System.currentTimeMillis();
        this.client = client;
    }
    public void update() {
        now     = System.currentTimeMillis();
        elapsed = now - start;
        hour    = minute = second = millisecond = 0;

        second = elapsed / 1000;
        millisecond  = elapsed % 1000;
        millisecond  = (millisecond == 0) ? 0 : millisecond/10;

        if(second > 59) {
            minute = second / 60;
            second = second - (minute*60);
        }
        if(minute > 59) {
            hour   = minute / 60;
            minute = minute - (hour*60);
        }
    }
    public String toString() {
        update();
        return new String(stringValueOf(hour)   + ":" + 
                          stringValueOf(minute) + ":" + 
                          stringValueOf(second) + ":" +
                          stringValueOf(millisecond));
    }
    public long getHour        () { return hour;        }
    public long getMinute      () { return minute;      }
    public long getSecond      () { return second;      }
    public long getMillisecond () { return millisecond; }

    public long elapsedTime() { 
        update();
        return elapsed;
    }
    public void reset() {
        start = System.currentTimeMillis();
    }
    public void run() {
        while(true) {
            try {
                Thread.currentThread().sleep(50, 0);
                update();
                if(client != null)
                    client.tick();
            }
            catch(InterruptedException e) {
                Assert.notFalse(false);
            }
        }
    }
    private String stringValueOf(long l) {
        if(l < 10) return "0" + String.valueOf(l);
        else       return String.valueOf(l);

    }
}