new project structure
- use objects and inheritance - enabled combined version for drinking and loading bar without impacting any of them
This commit is contained in:
148
zeitlaeufer/src/main/java/de/szimnau/AbstractLoadingBar.java
Normal file
148
zeitlaeufer/src/main/java/de/szimnau/AbstractLoadingBar.java
Normal file
@@ -0,0 +1,148 @@
|
||||
package de.szimnau;
|
||||
|
||||
import de.szimnau.tools.FormatTools;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static de.szimnau.tools.CommonTools.print;
|
||||
import static de.szimnau.tools.CommonTools.println;
|
||||
|
||||
|
||||
public abstract class AbstractLoadingBar {
|
||||
|
||||
private final LocalTime startTime;
|
||||
private LocalTime endTime;
|
||||
private long totalMinutes;
|
||||
private BigDecimal totalMinutesBD;
|
||||
|
||||
|
||||
protected AbstractLoadingBar(LocalTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
|
||||
protected AbstractLoadingBar(LocalTime startTime, LocalTime endTime) {
|
||||
this.startTime = startTime;
|
||||
setEndTime(endTime);
|
||||
}
|
||||
|
||||
|
||||
protected AbstractLoadingBar(LocalTime startTime, long totalMinutes) {
|
||||
this.startTime = startTime;
|
||||
this.endTime = startTime.plusMinutes(totalMinutes);
|
||||
this.totalMinutes = totalMinutes;
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
}
|
||||
|
||||
|
||||
protected LocalTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
|
||||
protected LocalTime getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
|
||||
protected void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
}
|
||||
|
||||
|
||||
protected long getTotalMinutes() {
|
||||
return totalMinutes;
|
||||
}
|
||||
|
||||
|
||||
protected BigDecimal getTotalMinutesBD() {
|
||||
return totalMinutesBD;
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBar() {
|
||||
showLoadingBar(false, false, 0);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug() {
|
||||
showLoadingBar(true, true, 100L);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug(long millisWaiting) {
|
||||
showLoadingBar(true, true, millisWaiting);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug(boolean passedMinutesZero) {
|
||||
showLoadingBar(true, passedMinutesZero, 100L);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug(boolean passedMinutesZero, long millisWaiting) {
|
||||
showLoadingBar(true, passedMinutesZero, millisWaiting);
|
||||
}
|
||||
|
||||
|
||||
private void showLoadingBar(boolean debug, boolean passedMinutesZero, long millisWaiting) {
|
||||
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
if (debug && passedMinutesZero) {
|
||||
passedMinutes = 0;
|
||||
}
|
||||
if (passedMinutes > totalMinutes) {
|
||||
passedMinutes = totalMinutes;
|
||||
} else if (passedMinutes < 0) {
|
||||
var now = LocalTime.now().truncatedTo(ChronoUnit.SECONDS);
|
||||
println("!ACHTUNG! Startzeit \"" + startTime + ":00\" liegt in der Zukunft von jetzt an (" + now + ") gesehen.");
|
||||
}
|
||||
println(FormatTools.minutesToTimeString(totalMinutes) + " gesamt; Endzeit: " + FormatTools.TIME_FORMATTER.format(endTime));
|
||||
while (passedMinutes < totalMinutes) {
|
||||
print(fillLoadingBar(passedMinutes, true));
|
||||
waitUntilNextMinute(debug, millisWaiting);
|
||||
passedMinutes++;
|
||||
}
|
||||
println(fillLoadingBar(passedMinutes, false));
|
||||
}
|
||||
|
||||
|
||||
protected abstract String fillLoadingBar(long passedMinutes, boolean progressive);
|
||||
|
||||
|
||||
protected void waitUntilNextMinute() {
|
||||
waitUntilNextMinute(false, 0L);
|
||||
}
|
||||
|
||||
|
||||
protected void waitUntilNextMinuteDebug() {
|
||||
waitUntilNextMinute(true, 100L);
|
||||
}
|
||||
|
||||
|
||||
protected void waitUntilNextMinuteDebug(long millisWaiting) {
|
||||
waitUntilNextMinute(true, millisWaiting);
|
||||
}
|
||||
|
||||
|
||||
private void waitUntilNextMinute(boolean debug, long millisWaiting) {
|
||||
try {
|
||||
if (debug) {
|
||||
TimeUnit.MILLISECONDS.sleep(millisWaiting);
|
||||
return;
|
||||
}
|
||||
var now = LocalTime.now();
|
||||
var oneMinuteLater = now.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
|
||||
/* We wait whole seconds to not make it overly complicated.
|
||||
That results in cut milliseconds: if we would have to wait 1 second and 526 milliseconds, we wait only 1 second.
|
||||
So, adjust for ignored milliseconds, add +1 second as it is better to switch between 00 and 01 as between 59 and 00 */
|
||||
TimeUnit.SECONDS.sleep(now.until(oneMinuteLater, ChronoUnit.SECONDS) + 1);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user