[FEATURE] Added days into Timer

Credits to @parnassian
This commit is contained in:
SCoutinho
2018-01-02 17:32:45 +00:00
parent a51cd95aae
commit 58d9e23094
@@ -1,120 +1,124 @@
package org.parabot.environment.api.utils; package org.parabot.environment.api.utils;
/** /**
*
* A simple timer class * A simple timer class
* *
* @author Everel, Parameter * @author Everel, Parameter
*
*/ */
public class Timer { public class Timer {
private long start; private long start;
private long end; private long end;
/** /**
* Timer Constructor * Timer Constructor
* *
* @param end * @param end
*/ */
public Timer(long end) { public Timer(long end) {
start = System.currentTimeMillis();
this.end = System.currentTimeMillis() + end;
}
start = System.currentTimeMillis(); /**
this.end = System.currentTimeMillis() + end; * Timer Constructor
} */
public Timer() {
this(0);
}
/** /**
* Timer Constructor * Determines the remaining time left.
*/ *
public Timer() { * @return the remaining time.
this(0); */
} public long getRemaining() {
return end - System.currentTimeMillis();
}
/** /**
* Determines the remaining time left. * Determines if the end time has been reached, does not mean it stopped
* * running.
* @return the remaining time. */
*/ public boolean isFinished() {
public long getRemaining() { return System.currentTimeMillis() > end;
return end - System.currentTimeMillis(); }
}
/** /**
* Determines if the end time has been reached, does not mean it stopped * Stops and resets the timer
* running. */
*/ public void restart() {
public boolean isFinished() { stop();
return System.currentTimeMillis() > end; reset();
} }
/** /**
* Stops and resets the timer * Resets the timer if stopped
*/ */
public void restart() { public void reset() {
stop(); if (start == 0) {
reset(); start = System.currentTimeMillis();
} }
}
/** /**
* Resets the timer if stopped * Resets the timer
*/ */
public void reset() { public void stop() {
if (start == 0) { end = (end - start) + System.currentTimeMillis();
start = System.currentTimeMillis(); start = 0;
} }
}
/** /**
* Resets the timer * Determines if timer is running
*/ *
public void stop() { * @return <b>true</b> if timer is running
end = (end - start) + System.currentTimeMillis(); */
start = 0; public boolean isRunning() {
} return start != 0;
}
/** /**
* Determines if timer is running * Gets the run time in long millis.
* *
* @return <b>true</b> if timer is running * @return the elapsed time.
*/ */
public boolean isRunning() { public long getElapsedTime() {
return start != 0; return System.currentTimeMillis() - start;
} }
/** /**
* Gets the run time in long millis. * Calculates hourly gains based on given variable
* *
* @return the elapsed time. * @param gained variable
*/ *
public long getElapsedTime() { * @return hourly gains
return System.currentTimeMillis() - start; */
} public int getPerHour(final int gained) {
return (int) ((gained) * 3600000D / (System.currentTimeMillis() - start));
}
/** /**
* Calculates hourly gains based on given variable * Generates string based on DD:HH:MM:SS
* *
* @param gained * @return String
* variable */
* @return hourly gains @Override
*/ public String toString() {
public int getPerHour(final int gained) { StringBuilder b = new StringBuilder();
return (int) ((gained) * 3600000D / (System.currentTimeMillis() - start)); long elapsed = getElapsedTime();
} int day = (int) (elapsed / 86400000);
elapsed -= day * 86400000;
/** int hour = (int) (elapsed / 3600000);
* Generates string based on HH:MM:SS elapsed -= hour * 3600000;
* int minute = (int) (elapsed / 60000);
* @return String elapsed -= minute * 60000;
*/ int second = (int) (elapsed / 1000);
@Override if (day > 0) {
public String toString() { b.append(day < 10 ? "0" : "").append(day).append("d:");
StringBuilder b = new StringBuilder(); }
long elapsed = getElapsedTime(); b.append(hour < 10 ? "0" : "").append(hour).append(":");
int second = (int) (elapsed / 1000 % 60); b.append(minute < 10 ? "0" : "").append(minute).append(":");
int minute = (int) (elapsed / 60000 % 60); b.append(second < 10 ? "0" : "").append(second);
int hour = (int) (elapsed / 3600000 % 60); return new String(b);
b.append(hour < 10 ? "0" : "").append(hour).append(":"); }
b.append(minute < 10 ? "0" : "").append(minute).append(":");
b.append(second < 10 ? "0" : "").append(second);
return new String(b);
}
} }