mirror of
https://github.com/songoda/EpicHeads.git
synced 2024-12-04 16:33:22 +01:00
43 lines
877 B
Java
43 lines
877 B
Java
|
package com.songoda.epicheads.util;
|
||
|
|
||
|
import java.text.DecimalFormat;
|
||
|
|
||
|
public class Clock {
|
||
|
|
||
|
private static final DecimalFormat millisecondsFormat = new DecimalFormat("#.##");
|
||
|
|
||
|
private final long start;
|
||
|
private long end;
|
||
|
|
||
|
public Clock() {
|
||
|
this.start = System.nanoTime();
|
||
|
this.end = -1;
|
||
|
}
|
||
|
|
||
|
public boolean hasEnded() {
|
||
|
return end >= 0;
|
||
|
}
|
||
|
|
||
|
public String stop() {
|
||
|
Checks.ensureTrue(!hasEnded(), "Timer has already been stopped.");
|
||
|
|
||
|
this.end = System.nanoTime();
|
||
|
|
||
|
return toString();
|
||
|
}
|
||
|
|
||
|
public double getDuration() {
|
||
|
return (hasEnded() ? end - start : System.nanoTime() - start) / 1e6;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String toString() {
|
||
|
return "(" + millisecondsFormat.format(getDuration()) + " ms)";
|
||
|
}
|
||
|
|
||
|
public static Clock start() {
|
||
|
return new Clock();
|
||
|
}
|
||
|
|
||
|
}
|