Compare commits
5 Commits
63b0e39ad5
...
f514e1c6f8
Author | SHA1 | Date | |
---|---|---|---|
|
f514e1c6f8 | ||
|
c6a346df00 | ||
|
bad7fa46eb | ||
|
b63326b0ac | ||
|
20f3049239 |
317
FabianUtil.java
317
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,170 @@ public class FabianUtil {
|
||||
private FabianUtil() {}
|
||||
|
||||
|
||||
public static class Logger {
|
||||
|
||||
private final java.util.logging.Logger logger;
|
||||
|
||||
|
||||
protected Logger(Class<?> clazz) {
|
||||
this(java.util.logging.Logger.getLogger(clazz.getName()));
|
||||
}
|
||||
|
||||
|
||||
protected Logger(java.util.logging.Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public boolean isLoggable(Level level) {
|
||||
return logger.isLoggable(level);
|
||||
}
|
||||
|
||||
|
||||
public void severe(String msg) {
|
||||
logger.severe(msg);
|
||||
}
|
||||
|
||||
|
||||
public void severe(String msg, Object param) {
|
||||
logger.log(Level.SEVERE, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void severe(String msg, Object... params) {
|
||||
logger.log(Level.SEVERE, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void severe(Supplier<String> msgSupplier) {
|
||||
logger.severe(msgSupplier);
|
||||
}
|
||||
|
||||
|
||||
public void warning(String msg) {
|
||||
logger.warning(msg);
|
||||
}
|
||||
|
||||
|
||||
public void warning(String msg, Object param) {
|
||||
logger.log(Level.WARNING, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void warning(String msg, Object... params) {
|
||||
logger.log(Level.WARNING, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void warning(Supplier<String> msgSupplier) {
|
||||
logger.warning(msgSupplier);
|
||||
}
|
||||
|
||||
|
||||
public void info(String msg) {
|
||||
logger.info(msg);
|
||||
}
|
||||
|
||||
|
||||
public void info(String msg, Object param) {
|
||||
logger.log(Level.INFO, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void info(String msg, Object... params) {
|
||||
logger.log(Level.INFO, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void info(Supplier<String> msgSupplier) {
|
||||
logger.info(msgSupplier);
|
||||
}
|
||||
|
||||
|
||||
public void config(String msg) {
|
||||
logger.config(msg);
|
||||
}
|
||||
|
||||
|
||||
public void config(String msg, Object param) {
|
||||
logger.log(Level.CONFIG, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void config(String msg, Object... params) {
|
||||
logger.log(Level.CONFIG, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void config(Supplier<String> msgSupplier) {
|
||||
logger.config(msgSupplier);
|
||||
}
|
||||
|
||||
|
||||
public void fine(String msg) {
|
||||
logger.fine(msg);
|
||||
}
|
||||
|
||||
|
||||
public void fine(String msg, Object param) {
|
||||
logger.log(Level.FINE, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void fine(String msg, Object... params) {
|
||||
logger.log(Level.FINE, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void fine(Supplier<String> msgSupplier) {
|
||||
logger.fine(msgSupplier);
|
||||
}
|
||||
|
||||
|
||||
public void finer(String msg) {
|
||||
logger.finer(msg);
|
||||
}
|
||||
|
||||
|
||||
public void finer(String msg, Object param) {
|
||||
logger.log(Level.FINER, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void finer(String msg, Object... params) {
|
||||
logger.log(Level.FINER, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void finer(Supplier<String> msgSupplier) {
|
||||
logger.finer(msgSupplier);
|
||||
}
|
||||
|
||||
|
||||
public void finest(String msg) {
|
||||
logger.finest(msg);
|
||||
}
|
||||
|
||||
|
||||
public void finest(String msg, Object param) {
|
||||
logger.log(Level.FINEST, msg, param);
|
||||
}
|
||||
|
||||
|
||||
public void finest(String msg, Object... params) {
|
||||
logger.log(Level.FINEST, msg, params);
|
||||
}
|
||||
|
||||
|
||||
public void finest(Supplier<String> msgSupplier) {
|
||||
logger.finest(msgSupplier);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 +204,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 +243,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 +283,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 +300,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 +331,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 +398,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 +415,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 +430,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 +459,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 +479,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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user