RuneHive-Game
Loading...
Searching...
No Matches
Stopwatch.java
Go to the documentation of this file.
1package com.runehive.util;
2
3import java.util.concurrent.TimeUnit;
4
5public final class Stopwatch {
6
7 private long cachedTime;
8
9 public Stopwatch() {
10 cachedTime = System.nanoTime();
11 }
12
13 public static Stopwatch start() {
14 return new Stopwatch();
15 }
16
17 @Override
18 public String toString() {
19 return String.format("STOPWATCH[elapsed=%d]", elapsedTime());
20 }
21
22 public Stopwatch reset() {
23 cachedTime = System.nanoTime();
24 return this;
25 }
26
27 public Stopwatch reset(int delay, TimeUnit unit) {
28 cachedTime = System.nanoTime() + TimeUnit.NANOSECONDS.convert(delay, unit);
29 return this;
30 }
31
32 public Stopwatch reset(long millis) {
33 cachedTime = System.nanoTime() + TimeUnit.NANOSECONDS.convert(millis, TimeUnit.MILLISECONDS);
34 return this;
35 }
36
37 public long elapsedTime(TimeUnit unit) {
38 return unit.convert(System.nanoTime() - cachedTime, TimeUnit.NANOSECONDS);
39 }
40
41 public boolean finished() {
42 return cachedTime - System.nanoTime() <= 0;
43 }
44
45 public long elapsedTime() {
46 return elapsedTime(TimeUnit.MILLISECONDS);
47 }
48
49 public boolean elapsed(long time, TimeUnit unit) {
50 return elapsedTime(unit) >= time;
51 }
52
53 public boolean elapsed(long time) {
54 return elapsed(time, TimeUnit.MILLISECONDS);
55 }
56
57 public long getCachedTime() {
58 return cachedTime;
59 }
60
61 public void setCachedTime(long cachedTime) {
62 this.cachedTime = cachedTime;
63 }
64
65}
boolean elapsed(long time, TimeUnit unit)
long elapsedTime(TimeUnit unit)
Stopwatch reset(int delay, TimeUnit unit)
void setCachedTime(long cachedTime)
static Stopwatch start()
Stopwatch reset(long millis)
boolean elapsed(long time)