diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/util/Timer.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/util/Timer.java | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/util/Timer.java b/src/uk/ac/ox/cs/pagoda/util/Timer.java new file mode 100644 index 0000000..d1814a4 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/util/Timer.java | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.util; | ||
| 2 | |||
| 3 | public class Timer { | ||
| 4 | |||
| 5 | double pastTime = 0; | ||
| 6 | boolean active = false; | ||
| 7 | |||
| 8 | long startTime; | ||
| 9 | |||
| 10 | public Timer() { | ||
| 11 | resume(); | ||
| 12 | } | ||
| 13 | |||
| 14 | public void resume() { | ||
| 15 | if (active) return; | ||
| 16 | startTime = System.currentTimeMillis();; | ||
| 17 | active = true; | ||
| 18 | } | ||
| 19 | |||
| 20 | public double duration() { | ||
| 21 | double time = pastTime; | ||
| 22 | if (active) | ||
| 23 | time += (System.currentTimeMillis() - startTime) / 1000.; | ||
| 24 | return time; | ||
| 25 | } | ||
| 26 | |||
| 27 | public void pause() { | ||
| 28 | if (!active) return ; | ||
| 29 | pastTime = duration(); | ||
| 30 | active = false; | ||
| 31 | } | ||
| 32 | |||
| 33 | public double reset() { | ||
| 34 | double ret = duration(); | ||
| 35 | pastTime = 0; | ||
| 36 | active = false; | ||
| 37 | resume(); | ||
| 38 | return ret; | ||
| 39 | } | ||
| 40 | |||
| 41 | double timeout = -1; | ||
| 42 | |||
| 43 | public boolean timeOut() { | ||
| 44 | if (timeout < 0) return false; | ||
| 45 | return duration() > timeout; | ||
| 46 | } | ||
| 47 | |||
| 48 | public void setTimeout(double timeout) { | ||
| 49 | this.timeout = timeout; | ||
| 50 | } | ||
| 51 | |||
| 52 | } | ||
