-
-
Save FerusGrim/a133532fb2a2318dc717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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