-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAthenaTimer.java
More file actions
57 lines (50 loc) · 1.66 KB
/
Copy pathAthenaTimer.java
File metadata and controls
57 lines (50 loc) · 1.66 KB
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
/**
* Simple elapsed-time stopwatch. All external values are kept in a single
* time base (relative milliseconds) to avoid mixing deltas and absolute
* System.currentTimeMillis() timestamps the way the original implementation did.
*
* - When playing: get() returns accumulated + (now - startMs)
* - When paused: get() returns accumulated
*/
public class AthenaTimer {
private long startMs = 0L; // when the current play span began (abs ms)
private int accum = 0; // ms accumulated from previous play spans
private boolean isPlaying = false;
public AthenaTimer(long timerState) {
// `timerState` is treated as "this timer was started N ms ago in wall time".
// Keep it internally consistent with the playing model.
startMs = timerState;
accum = 0;
isPlaying = true;
}
public int get() {
if (isPlaying) {
return accum + (int) (System.currentTimeMillis() - startMs);
}
return accum;
}
/**
* Returns the sum of current elapsed time and `val`, in milliseconds.
* Preserves the original public contract used by the interpreter binding.
*/
public int set(int val) {
return get() + val;
}
public void pause() {
if (!isPlaying) return;
accum += (int) (System.currentTimeMillis() - startMs);
isPlaying = false;
}
public void resume() {
if (isPlaying) return;
startMs = System.currentTimeMillis();
isPlaying = true;
}
public void reset() {
accum = 0;
startMs = System.currentTimeMillis();
}
public boolean playing() {
return isPlaying;
}
}