2024-08-05 14:48:14 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
|
|
|
|
public class FabianUtil {
|
|
|
|
|
2024-11-06 10:09:36 +01:00
|
|
|
private static final String STD_DIR_TEMP = "/FIXME/temp/";
|
|
|
|
public static final String ENDING_TXT = "txt";
|
|
|
|
public static final String ENDING_JSON = "json";
|
|
|
|
public static final String ENDING_HTML = "html";
|
|
|
|
public static final String ENDING_PDF = "pdf";
|
|
|
|
public static final String ENDING_TIFF = "tiff";
|
2024-08-05 14:48:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
private FabianUtil() {}
|
|
|
|
|
|
|
|
|
|
|
|
public static class ProgressHandler {
|
|
|
|
|
|
|
|
private static final DecimalFormat TIME_DECIMAL_FORMAT = new DecimalFormat("00");
|
2024-11-06 09:58:52 +01:00
|
|
|
protected static final int DEFAULT_LOG_DISTANCE = 50;
|
|
|
|
protected static final int DEFAULT_TIME_LOG_DISTANCE = 15;
|
2024-08-05 14:48:14 +02:00
|
|
|
|
|
|
|
private final ZonedDateTime startTime;
|
|
|
|
private final BigDecimal logDistance;
|
2024-11-06 09:58:52 +01:00
|
|
|
private final int timeLogDistance;
|
2024-08-05 14:48:14 +02:00
|
|
|
private BigDecimal counter = BigDecimal.ZERO;
|
2024-11-06 09:58:52 +01:00
|
|
|
private ZonedDateTime lastLoggedAt;
|
|
|
|
|
|
|
|
|
|
|
|
public ProgressHandler() {
|
|
|
|
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE);
|
|
|
|
}
|
2024-08-05 14:48:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
public ProgressHandler(int logDistance) {
|
2024-11-06 09:58:52 +01:00
|
|
|
this(logDistance, DEFAULT_TIME_LOG_DISTANCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ProgressHandler(int logDistance, int timeLogDistance) {
|
2024-08-05 14:48:14 +02:00
|
|
|
startTime = ZonedDateTime.now();
|
2024-11-06 09:58:52 +01:00
|
|
|
this.logDistance = validateBD(logDistance, "Log-Distanz");
|
|
|
|
this.timeLogDistance = validateInt(timeLogDistance, "Zeitbasierte Log-Distanz");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected BigDecimal validateBD(int bd, String name) {
|
|
|
|
return BigDecimal.valueOf(validateInt(bd, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected int validateInt(int n, String name) {
|
|
|
|
if (n < 1) {
|
|
|
|
throw new RuntimeException(name + " darf nicht 0 oder negativ sein.");
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
2024-11-06 09:58:52 +01:00
|
|
|
return n;
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected BigDecimal getCounter() {
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void tickAndLog() {
|
|
|
|
tick();
|
|
|
|
logIf();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void tick() {
|
|
|
|
counter = counter.add(BigDecimal.ONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void logAndTick() {
|
|
|
|
logIf();
|
|
|
|
tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void logIf() {
|
2024-11-06 10:00:35 +01:00
|
|
|
if (counter.remainder(logDistance).signum() == 0 || isBored()) {
|
2024-08-05 14:48:14 +02:00
|
|
|
log();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-06 10:00:35 +01:00
|
|
|
private boolean isBored() {
|
|
|
|
ZonedDateTime time = lastLoggedAt == null ? startTime : lastLoggedAt;
|
|
|
|
return time.until(ZonedDateTime.now(), ChronoUnit.SECONDS) >= timeLogDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-05 14:48:14 +02:00
|
|
|
public void log() {
|
|
|
|
Duration timePassed = getTimePassed();
|
|
|
|
System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
|
2024-11-06 10:00:35 +01:00
|
|
|
updateLastLoggedAt();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void updateLastLoggedAt() {
|
|
|
|
lastLoggedAt = ZonedDateTime.now();
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected Duration getTimePassed() {
|
|
|
|
return Duration.ofSeconds(startTime.until(ZonedDateTime.now(), ChronoUnit.SECONDS));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected String formatDuration(Duration dur) {
|
2024-11-06 09:58:52 +01:00
|
|
|
return formatNumber(dur.toHours()) + ":" + formatNumber(dur.toMinutesPart()) + ":" + formatNumber(dur.toSecondsPart());
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-06 09:58:52 +01:00
|
|
|
protected String formatNumber(Number n) {
|
|
|
|
return TIME_DECIMAL_FORMAT.format(n == null ? 0 : n);
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-06 09:58:52 +01:00
|
|
|
public void logFinal() {
|
|
|
|
Duration timePassed = getTimePassed();
|
|
|
|
System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class ProgressHandlerWithTotal extends ProgressHandler {
|
|
|
|
|
|
|
|
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
|
|
|
|
|
|
|
private final BigDecimal total;
|
|
|
|
|
|
|
|
|
2024-11-06 09:58:52 +01:00
|
|
|
public ProgressHandlerWithTotal(int total) {
|
|
|
|
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, total);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-05 14:48:14 +02:00
|
|
|
public ProgressHandlerWithTotal(int logDistance, int total) {
|
2024-11-06 09:58:52 +01:00
|
|
|
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, total);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total) {
|
|
|
|
super(logDistance, timeLogDistance);
|
|
|
|
this.total = validateBD(total, "Gesamt-Anzahl");
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void log() {
|
|
|
|
BigDecimal counter = getCounter();
|
|
|
|
BigDecimal percentage = BigDecimal.valueOf(100).divide(total, 15, RoundingMode.HALF_UP).multiply(counter);
|
|
|
|
|
|
|
|
Duration timePassed = getTimePassed();
|
|
|
|
|
|
|
|
String remainingTimePart = "";
|
|
|
|
if (counter.signum() != 0) {
|
|
|
|
BigDecimal secondsPassed = BigDecimal.valueOf(timePassed.getSeconds());
|
|
|
|
BigDecimal totalSeconds = secondsPassed.divide(counter, 15, RoundingMode.HALF_UP).multiply(total);
|
|
|
|
BigDecimal secondsToGo = totalSeconds.subtract(secondsPassed);
|
|
|
|
Duration timeToGo = Duration.of(secondsToGo.longValue(), ChronoUnit.SECONDS);
|
|
|
|
remainingTimePart = ", " + formatDuration(timeToGo) + " verbleibend";
|
|
|
|
}
|
|
|
|
|
2024-11-06 09:58:52 +01:00
|
|
|
String counterPrint = total.compareTo(BigDecimal.TEN) >= 0 ? formatNumber(counter) : counter.toString();
|
|
|
|
System.out.println(counterPrint + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage) + " %) erledigt. "
|
2024-08-05 14:48:14 +02:00
|
|
|
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
|
2024-11-06 10:00:35 +01:00
|
|
|
updateLastLoggedAt();
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void writeTestOutput(String filename, String ending, byte[] content) {
|
2024-11-06 10:09:36 +01:00
|
|
|
writeToFile(STD_DIR_TEMP + "testOutput/", filename, ending, content);
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void writeDump(String ending, String content) {
|
|
|
|
writeDump(ending, strToBytes(content));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void writeDump(String ending, byte[] content) {
|
2024-11-06 10:09:36 +01:00
|
|
|
writeToFile(STD_DIR_TEMP, "dump", ending, content);
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void writeDump(String filename, String ending, String content) {
|
|
|
|
writeDump(filename, ending, strToBytes(content));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void writeDump(String filename, String ending, byte[] content) {
|
2024-11-06 10:09:36 +01:00
|
|
|
writeToFile(STD_DIR_TEMP, filename, ending, content);
|
2024-08-05 14:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void writeToFile(String path, String filename, String ending, byte[] content) {
|
|
|
|
try {
|
|
|
|
Files.write(Paths.get(path + filename + "." + ending), content);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|