JavaUtils/FabianUtil.java

187 lines
5.2 KiB
Java
Raw Normal View History

package de.vorsorge.theo.util;
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 {
public static String ENDING_TXT = "txt";
public static String ENDING_JSON = "json";
public static String ENDING_HTML = "html";
public static String ENDING_PDF = "pdf";
public static String ENDING_TIFF = "tiff";
private FabianUtil() {}
public static class ProgressHandler {
private static final DecimalFormat TIME_DECIMAL_FORMAT = new DecimalFormat("00");
private final ZonedDateTime startTime;
private final BigDecimal logDistance;
private BigDecimal counter = BigDecimal.ZERO;
public ProgressHandler(int logDistance) {
startTime = ZonedDateTime.now();
if (logDistance < 1) {
throw new RuntimeException("Log-Distanz darf nicht 0 oder negativ sein.");
}
this.logDistance = BigDecimal.valueOf(logDistance);
}
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() {
if (counter.remainder(logDistance).signum() == 0) {
log();
}
}
public void log() {
Duration timePassed = getTimePassed();
System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
}
protected Duration getTimePassed() {
return Duration.ofSeconds(startTime.until(ZonedDateTime.now(), ChronoUnit.SECONDS));
}
protected String formatDuration(Duration dur) {
return formatLong(dur.toHours()) + ":" + formatInt(dur.toMinutesPart()) + ":" + formatInt(dur.toSecondsPart());
}
private String formatLong(long n) {
return TIME_DECIMAL_FORMAT.format(n);
}
private String formatInt(int n) {
return TIME_DECIMAL_FORMAT.format(n);
}
}
public static class ProgressHandlerWithTotal extends ProgressHandler {
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
private final BigDecimal total;
public ProgressHandlerWithTotal(int logDistance, int total) {
super(logDistance);
if (total < 1) {
throw new RuntimeException("Maximale Anzahl darf nicht 0 oder negativ sein.");
}
this.total = BigDecimal.valueOf(total);
}
@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";
}
System.out.println(counter + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage.doubleValue()) + " %) erledigt. "
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
}
public void logFinal() {
Duration timePassed = getTimePassed();
System.out.println(total + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
}
}
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) {
writeToFile("C:/theo_dev/temp/testOutput/", filename, ending, content);
}
public static void writeDump(String ending, String content) {
writeDump(ending, strToBytes(content));
}
public static void writeDump(String ending, byte[] content) {
writeToFile("C:/theo_dev/temp/", "dump", ending, content);
}
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) {
writeToFile("C:/theo_dev/temp/", filename, ending, content);
}
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);
}
}
}