Compare commits
64 Commits
e0750ef26f
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
13c783a762 | ||
|
d881edd247 | ||
|
f514e1c6f8 | ||
|
c6a346df00 | ||
|
bad7fa46eb | ||
|
b63326b0ac | ||
|
20f3049239 | ||
|
63b0e39ad5 | ||
378ab4c026 | |||
5485517150 | |||
8ed458b4f3 | |||
a9903b023f | |||
f1c85e0fb0 | |||
f52eb4755a | |||
a664f42f97 | |||
fd81d268c1 | |||
b0cf3a02c2 | |||
29807d7eaf | |||
f97f4f86d3 | |||
8c566d2104 | |||
74f2acda17 | |||
128e455985 | |||
854dc0697f | |||
426009bdfe | |||
c976a50051 | |||
a09bdcc7aa | |||
f17eb56506 | |||
2caf663ba7 | |||
4434129f1c | |||
f0bda09710 | |||
1f1e06a8e1 | |||
1e40a19b69 | |||
45bee2714b | |||
971bf22495 | |||
e6ef9ec87f | |||
7fc7efd166 | |||
23d73ee19d | |||
853beb07d2 | |||
e88c1a3d49 | |||
d0687c9568 | |||
d9553ace7a | |||
f8e1b13ae5 | |||
f5474a59fa | |||
d87e503cfe | |||
ad25f48116 | |||
2231cf9b3f | |||
f854d2460c | |||
591e256fa5 | |||
64d29125e9 | |||
710ff47a79 | |||
34fcaf32fe | |||
4753243c10 | |||
eed441343c | |||
84a2a94064 | |||
ccb9c06f98 | |||
c3be3892bd | |||
468403801b | |||
b2800a2f78 | |||
4581cec622 | |||
dc99ca2ee4 | |||
d73f2e0cd4 | |||
7cab32467d | |||
a77c8e3e5f | |||
c9d33e6f76 |
@@ -69,6 +69,12 @@ class Darlehenberechner {
|
||||
}
|
||||
|
||||
|
||||
public Konfiguration setLaufzeitJahre(Integer jahre) {
|
||||
this.laufzeitMonate = (jahre * 12);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Konfiguration setLaufzeit(Integer jahre, Integer monate) {
|
||||
this.laufzeitMonate = (jahre * 12) + monate;
|
||||
return this;
|
||||
@@ -172,7 +178,7 @@ class Darlehenberechner {
|
||||
}
|
||||
|
||||
|
||||
private static void askParametersAndRun() throws IOException {
|
||||
private static void askParametersAndRun() throws ParseException, IOException {
|
||||
DECIMAL_FORMAT.setParseBigDecimal(true);
|
||||
|
||||
var konfig = new Konfiguration();
|
||||
@@ -211,7 +217,7 @@ class Darlehenberechner {
|
||||
}
|
||||
|
||||
|
||||
private static void parseParametersAndRun(String[] args) {
|
||||
private static void parseParametersAndRun(String[] args) throws ParseException {
|
||||
var konfig = new Konfiguration();
|
||||
int count = 0;
|
||||
DECIMAL_FORMAT.setParseBigDecimal(true);
|
||||
|
165
DrinkingBar.java
Normal file
165
DrinkingBar.java
Normal file
@@ -0,0 +1,165 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class DrinkingBar {
|
||||
|
||||
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
|
||||
private static final int MINS_PER_HOUR = 60;
|
||||
private static final int MINS_PER_HALF_HOUR = MINS_PER_HOUR / 2;
|
||||
private static final int MINUTES_BEFORE_PAUSE = 4 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
|
||||
private static final int MINUTES_WITH_PAUSE = 6 * MINS_PER_HOUR;
|
||||
private static final int DEFAULT_TOTAL_TIME = 8 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
|
||||
private static final BigDecimal DEFAULT_TOTAL_TIME_BD = BigDecimal.valueOf(DEFAULT_TOTAL_TIME);
|
||||
private static final BigDecimal DEFAULT_TOTAL_LITRES = BigDecimal.valueOf(2.0);
|
||||
private static final BigDecimal QUARTER_LITRE = BigDecimal.valueOf(0.25);
|
||||
private static final DecimalFormat LITRE_FORMAT = new DecimalFormat("0.00");
|
||||
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
||||
private static final BigDecimal MINS_PER_HOUR_BD = BigDecimal.valueOf(MINS_PER_HOUR);
|
||||
private static final MathContext MC_INTEGER = new MathContext(1, RoundingMode.HALF_EVEN);
|
||||
|
||||
private final LocalTime startTime;
|
||||
private LocalTime endTime;
|
||||
private long totalMinutes;
|
||||
private BigDecimal totalMinutesBD;
|
||||
private BigDecimal totalLitres;
|
||||
|
||||
|
||||
private DrinkingBar(LocalTime startTime) {
|
||||
this.startTime = startTime;
|
||||
this.totalMinutes = DEFAULT_TOTAL_TIME;
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
this.endTime = startTime.plusMinutes(totalMinutes);
|
||||
this.totalLitres = DEFAULT_TOTAL_LITRES;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||
print("Ankunftszeit: ");
|
||||
var startTime = LocalTime.parse(br.readLine(), TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
var db = new DrinkingBar(startTime);
|
||||
db.showLoadingBar();
|
||||
}
|
||||
|
||||
|
||||
private static void println(Object o) {
|
||||
System.out.println(o);
|
||||
}
|
||||
|
||||
|
||||
private static void print(Object o) {
|
||||
System.out.print(o);
|
||||
}
|
||||
|
||||
|
||||
protected long getPassedMinutes() {
|
||||
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
private void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
extraInitEndTimeTotalMinutes();
|
||||
}
|
||||
|
||||
|
||||
protected void extraInitEndTimeTotalMinutes() {
|
||||
// correct necessary litres to drink based on the end time.
|
||||
// lower the volume in quarter litre steps
|
||||
BigDecimal calcTotalLitres = DEFAULT_TOTAL_LITRES;
|
||||
BigDecimal totalLitresFromMinutes = DEFAULT_TOTAL_LITRES
|
||||
.multiply(totalMinutesBD) // reverse dreisatz
|
||||
.divide(DEFAULT_TOTAL_TIME_BD, MathContext.DECIMAL64);
|
||||
do {
|
||||
calcTotalLitres = calcTotalLitres.subtract(QUARTER_LITRE);
|
||||
} while (calcTotalLitres.compareTo(totalLitresFromMinutes) >= 0);
|
||||
// add quarter since we always did a step "too many", due to the do ... while loop
|
||||
this.totalLitres = calcTotalLitres.add(QUARTER_LITRE);
|
||||
}
|
||||
|
||||
|
||||
private void showLoadingBar() {
|
||||
long passedMinutes = getPassedMinutes();
|
||||
// long passedMinutes = 0; // DEBUG
|
||||
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(minutesToTimeString(totalMinutesBD) + " gesamt; Endzeit: " + TIME_FORMATTER.format(endTime));
|
||||
while (passedMinutes < totalMinutes) {
|
||||
print(fillLoadingBar(passedMinutes, true));
|
||||
waitUntilNextMinute();
|
||||
passedMinutes++;
|
||||
}
|
||||
println(fillLoadingBar(passedMinutes, false));
|
||||
}
|
||||
|
||||
|
||||
private String fillLoadingBar(long passedMinutes, boolean progressive) {
|
||||
long effectivePassedMinutes = passedMinutes < 0 ? 0 : passedMinutes;
|
||||
if (totalMinutes > MINUTES_WITH_PAUSE && passedMinutes > MINUTES_BEFORE_PAUSE && passedMinutes <= MINUTES_WITH_PAUSE) {
|
||||
effectivePassedMinutes = MINUTES_BEFORE_PAUSE;
|
||||
}
|
||||
var effectivePassedMinutesBD = BigDecimal.valueOf(effectivePassedMinutes);
|
||||
BigDecimal currentLitres = totalLitres
|
||||
.multiply(effectivePassedMinutesBD) // reverse dreisatz
|
||||
.divide(totalMinutesBD, MathContext.DECIMAL64)
|
||||
.add(QUARTER_LITRE);
|
||||
BigDecimal printedLitres = currentLitres.subtract(currentLitres.remainder(QUARTER_LITRE, MathContext.DECIMAL64));
|
||||
/* BigDecimal currentProgressToNextStep = ONE_HUNDRED_PERCENT
|
||||
.multiply(currentLitres.subtract(printedLitres)) // reverse dreisatz
|
||||
.divide(QUARTER_LITRE, MathContext.DECIMAL64); */
|
||||
BigDecimal minutesToNextStep = getMinutesToNextStep(currentLitres);
|
||||
String progressivePart = progressive ? "\r" : "";
|
||||
return progressivePart + "Aktuelles Volumen: " + LITRE_FORMAT.format(printedLitres) + "L - "
|
||||
// + PERCENTAGE_FORMAT.format(currentProgressToNextStep) + "% - "
|
||||
+ minutesToTimeString(minutesToNextStep);
|
||||
}
|
||||
|
||||
|
||||
private BigDecimal getMinutesToNextStep(BigDecimal currentLitres) {
|
||||
// berechne Liter benötigt bis zum nächsten 0.25er Schritt
|
||||
BigDecimal litresToNextStep = QUARTER_LITRE.subtract(currentLitres.remainder(QUARTER_LITRE));
|
||||
// berechne Minuten benötigt für 1 Liter
|
||||
BigDecimal minutesPerLitre = totalMinutesBD.divide(totalLitres, MathContext.DECIMAL64);
|
||||
// berechne Minuten benötigt bis zum nächsten 0.25er Schritt
|
||||
// runde auf ganze Zahl, da wir nur ganze Minuten anzeigen und damit 1.999 = 2 Minuten sind
|
||||
return minutesPerLitre.multiply(litresToNextStep).setScale(0, RoundingMode.HALF_EVEN);
|
||||
}
|
||||
|
||||
|
||||
private String minutesToTimeString(BigDecimal minutes) {
|
||||
BigDecimal[] hoursAndMinutes = minutes.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
|
||||
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
|
||||
}
|
||||
|
||||
|
||||
private void waitUntilNextMinute() {
|
||||
try {
|
||||
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);
|
||||
// TimeUnit.MILLISECONDS.sleep(100L); // DEBUG
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
}
|
||||
}
|
239
FabianUtil.java
239
FabianUtil.java
@@ -1,20 +1,26 @@
|
||||
package de.vorsorge.theo.util;
|
||||
package FIXME;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.Duration;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class FabianUtil {
|
||||
|
||||
private static final Logger LOG = new Logger(FabianUtil.class);
|
||||
|
||||
private static final String STD_DIR_TEMP = "/FIXME/temp/";
|
||||
private static final String TEST_OUTPUT_DIR = "testOutput/";
|
||||
public static final String ENDING_TXT = "txt";
|
||||
public static final String ENDING_JSON = "json";
|
||||
public static final String ENDING_HTML = "html";
|
||||
@@ -25,8 +31,92 @@ public class FabianUtil {
|
||||
private FabianUtil() {}
|
||||
|
||||
|
||||
public static class Logger extends java.util.logging.Logger {
|
||||
|
||||
public Logger(Class<?> clazz) {
|
||||
this(java.util.logging.Logger.getLogger(clazz.getName()));
|
||||
}
|
||||
|
||||
|
||||
public Logger(java.util.logging.Logger logger) {
|
||||
super(logger.getName(), logger.getResourceBundleName());
|
||||
}
|
||||
|
||||
|
||||
public void severe(String msg, Object param) {
|
||||
log(Level.SEVERE, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void severe(String msg, Object... params) {
|
||||
log(Level.SEVERE, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void warning(String msg, Object param) {
|
||||
log(Level.WARNING, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void warning(String msg, Object... params) {
|
||||
log(Level.WARNING, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void info(String msg, Object param) {
|
||||
log(Level.INFO, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void info(String msg, Object... params) {
|
||||
log(Level.INFO, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void config(String msg, Object param) {
|
||||
log(Level.CONFIG, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void config(String msg, Object... params) {
|
||||
log(Level.CONFIG, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void fine(String msg, Object param) {
|
||||
log(Level.FINE, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void fine(String msg, Object... params) {
|
||||
log(Level.FINE, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void finer(String msg, Object param) {
|
||||
log(Level.FINER, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void finer(String msg, Object... params) {
|
||||
log(Level.FINER, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void finest(String msg, Object param) {
|
||||
log(Level.FINEST, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void finest(String msg, Object... params) {
|
||||
log(Level.FINEST, msg, params);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ProgressHandler {
|
||||
|
||||
private static final Logger LOG = new Logger(ProgressHandler.class);
|
||||
private static final DecimalFormat TIME_DECIMAL_FORMAT = new DecimalFormat("00");
|
||||
protected static final int DEFAULT_LOG_DISTANCE = 50;
|
||||
protected static final int DEFAULT_TIME_LOG_DISTANCE = 15;
|
||||
@@ -36,22 +126,29 @@ public class FabianUtil {
|
||||
private final int timeLogDistance;
|
||||
private BigDecimal counter = BigDecimal.ZERO;
|
||||
private ZonedDateTime lastLoggedAt;
|
||||
protected final PrintStream printer;
|
||||
|
||||
|
||||
public ProgressHandler() {
|
||||
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE);
|
||||
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, null);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandler(int logDistance) {
|
||||
this(logDistance, DEFAULT_TIME_LOG_DISTANCE);
|
||||
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, null);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandler(int logDistance, int timeLogDistance) {
|
||||
this(logDistance, timeLogDistance, null);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandler(int logDistance, int timeLogDistance, PrintStream printer) {
|
||||
startTime = ZonedDateTime.now();
|
||||
this.logDistance = validateBD(logDistance, "Log-Distanz");
|
||||
this.timeLogDistance = validateInt(timeLogDistance, "Zeitbasierte Log-Distanz");
|
||||
this.printer = printer;
|
||||
}
|
||||
|
||||
|
||||
@@ -68,12 +165,17 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
protected synchronized BigDecimal getCounter() {
|
||||
protected Logger logger() {
|
||||
return LOG;
|
||||
}
|
||||
|
||||
|
||||
protected final synchronized BigDecimal counter() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
protected synchronized int getTimeLogDistance() {
|
||||
protected final synchronized int timeLogDistance() {
|
||||
return timeLogDistance;
|
||||
}
|
||||
|
||||
@@ -103,7 +205,7 @@ public class FabianUtil {
|
||||
|
||||
|
||||
protected synchronized boolean shouldLog() {
|
||||
return counter.remainder(logDistance).signum() == 0 || isBored();
|
||||
return (printer != null || logger().isLoggable(Level.INFO)) && (counter.remainder(logDistance).signum() == 0 || isBored());
|
||||
}
|
||||
|
||||
|
||||
@@ -120,7 +222,11 @@ public class FabianUtil {
|
||||
|
||||
public synchronized void log() {
|
||||
Duration timePassed = getTimePassed();
|
||||
System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
if (printer != null) {
|
||||
printer.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
} else {
|
||||
logger().info("{} Stück erledigt. {} vergangen.", counter, formatDuration(timePassed));
|
||||
}
|
||||
updateLastLoggedAt();
|
||||
}
|
||||
|
||||
@@ -147,44 +253,61 @@ public class FabianUtil {
|
||||
|
||||
public synchronized void logFinal() {
|
||||
Duration timePassed = getTimePassed();
|
||||
System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
if (printer != null) {
|
||||
printer.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
} else {
|
||||
logger().info("{} Stück (100 %) erledigt. {} vergangen.", counter, formatDuration(timePassed));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ProgressHandlerWithTotal extends ProgressHandler {
|
||||
|
||||
private static final Logger LOG = new Logger(ProgressHandlerWithTotal.class);
|
||||
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
||||
private static final BigDecimal ONE_HUNDRED_PERCENT = BigDecimal.valueOf(100);
|
||||
|
||||
private final BigDecimal total;
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int total) {
|
||||
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, total);
|
||||
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, total, null);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int logDistance, int total) {
|
||||
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, total);
|
||||
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, total, null);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total) {
|
||||
super(logDistance, timeLogDistance);
|
||||
this(logDistance, timeLogDistance, total, null);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total, PrintStream printer) {
|
||||
super(logDistance, timeLogDistance, printer);
|
||||
this.total = validateBD(total, "Gesamt-Anzahl");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Logger logger() {
|
||||
return LOG;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected synchronized boolean shouldLog() {
|
||||
return super.shouldLog() && getCounter().compareTo(total) <= 0;
|
||||
return super.shouldLog() && counter().compareTo(total) <= 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized void log() {
|
||||
BigDecimal counter = getCounter();
|
||||
BigDecimal percentage = BigDecimal.valueOf(100).divide(total, MathContext.DECIMAL64).multiply(counter);
|
||||
BigDecimal counter = counter();
|
||||
BigDecimal percentage = ONE_HUNDRED_PERCENT.divide(total, MathContext.DECIMAL64).multiply(counter);
|
||||
Duration timePassed = getTimePassed();
|
||||
|
||||
String remainingTimePart = "";
|
||||
@@ -197,8 +320,13 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
String counterPrint = total.compareTo(BigDecimal.TEN) >= 0 ? formatNumber(counter) : counter.toString();
|
||||
System.out.println(counterPrint + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage) + " %) erledigt. "
|
||||
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
|
||||
if (printer != null) {
|
||||
printer.println(counterPrint + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage) + " %) erledigt. "
|
||||
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
|
||||
} else {
|
||||
logger().info("{} Stück von {} ({} %) erledigt. {} vergangen{}.",
|
||||
counterPrint, total, PERCENTAGE_FORMAT.format(percentage), formatDuration(timePassed), remainingTimePart);
|
||||
}
|
||||
updateLastLoggedAt();
|
||||
}
|
||||
|
||||
@@ -209,11 +337,11 @@ public class FabianUtil {
|
||||
|
||||
|
||||
private synchronized void logTimeBased() {
|
||||
while (getCounter().compareTo(total) != 0) {
|
||||
while (counter().compareTo(total) != 0) {
|
||||
logIf();
|
||||
long timeSinceLastLogged = getTimeSinceLastLogged();
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(timeSinceLastLogged - getTimeLogDistance());
|
||||
TimeUnit.SECONDS.sleep(timeSinceLastLogged - timeLogDistance());
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
@@ -224,22 +352,27 @@ public class FabianUtil {
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, String content) {
|
||||
writeTestOutput(filename, ending, strToBytes(content));
|
||||
}
|
||||
|
||||
|
||||
public static byte[] strToBytes(String str) {
|
||||
return str.getBytes(StandardCharsets.UTF_8);
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, byte[] content) {
|
||||
writeToFile(STD_DIR_TEMP + "testOutput/", filename, ending, content);
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, InputStream content) {
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, Reader content) {
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String ending, String content) {
|
||||
writeDump(ending, strToBytes(content));
|
||||
writeToFile(STD_DIR_TEMP, "dump", ending, content);
|
||||
}
|
||||
|
||||
|
||||
@@ -248,8 +381,18 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String ending, InputStream content) {
|
||||
writeToFile(STD_DIR_TEMP, "dump", ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String ending, Reader content) {
|
||||
writeToFile(STD_DIR_TEMP, "dump", ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String filename, String ending, String content) {
|
||||
writeDump(filename, ending, strToBytes(content));
|
||||
writeToFile(STD_DIR_TEMP, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
@@ -258,9 +401,45 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String filename, String ending, InputStream content) {
|
||||
writeToFile(STD_DIR_TEMP, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String filename, String ending, Reader content) {
|
||||
writeToFile(STD_DIR_TEMP, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, String content) {
|
||||
writeToFile(path, filename, ending, new StringReader(content));
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, byte[] content) {
|
||||
try {
|
||||
writeToFile(path, filename, ending, new InputStreamReader(new ByteArrayInputStream(content)));
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, InputStream content) {
|
||||
writeToFile(path, filename, ending, new InputStreamReader(content));
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, Reader content) {
|
||||
Path toFile = Paths.get(path + filename + "." + ending);
|
||||
LOG.fine("Schreibe in Datei {}", toFile);
|
||||
try (content) {
|
||||
BufferedWriter out = Files.newBufferedWriter(toFile);
|
||||
content.transferTo(out);
|
||||
/* varianten: mit streams:
|
||||
Files.copy(content, Paths.get(path + filename + "." + ending), StandardCopyOption.REPLACE_EXISTING);
|
||||
oder selber pipen, wie beim Reader/ writer:
|
||||
OutputStream out = Files.newOutputStream(Paths.get(path + filename + "." + ending));
|
||||
content.transferTo(out);
|
||||
oder mit byte-array:
|
||||
Files.write(Paths.get(path + filename + "." + ending), content);
|
||||
*/
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@@ -12,6 +12,8 @@ import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class Java11 {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
////////
|
||||
// 1. //
|
||||
@@ -159,4 +161,4 @@ interface InterfaceWithPrivateMethods {
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface Nonnull {}
|
||||
@interface Nonnull {}
|
||||
|
257
LoadingBar.java
257
LoadingBar.java
@@ -27,11 +27,13 @@ public class LoadingBar {
|
||||
private static final int MIN_LUNCH_DURATION = 30;
|
||||
private static final LocalTime LATEST_LUNCH_TIME = LocalTime.of(13, 30);
|
||||
private static final int MINS_PER_HOUR = 60;
|
||||
private static final BigDecimal MINS_PER_HOUR_BD = BigDecimal.valueOf(MINS_PER_HOUR);
|
||||
private static final long DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH = 5L * MINS_PER_HOUR;
|
||||
private static final int MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH = 6 * MINS_PER_HOUR;
|
||||
private static final long MAX_NUMBER_WORK_MINS = 8L * MINS_PER_HOUR;
|
||||
private static final int LINE_LENGTH = 100;
|
||||
private static final MathContext MC_INTEGER = new MathContext(1, RoundingMode.HALF_EVEN);
|
||||
private static final BigDecimal ONE_HUNDRED_PERCENT = BigDecimal.valueOf(100);
|
||||
|
||||
|
||||
private enum DaySection {
|
||||
@@ -64,6 +66,17 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private final LocalTime startTime;
|
||||
private LocalTime endTime;
|
||||
private long totalMinutes;
|
||||
private BigDecimal totalMinutesBD;
|
||||
|
||||
|
||||
private LoadingBar(LocalTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args.length == 0) {
|
||||
askParametersAndRun();
|
||||
@@ -76,10 +89,14 @@ public class LoadingBar {
|
||||
private static void askParametersAndRun() throws IOException {
|
||||
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||
print("Ankunftszeit: ");
|
||||
String startTimeRaw = br.readLine();
|
||||
var startTime = LocalTime.parse(startTimeRaw, TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
handleMittagspause(br, startTime);
|
||||
handleZapfenstreich(br, startTime);
|
||||
var startTime = LocalTime.parse(br.readLine(), TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
var lb = new LoadingBar(startTime);
|
||||
if (lb.hasMittagspauseArrived()) {
|
||||
handleMittagspause(br, lb);
|
||||
lb.showLoadingBar();
|
||||
}
|
||||
handleZapfenstreich(br, lb);
|
||||
lb.showLoadingBar();
|
||||
}
|
||||
|
||||
|
||||
@@ -93,38 +110,41 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static void handleMittagspause(BufferedReader br, LocalTime startTime) throws IOException {
|
||||
private static void handleMittagspause(BufferedReader br, LoadingBar lb) throws IOException {
|
||||
print("Mittagspause verschieben um (optional): ");
|
||||
String mittagspauseOffsetRaw = br.readLine();
|
||||
if (mittagspauseOffsetRaw != null && !mittagspauseOffsetRaw.isBlank()) {
|
||||
var mittagspauseOffset = Integer.parseInt(mittagspauseOffsetRaw);
|
||||
showLoadingBarMittagspause(startTime, mittagspauseOffset);
|
||||
lb.initMittagspause(mittagspauseOffset);
|
||||
return;
|
||||
}
|
||||
print("Mittagspause um (optional): ");
|
||||
String manualMittagspauseRaw = br.readLine();
|
||||
if (manualMittagspauseRaw != null && !manualMittagspauseRaw.isBlank()) {
|
||||
var manualMittagspause = LocalTime.parse(manualMittagspauseRaw, TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
showLoadingBarMittagspause(startTime, manualMittagspause);
|
||||
lb.initMittagspause(manualMittagspause);
|
||||
} else {
|
||||
showLoadingBarMittagspause(startTime);
|
||||
lb.initMittagspause();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void handleZapfenstreich(BufferedReader br, LocalTime startTime) throws IOException {
|
||||
private static void handleZapfenstreich(BufferedReader br, LoadingBar lb) throws IOException {
|
||||
print("Mittagspause hat gedauert (optional): ");
|
||||
String mittagspauseDurationRaw = br.readLine();
|
||||
Integer mittagspauseDuration = null;
|
||||
if (mittagspauseDurationRaw != null && !mittagspauseDurationRaw.isBlank()) {
|
||||
mittagspauseDuration = Integer.valueOf(mittagspauseDurationRaw);
|
||||
}
|
||||
LocalTime vorlaeufigeEndzeit = lb.startTime.plusMinutes(MAX_NUMBER_WORK_MINS)
|
||||
.plusMinutes(mittagspauseDuration != null ? mittagspauseDuration : MIN_LUNCH_DURATION);
|
||||
println("Endzeit: " + TIME_FORMATTER.format(vorlaeufigeEndzeit));
|
||||
print("Feierabend verschieben um (optional): ");
|
||||
String zapfenstreichOffsetRaw = br.readLine();
|
||||
Integer zapfenstreichOffset = null;
|
||||
if (zapfenstreichOffsetRaw != null && !zapfenstreichOffsetRaw.isBlank()) {
|
||||
zapfenstreichOffset = Integer.valueOf(zapfenstreichOffsetRaw);
|
||||
showLoadingBarZapfenstreich(startTime, mittagspauseDuration, zapfenstreichOffset);
|
||||
lb.initZapfenstreich(mittagspauseDuration, zapfenstreichOffset);
|
||||
return;
|
||||
}
|
||||
print("Manuelle Uhrzeit Feierabend (optional): ");
|
||||
@@ -132,18 +152,24 @@ public class LoadingBar {
|
||||
LocalTime manualZapfenstreich = null;
|
||||
if (manualZapfenstreichRaw != null && !manualZapfenstreichRaw.isBlank()) {
|
||||
manualZapfenstreich = LocalTime.parse(manualZapfenstreichRaw, TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
showLoadingBarZapfenstreich(startTime, mittagspauseDuration, manualZapfenstreich);
|
||||
return;
|
||||
lb.initZapfenstreich(mittagspauseDuration, manualZapfenstreich);
|
||||
} else {
|
||||
lb.initZapfenstreich(mittagspauseDuration);
|
||||
}
|
||||
showLoadingBarZapfenstreich(startTime, mittagspauseDuration);
|
||||
}
|
||||
|
||||
|
||||
private static void parseParametersAndRun(String[] args) {
|
||||
LoadingBar lb = getLoadingBarFromCLI(args);
|
||||
lb.showLoadingBar();
|
||||
}
|
||||
|
||||
|
||||
private static LoadingBar getLoadingBarFromCLI(String[] args) {
|
||||
String nextArg = args[0];
|
||||
if ("--help".equals(nextArg)) {
|
||||
printHelp();
|
||||
return;
|
||||
System.exit(1);
|
||||
}
|
||||
verifyMinimumNumberOfArgs(args);
|
||||
verifyTimeFormat(nextArg, "Erstes Argument");
|
||||
@@ -151,34 +177,33 @@ public class LoadingBar {
|
||||
nextArg = args[1];
|
||||
var section = DaySection.byParam(nextArg);
|
||||
verifyDaySection(section, nextArg);
|
||||
if (section == DaySection.MITTAG) {
|
||||
handleMittagspause(args, startTime);
|
||||
} else {
|
||||
handleZapfenstreich(args, startTime);
|
||||
}
|
||||
return section == DaySection.MITTAG ? getLoadingBarMittagspause(args, startTime) : getLoadingBarZapfenstreich(args, startTime);
|
||||
}
|
||||
|
||||
|
||||
private static void handleMittagspause(String[] args, LocalTime startTime) {
|
||||
private static LoadingBar getLoadingBarMittagspause(String[] args, LocalTime startTime) {
|
||||
var lb = new LoadingBar(startTime);
|
||||
if (args.length == 2) {
|
||||
showLoadingBarMittagspause(startTime);
|
||||
return;
|
||||
lb.initMittagspause();
|
||||
return lb;
|
||||
}
|
||||
String nextArg = args[2];
|
||||
if (OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
showLoadingBarMittagspause(startTime, Integer.parseInt(nextArg));
|
||||
return;
|
||||
lb.initMittagspause(Integer.parseInt(nextArg));
|
||||
return lb;
|
||||
}
|
||||
verifyTimeFormat(nextArg, "Argument nach " + DaySection.MITTAG.getParam());
|
||||
var manualMittagspause = LocalTime.parse(nextArg, TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
showLoadingBarMittagspause(startTime, manualMittagspause);
|
||||
lb.initMittagspause(manualMittagspause);
|
||||
return lb;
|
||||
}
|
||||
|
||||
|
||||
private static void handleZapfenstreich(String[] args, LocalTime startTime) {
|
||||
private static LoadingBar getLoadingBarZapfenstreich(String[] args, LocalTime startTime) {
|
||||
var lb = new LoadingBar(startTime);
|
||||
if (args.length == 2) {
|
||||
showLoadingBarZapfenstreich(startTime);
|
||||
return;
|
||||
lb.initZapfenstreich();
|
||||
return lb;
|
||||
}
|
||||
String nextArg = args[2];
|
||||
LocalTime maxZapfenstreich = null;
|
||||
@@ -189,18 +214,18 @@ public class LoadingBar {
|
||||
} else if (OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
endTimeOffset = Integer.parseInt(nextArg);
|
||||
} else {
|
||||
verifyDurationFormat(nextArg, "Argument nach " + DaySection.ZAPFENSTREICH.getParam(), true); // FSFIXME erweitere Fehlermeldung
|
||||
verifyDurationFormat(nextArg, "Argument nach " + DaySection.ZAPFENSTREICH.getParam());
|
||||
lunchDuration = Integer.parseInt(nextArg);
|
||||
}
|
||||
if (args.length == 3) {
|
||||
if (maxZapfenstreich == null && endTimeOffset == 0) {
|
||||
showLoadingBarZapfenstreich(startTime, lunchDuration);
|
||||
lb.initZapfenstreich(lunchDuration);
|
||||
} else if (maxZapfenstreich == null) {
|
||||
showLoadingBarZapfenstreich(startTime, lunchDuration, endTimeOffset);
|
||||
lb.initZapfenstreich(lunchDuration, endTimeOffset);
|
||||
} else {
|
||||
showLoadingBarZapfenstreich(startTime, lunchDuration, maxZapfenstreich);
|
||||
lb.initZapfenstreich(lunchDuration, maxZapfenstreich);
|
||||
}
|
||||
return;
|
||||
return lb;
|
||||
}
|
||||
nextArg = args[3];
|
||||
if (lunchDuration == null) {
|
||||
@@ -210,12 +235,13 @@ public class LoadingBar {
|
||||
if (maxZapfenstreich == null && !OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
verifyTimeFormat(nextArg, "Letztes Argument nach " + DaySection.ZAPFENSTREICH.getParam() + " und Mittagspausendauer");
|
||||
maxZapfenstreich = LocalTime.parse(nextArg, TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
showLoadingBarZapfenstreich(startTime, lunchDuration, maxZapfenstreich);
|
||||
return;
|
||||
lb.initZapfenstreich(lunchDuration, maxZapfenstreich);
|
||||
return lb;
|
||||
}
|
||||
verifyOffsetFormat(nextArg, "Letztes Argument nach " + DaySection.ZAPFENSTREICH.getParam() + " und Enduhrzeit");
|
||||
endTimeOffset = Integer.parseInt(nextArg);
|
||||
showLoadingBarZapfenstreich(startTime, lunchDuration, endTimeOffset);
|
||||
lb.initZapfenstreich(lunchDuration, endTimeOffset);
|
||||
return lb;
|
||||
}
|
||||
|
||||
|
||||
@@ -230,26 +256,25 @@ public class LoadingBar {
|
||||
|
||||
|
||||
private static void verifyTimeFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(TIME_PATTERN, param, errMsgPrefix, true, false);
|
||||
verifyInputFormat(TIME_PATTERN, param, errMsgPrefix, "Uhrzeitformat (" + TIME_FORMAT + ")", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyDurationFormat(String param, String errMsgPrefix, boolean timeInputPossible) {
|
||||
verifyInputFormat(LUNCH_DURATION_PATTERN, param, errMsgPrefix, false, timeInputPossible);
|
||||
private static void verifyDurationFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(LUNCH_DURATION_PATTERN, param, errMsgPrefix, "Minutenanzahl (ganze Zahl)", true);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyOffsetFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(OFFSET_PATTERN, param, errMsgPrefix, false, false);
|
||||
verifyInputFormat(OFFSET_PATTERN, param, errMsgPrefix, "Minutendifferenz (ganze Zahl mit Vorzeichen)", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyInputFormat(Pattern pattern, String param, String errMsgPrefix, boolean timeInput, boolean timeInputPossible) {
|
||||
private static void verifyInputFormat(Pattern pattern, String param, String errMsgPrefix, String firstInputPart, boolean timeInputPossible) {
|
||||
if (pattern.matcher(param).matches()) {
|
||||
return;
|
||||
} // FSFIXME fine tune message -> HH:mm, mm, -+mm
|
||||
var firstInputPart = timeInput ? "Uhrzeitformat (" + TIME_FORMAT + ")" : "Minutenanzahl (" + LUNCH_DURATION_PATTERN + ")";
|
||||
var possibleTimeInputPart = !timeInput && timeInputPossible ? " oder Uhrzeitformat (" + TIME_FORMAT + ")" : "";
|
||||
}
|
||||
var possibleTimeInputPart = timeInputPossible ? " oder Uhrzeitformat (" + TIME_FORMAT + ")" : "";
|
||||
println(errMsgPrefix + " \"" + param + "\" muss " + firstInputPart + possibleTimeInputPart + " entsprechen.");
|
||||
System.exit(1);
|
||||
}
|
||||
@@ -298,75 +323,97 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarMittagspause(LocalTime startTime) {
|
||||
showLoadingBarMittagspause(startTime, null);
|
||||
protected long getPassedMinutes() {
|
||||
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarMittagspause(LocalTime startTime, int endTimeOffset) {
|
||||
LocalTime endTime = startTime.plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH + endTimeOffset);
|
||||
LocalTime trueEndTime = endTime.isAfter(LATEST_LUNCH_TIME) ? LATEST_LUNCH_TIME : endTime;
|
||||
showLoadingBar(startTime, trueEndTime);
|
||||
private void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarMittagspause(LocalTime startTime, LocalTime manualEndTime) {
|
||||
LocalTime endTime = manualEndTime != null ? manualEndTime : startTime.plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH);
|
||||
LocalTime trueEndTime = endTime.isAfter(LATEST_LUNCH_TIME) ? LATEST_LUNCH_TIME : endTime;
|
||||
showLoadingBar(startTime, trueEndTime);
|
||||
|
||||
private boolean hasMittagspauseArrived() {
|
||||
return startTime.until(LocalTime.now(), ChronoUnit.MINUTES) < DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH;
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime) {
|
||||
showLoadingBarZapfenstreich(startTime, -1, 0);
|
||||
private void initMittagspause() {
|
||||
LocalTime defaultEndTime = startTime.plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH);
|
||||
realInitMittagspause(defaultEndTime);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime, Integer manualLunchDuration) {
|
||||
showLoadingBarZapfenstreich(startTime, manualLunchDuration, 0);
|
||||
private void initMittagspause(int endTimeOffset) {
|
||||
LocalTime offsetEndTime = startTime.plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH + endTimeOffset);
|
||||
realInitMittagspause(offsetEndTime);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime, Integer manualLunchDuration, int endTimeOffset) {
|
||||
private void initMittagspause(LocalTime manualEndTime) {
|
||||
LocalTime effectiveEndTime = manualEndTime != null ? manualEndTime : startTime.plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH);
|
||||
realInitMittagspause(effectiveEndTime);
|
||||
}
|
||||
|
||||
|
||||
private void realInitMittagspause(LocalTime theoreticalEndTime) {
|
||||
setEndTime(theoreticalEndTime.isAfter(LATEST_LUNCH_TIME) ? LATEST_LUNCH_TIME : theoreticalEndTime);
|
||||
}
|
||||
|
||||
|
||||
private void initZapfenstreich() {
|
||||
LocalTime trueEndTime = startTime.plusMinutes(MAX_NUMBER_WORK_MINS + MIN_LUNCH_DURATION);
|
||||
realInitZapfenstreich(MIN_LUNCH_DURATION, trueEndTime);
|
||||
}
|
||||
|
||||
|
||||
private void initZapfenstreich(Integer manualLunchDuration) {
|
||||
initZapfenstreich(manualLunchDuration, 0);
|
||||
}
|
||||
|
||||
|
||||
private void initZapfenstreich(Integer manualLunchDuration, int endTimeOffset) {
|
||||
long minLunchDuration = getMinLunchDuration(endTimeOffset);
|
||||
long realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
LocalTime trueEndTime = startTime.plusMinutes(MAX_NUMBER_WORK_MINS + realLunchDuration + endTimeOffset);
|
||||
realShowLoadingBarZapfenstreich(startTime, realLunchDuration, trueEndTime);
|
||||
realInitZapfenstreich(realLunchDuration, trueEndTime);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime, Integer manualLunchDuration, LocalTime manualEndTime) {
|
||||
private void initZapfenstreich(Integer manualLunchDuration, LocalTime manualEndTime) {
|
||||
LocalTime trueEndTime = manualEndTime;
|
||||
long minLunchDuration = getMinLunchDuration(startTime, trueEndTime);
|
||||
long minLunchDuration = getMinLunchDuration(trueEndTime);
|
||||
long realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
if (trueEndTime == null) {
|
||||
trueEndTime = startTime.plusMinutes(MAX_NUMBER_WORK_MINS + realLunchDuration);
|
||||
}
|
||||
realShowLoadingBarZapfenstreich(startTime, realLunchDuration, trueEndTime);
|
||||
realInitZapfenstreich(realLunchDuration, trueEndTime);
|
||||
}
|
||||
|
||||
|
||||
private static long getMinLunchDuration(int endTimeOffset) {
|
||||
private long getMinLunchDuration(int endTimeOffset) {
|
||||
if (endTimeOffset == 0) {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
long totalDuration = MAX_NUMBER_WORK_MINS + endTimeOffset;
|
||||
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
return getMinLunchDuration(totalDuration);
|
||||
}
|
||||
|
||||
|
||||
private static long getMinLunchDuration(LocalTime startTime, LocalTime endTime) {
|
||||
if (endTime == null) {
|
||||
private long getMinLunchDuration(LocalTime manualEndTime) {
|
||||
if (manualEndTime == null) {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
long totalDuration = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
int effectiveLunchDuration = ((int) totalDuration) - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
long totalDuration = startTime.until(manualEndTime, ChronoUnit.MINUTES);
|
||||
return getMinLunchDuration(totalDuration);
|
||||
}
|
||||
|
||||
|
||||
private static long getMinLunchDuration(long effectiveLunchDuration) {
|
||||
private long getMinLunchDuration(long precalculatedTotalDuration) {
|
||||
long effectiveLunchDuration = precalculatedTotalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
if (effectiveLunchDuration < 0) {
|
||||
effectiveLunchDuration = 0;
|
||||
}
|
||||
@@ -374,52 +421,61 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static long getRealLunchDuration(Integer manualLunchDuration, long minLunchDuration) {
|
||||
private long getRealLunchDuration(Integer manualLunchDuration, long minLunchDuration) {
|
||||
return manualLunchDuration != null && manualLunchDuration >= minLunchDuration ? manualLunchDuration : minLunchDuration;
|
||||
}
|
||||
|
||||
|
||||
private static void realShowLoadingBarZapfenstreich(LocalTime startTime, long manualLunchDuration, LocalTime endTime) {
|
||||
if (manualLunchDuration > 0) {
|
||||
var totalWorkTime = LocalTime.MIDNIGHT.plusMinutes(startTime.until(endTime, ChronoUnit.MINUTES) - manualLunchDuration);
|
||||
private void realInitZapfenstreich(long effectiveLunchDuration, LocalTime effectiveEndTime) {
|
||||
if (effectiveLunchDuration > 0) {
|
||||
var totalWorkTime = LocalTime.MIDNIGHT.plusMinutes(startTime.until(effectiveEndTime, ChronoUnit.MINUTES) - effectiveLunchDuration);
|
||||
print("Arbeitszeit: " + TIME_FORMATTER.format(totalWorkTime) + "; ");
|
||||
}
|
||||
showLoadingBar(startTime, endTime);
|
||||
setEndTime(effectiveEndTime);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBar(LocalTime startTime, LocalTime endTime) {
|
||||
long totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
private void showLoadingBar() {
|
||||
long passedMinutes = getPassedMinutes();
|
||||
// long passedMinutes = 0; // DEBUG
|
||||
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(minutesToTimeString(totalMinutes) + " gesamt; Endzeit: " + TIME_FORMATTER.format(endTime));
|
||||
println(minutesToTimeString(totalMinutesBD) + " gesamt; Endzeit: " + TIME_FORMATTER.format(endTime));
|
||||
while (passedMinutes < totalMinutes) {
|
||||
print(fillLoadingBar(totalMinutes, passedMinutes, true));
|
||||
try {
|
||||
var now = LocalTime.now();
|
||||
var oneMinuteLater = now.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
|
||||
// +1 second to adjust for ignored milliseconds as it is better to switch between 00 and 01 as between 59 and 00
|
||||
TimeUnit.SECONDS.sleep(now.until(oneMinuteLater, ChronoUnit.SECONDS) + 1);
|
||||
// TimeUnit.SECONDS.sleep(1L); // DEBUG
|
||||
} catch (InterruptedException ie) {
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
print(fillLoadingBar(passedMinutes, true));
|
||||
waitUntilNextMinute();
|
||||
passedMinutes++;
|
||||
}
|
||||
println(fillLoadingBar(totalMinutes, passedMinutes, false));
|
||||
println(fillLoadingBar(passedMinutes, false));
|
||||
}
|
||||
|
||||
|
||||
private static String fillLoadingBar(long totalMinutes, long passedMinutes, boolean progressive) {
|
||||
var nonNegativePassedMinutes = passedMinutes < 0 ? 0 : passedMinutes;
|
||||
BigDecimal wholePercentage = BigDecimal.valueOf(100)
|
||||
.multiply( // kind of reverse dreisatz to avoid to have e.g. 99.9999 instead of 100 %
|
||||
BigDecimal.valueOf(nonNegativePassedMinutes).divide(BigDecimal.valueOf(totalMinutes), MathContext.DECIMAL64));
|
||||
private void waitUntilNextMinute() {
|
||||
try {
|
||||
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);
|
||||
// TimeUnit.MILLISECONDS.sleep(100L); // DEBUG
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String fillLoadingBar(long passedMinutes, boolean progressive) {
|
||||
var nonNegativePassedMinutes = BigDecimal.valueOf(passedMinutes < 0 ? 0 : passedMinutes);
|
||||
BigDecimal wholePercentage = ONE_HUNDRED_PERCENT
|
||||
// kind of reverse dreisatz to avoid having e.g. 99.9999 instead of 100 %
|
||||
.multiply(nonNegativePassedMinutes)
|
||||
.divide(totalMinutesBD, MathContext.DECIMAL64);
|
||||
int numberOfEquals = wholePercentage.intValue();
|
||||
var sb = new StringBuilder("[");
|
||||
for (int i = 0; i < LINE_LENGTH; i++) {
|
||||
@@ -429,7 +485,7 @@ public class LoadingBar {
|
||||
sb.append("-");
|
||||
}
|
||||
}
|
||||
long remainingMinutes = totalMinutes - nonNegativePassedMinutes;
|
||||
BigDecimal remainingMinutes = totalMinutesBD.subtract(nonNegativePassedMinutes);
|
||||
sb.append("] ").append(PERCENTAGE_FORMAT.format(wholePercentage)).append("% ")
|
||||
.append(minutesToTimeString(nonNegativePassedMinutes)).append(" - ").append(minutesToTimeString(remainingMinutes));
|
||||
if (progressive) {
|
||||
@@ -439,9 +495,8 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static String minutesToTimeString(long minutes) {
|
||||
var minutesBD = BigDecimal.valueOf(minutes);
|
||||
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(BigDecimal.valueOf(MINS_PER_HOUR), MC_INTEGER);
|
||||
private String minutesToTimeString(BigDecimal minutes) {
|
||||
BigDecimal[] hoursAndMinutes = minutes.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
|
||||
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
@@ -16,8 +16,23 @@ public class SimpleLoadingBar {
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile("(?>[01]\\d|2[0-4]):[0-5]\\d");
|
||||
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
||||
private static final int MINS_PER_HOUR = 60;
|
||||
private static final BigDecimal MINS_PER_HOUR_BD = BigDecimal.valueOf(MINS_PER_HOUR);
|
||||
private static final int LINE_LENGTH = 100;
|
||||
private static final MathContext MC_INTEGER = new MathContext(1, RoundingMode.HALF_EVEN);
|
||||
private static final BigDecimal ONE_HUNDRED_PERCENT = BigDecimal.valueOf(100);
|
||||
|
||||
private final String title;
|
||||
private final LocalTime startTime;
|
||||
private LocalTime endTime;
|
||||
private long totalMinutes;
|
||||
private BigDecimal totalMinutesBD;
|
||||
|
||||
|
||||
private SimpleLoadingBar(LocalTime startTime, LocalTime endTime, String title) {
|
||||
this.startTime = startTime;
|
||||
setEndTime(endTime);
|
||||
this.title = initTitle(title);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -27,13 +42,12 @@ public class SimpleLoadingBar {
|
||||
}
|
||||
verifyMinimumNumberOfArgs(args);
|
||||
String nextArg = args[0];
|
||||
verifyTimeFormat(nextArg, "Erstes Argument"); // FSFIXME package
|
||||
var firstTime = LocalTime.parse(nextArg, TIME_FORMATTER); // FSFIXME package
|
||||
verifyTimeFormat(nextArg, "Erstes Argument");
|
||||
var firstTime = LocalTime.parse(nextArg, TIME_FORMATTER);
|
||||
LocalTime startTime = null;
|
||||
String title = "";
|
||||
LocalTime endTime = null;
|
||||
if (args.length > 1) {
|
||||
startTime = firstTime;
|
||||
nextArg = args[1];
|
||||
if ("-msg".equals(nextArg)) {
|
||||
title = args.length > 2 ? args[2] : title;
|
||||
@@ -45,23 +59,32 @@ public class SimpleLoadingBar {
|
||||
if (endTime == null) {
|
||||
startTime = LocalTime.now();
|
||||
endTime = firstTime;
|
||||
} else {
|
||||
startTime = firstTime;
|
||||
}
|
||||
String fallbackTitle = "Ende! Endzeit " + TIME_FORMATTER.format(endTime) + " erreicht.";
|
||||
if (args.length == 2 || !title.isBlank()) {
|
||||
title = title.isBlank() ? fallbackTitle : title;
|
||||
showLoadingBar(startTime, endTime, title);
|
||||
new SimpleLoadingBar(startTime, endTime, title).showLoadingBar();
|
||||
return;
|
||||
}
|
||||
// if there are 3 arguments, the third will be discarded.
|
||||
boolean hasTitleArg = args.length > 3 && "-msg".equals(args[2]);
|
||||
title = hasTitleArg ? args[3] : title;
|
||||
title = title.isBlank() ? fallbackTitle : title;
|
||||
showLoadingBar(startTime, endTime, title);
|
||||
new SimpleLoadingBar(startTime, endTime, title).showLoadingBar();
|
||||
}
|
||||
|
||||
|
||||
private static void println(Object o) {
|
||||
System.out.println(o);
|
||||
}
|
||||
|
||||
|
||||
private static void print(Object o) {
|
||||
System.out.print(o);
|
||||
}
|
||||
|
||||
|
||||
private static void printHelp() {
|
||||
System.out.println("Mögliche Argumente für LoadingBar:\n"
|
||||
println("Mögliche Argumente für LoadingBar:\n"
|
||||
+ "Startzeit, Endzeit, Endnachricht (Optional)\n"
|
||||
+ TIME_FORMAT + " " + TIME_FORMAT + " -msg <Nachricht>\n"
|
||||
+ "Endzeit (Startzeit = jetzt), Endnachricht (Optional)\n"
|
||||
@@ -74,7 +97,7 @@ public class SimpleLoadingBar {
|
||||
if (args.length >= 1) {
|
||||
return;
|
||||
}
|
||||
System.out.println("Mindestens 1 Argument muss gegeben sein.");
|
||||
println("Mindestens 1 Argument muss gegeben sein.");
|
||||
printHelp();
|
||||
System.exit(1);
|
||||
}
|
||||
@@ -84,44 +107,57 @@ public class SimpleLoadingBar {
|
||||
if (TIME_PATTERN.matcher(param).matches()) {
|
||||
return;
|
||||
}
|
||||
System.out.println(errMsgPrefix + " \"" + param + "\" muss Uhrzeitformat (" + TIME_FORMAT + ") entsprechen.");
|
||||
println(errMsgPrefix + " \"" + param + "\" muss Uhrzeitformat (" + TIME_FORMAT + ") entsprechen.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
public static void showLoadingBar(LocalTime startTime, LocalTime endTime, String title) {
|
||||
long initialMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
System.out.print(minutesToTimeString(initialMinutes) + " gesamt; Endzeit: " + TIME_FORMATTER.format(endTime) + "\n");
|
||||
long passedMinutes = startTime.until(LocalTime.now(), ChronoUnit.MINUTES);
|
||||
if (passedMinutes > initialMinutes) {
|
||||
passedMinutes = initialMinutes;
|
||||
} else if (passedMinutes < 0) {
|
||||
System.out.println(fillLoadingBar(initialMinutes, 0, false));
|
||||
return;
|
||||
}
|
||||
while (passedMinutes < initialMinutes) {
|
||||
System.out.print(fillLoadingBar(initialMinutes, passedMinutes, true));
|
||||
try {
|
||||
var now = LocalTime.now();
|
||||
var oneMinuteLater = now.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
|
||||
// +1 second to adjust for ignored milliseconds as it is better to switch between 00 and 01 as between 59 and 00
|
||||
TimeUnit.SECONDS.sleep(now.until(oneMinuteLater, ChronoUnit.SECONDS) + 1);
|
||||
// TimeUnit.SECONDS.sleep(1L); // DEBUG
|
||||
} catch (InterruptedException ie) {
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
passedMinutes++;
|
||||
}
|
||||
System.out.println(fillLoadingBar(initialMinutes, passedMinutes, false));
|
||||
System.out.println(formatTitle(title));
|
||||
protected long getPassedMinutes() {
|
||||
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
private static String fillLoadingBar(long initialMinutes, long passedMinutes, boolean progressive) {
|
||||
BigDecimal wholePercentage = BigDecimal.valueOf(100)
|
||||
.multiply(BigDecimal.valueOf(passedMinutes) // kind of reverse dreisatz to avoid to have e.g. 99.9999 instead of 100 %
|
||||
.divide(BigDecimal.valueOf(initialMinutes), MathContext.DECIMAL64));
|
||||
long remainingMinutes = initialMinutes - passedMinutes;
|
||||
private void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
}
|
||||
|
||||
|
||||
private String initTitle(String inputTitle) {
|
||||
String fallbackTitle = "Ende! Endzeit " + TIME_FORMATTER.format(endTime) + " erreicht.";
|
||||
String effectiveTitle = inputTitle == null || inputTitle.isBlank() ? fallbackTitle : inputTitle;
|
||||
String separator = "*".repeat(effectiveTitle.length());
|
||||
return separator + "\n" + effectiveTitle + "\n" + separator;
|
||||
}
|
||||
|
||||
|
||||
private void showLoadingBar() {
|
||||
long passedMinutes = getPassedMinutes();
|
||||
// long passedMinutes = 0; // DEBUG
|
||||
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(minutesToTimeString(totalMinutesBD) + " gesamt; Endzeit: " + TIME_FORMATTER.format(endTime));
|
||||
while (passedMinutes < totalMinutes) {
|
||||
print(fillLoadingBar(passedMinutes, true));
|
||||
waitUntilNextMinute();
|
||||
passedMinutes++;
|
||||
}
|
||||
println(fillLoadingBar(passedMinutes, false));
|
||||
println(title);
|
||||
}
|
||||
|
||||
|
||||
private String fillLoadingBar(long passedMinutes, boolean progressive) {
|
||||
var nonNegativePassedMinutes = BigDecimal.valueOf(passedMinutes < 0 ? 0 : passedMinutes);
|
||||
BigDecimal wholePercentage = ONE_HUNDRED_PERCENT
|
||||
// kind of reverse dreisatz to avoid having e.g. 99.9999 instead of 100 %
|
||||
.multiply(nonNegativePassedMinutes)
|
||||
.divide(totalMinutesBD, MathContext.DECIMAL64);
|
||||
int numberOfEquals = wholePercentage.intValue();
|
||||
var sb = new StringBuilder("[");
|
||||
for (int i = 0; i < LINE_LENGTH; i++) {
|
||||
@@ -131,8 +167,9 @@ public class SimpleLoadingBar {
|
||||
sb.append("-");
|
||||
}
|
||||
}
|
||||
BigDecimal remainingMinutes = totalMinutesBD.subtract(nonNegativePassedMinutes);
|
||||
sb.append("] ").append(PERCENTAGE_FORMAT.format(wholePercentage)).append("% ")
|
||||
.append(minutesToTimeString(passedMinutes)).append(" - ").append(minutesToTimeString(remainingMinutes));
|
||||
.append(minutesToTimeString(nonNegativePassedMinutes)).append(" - ").append(minutesToTimeString(remainingMinutes));
|
||||
if (progressive) {
|
||||
sb.append("\r");
|
||||
}
|
||||
@@ -140,15 +177,24 @@ public class SimpleLoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static String minutesToTimeString(long minutes) {
|
||||
var minutesBD = BigDecimal.valueOf(minutes);
|
||||
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(BigDecimal.valueOf(MINS_PER_HOUR), MC_INTEGER);
|
||||
private String minutesToTimeString(BigDecimal minutes) {
|
||||
BigDecimal[] hoursAndMinutes = minutes.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
|
||||
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
|
||||
}
|
||||
|
||||
|
||||
private static String formatTitle(String title) {
|
||||
String separator = "*".repeat(title.length());
|
||||
return separator + "\n" + title + "\n" + separator;
|
||||
private void waitUntilNextMinute() {
|
||||
try {
|
||||
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);
|
||||
// TimeUnit.MILLISECONDS.sleep(100L); // DEBUG
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(ie);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user