Skip to content

Instantly share code, notes, and snippets.

@FerusGrim
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FerusGrim/a133532fb2a2318dc717 to your computer and use it in GitHub Desktop.
Save FerusGrim/a133532fb2a2318dc717 to your computer and use it in GitHub Desktop.
package me.ferusgrim.utils;
import java.util.concurrent.TimeUnit;
public class TimeTracker {
private final TimeUnit measurement;
private final long waitTime;
private long startTime = 0;
public TimeTracker(TimeUnit measurement, long waitTime, boolean startTimer) {
this.measurement = measurement;
this.waitTime = waitTime;
if (startTimer) {
this.startTime = System.currentTimeMillis();
}
}
public TimeTracker(TimeUnit measurement, long waitTime) {
this(measurement, waitTime, false);
}
public TimeUnit getMeasurement() {
return this.measurement;
}
public long getWaitTime() {
return this.waitTime;
}
public long getStartTime() {
return this.startTime;
}
public long getEndTime() {
return this.startTime + this.measurement.toMillis(this.waitTime);
}
public TimeTracker startTimer() {
if (this.startTime == 0) {
throw new UnsupportedOperationException("Timer has already been started!");
}
this.startTime = System.currentTimeMillis();
return this;
}
public TimeTracker resetTimer() {
this.startTime = 0;
return this;
}
public TimeTracker restart() {
this.resetTimer();
this.startTimer();
return this;
}
public boolean hasEnoughTimePassed() {
if (this.startTime == 0) {
throw new UnsupportedOperationException("Timer hasn't been started, yet!");
}
return System.currentTimeMillis() >= this.getEndTime();
}
public boolean isRunning() {
return this.startTime > 0;
}
public TimeTracker duplicate() {
return new TimeTracker(this.measurement, this.waitTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment