Compare commits
No commits in common. "4c76542f54fa517ac167e8fcc67b18f57086dbf6" and "2af0eaebddb8d1a01ded18e4dffa488a3552cab6" have entirely different histories.
4c76542f54
...
2af0eaebdd
@ -1,3 +1,5 @@
|
||||
package de.vorsorge.theo.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@ -24,43 +26,18 @@ public class FabianUtil {
|
||||
public static class ProgressHandler {
|
||||
|
||||
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;
|
||||
|
||||
private final ZonedDateTime startTime;
|
||||
private final BigDecimal logDistance;
|
||||
private final int timeLogDistance;
|
||||
private BigDecimal counter = BigDecimal.ZERO;
|
||||
private ZonedDateTime lastLoggedAt;
|
||||
|
||||
|
||||
public ProgressHandler() {
|
||||
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandler(int logDistance) {
|
||||
this(logDistance, DEFAULT_TIME_LOG_DISTANCE);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandler(int logDistance, int timeLogDistance) {
|
||||
startTime = ZonedDateTime.now();
|
||||
this.logDistance = validateBD(logDistance, "Log-Distanz");
|
||||
this.timeLogDistance = validateInt(timeLogDistance, "Zeitbasierte Log-Distanz");
|
||||
if (logDistance < 1) {
|
||||
throw new RuntimeException("Log-Distanz darf nicht 0 oder negativ sein.");
|
||||
}
|
||||
|
||||
|
||||
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.");
|
||||
}
|
||||
return n;
|
||||
this.logDistance = BigDecimal.valueOf(logDistance);
|
||||
}
|
||||
|
||||
|
||||
@ -87,27 +64,15 @@ public class FabianUtil {
|
||||
|
||||
|
||||
public void logIf() {
|
||||
if (counter.remainder(logDistance).signum() == 0 || isBored()) {
|
||||
if (counter.remainder(logDistance).signum() == 0) {
|
||||
log();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean isBored() {
|
||||
ZonedDateTime time = lastLoggedAt == null ? startTime : lastLoggedAt;
|
||||
return time.until(ZonedDateTime.now(), ChronoUnit.SECONDS) >= timeLogDistance;
|
||||
}
|
||||
|
||||
|
||||
public void log() {
|
||||
Duration timePassed = getTimePassed();
|
||||
System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
updateLastLoggedAt();
|
||||
}
|
||||
|
||||
|
||||
protected void updateLastLoggedAt() {
|
||||
lastLoggedAt = ZonedDateTime.now();
|
||||
}
|
||||
|
||||
|
||||
@ -117,18 +82,17 @@ public class FabianUtil {
|
||||
|
||||
|
||||
protected String formatDuration(Duration dur) {
|
||||
return formatNumber(dur.toHours()) + ":" + formatNumber(dur.toMinutesPart()) + ":" + formatNumber(dur.toSecondsPart());
|
||||
return formatLong(dur.toHours()) + ":" + formatInt(dur.toMinutesPart()) + ":" + formatInt(dur.toSecondsPart());
|
||||
}
|
||||
|
||||
|
||||
protected String formatNumber(Number n) {
|
||||
return TIME_DECIMAL_FORMAT.format(n == null ? 0 : n);
|
||||
private String formatLong(long n) {
|
||||
return TIME_DECIMAL_FORMAT.format(n);
|
||||
}
|
||||
|
||||
|
||||
public void logFinal() {
|
||||
Duration timePassed = getTimePassed();
|
||||
System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
private String formatInt(int n) {
|
||||
return TIME_DECIMAL_FORMAT.format(n);
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,19 +104,12 @@ public class FabianUtil {
|
||||
private final BigDecimal total;
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int total) {
|
||||
this(DEFAULT_LOG_DISTANCE, DEFAULT_TIME_LOG_DISTANCE, total);
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int logDistance, int total) {
|
||||
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, total);
|
||||
super(logDistance);
|
||||
if (total < 1) {
|
||||
throw new RuntimeException("Maximale Anzahl darf nicht 0 oder negativ sein.");
|
||||
}
|
||||
|
||||
|
||||
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total) {
|
||||
super(logDistance, timeLogDistance);
|
||||
this.total = validateBD(total, "Gesamt-Anzahl");
|
||||
this.total = BigDecimal.valueOf(total);
|
||||
}
|
||||
|
||||
|
||||
@ -172,10 +129,14 @@ public class FabianUtil {
|
||||
remainingTimePart = ", " + formatDuration(timeToGo) + " verbleibend";
|
||||
}
|
||||
|
||||
String counterPrint = total.compareTo(BigDecimal.TEN) >= 0 ? formatNumber(counter) : counter.toString();
|
||||
System.out.println(counterPrint + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage) + " %) erledigt. "
|
||||
System.out.println(counter + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage.doubleValue()) + " %) erledigt. "
|
||||
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
|
||||
updateLastLoggedAt();
|
||||
}
|
||||
|
||||
|
||||
public void logFinal() {
|
||||
Duration timePassed = getTimePassed();
|
||||
System.out.println(total + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,24 +9,21 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LoadingBar {
|
||||
class LoadingBar {
|
||||
|
||||
private static final String TIME_FORMAT = "HH:mm";
|
||||
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_FORMAT);
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile("(?>[01]\\d|2[0-4]):[0-5]\\d");
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile("(?>[01][0-9]|2[0-4]):[0-5][0-9]");
|
||||
private static final Pattern LUNCH_DURATION_PATTERN = Pattern.compile("\\d+");
|
||||
private static final Pattern OFFSET_PATTERN = Pattern.compile("[+-]\\d+");
|
||||
private static final Pattern OFFSET_PATTERN = Pattern.compile("[\\+\\-]\\d+");
|
||||
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
||||
private static final int MIN_LUNCH_DURATION = 30;
|
||||
private static final LocalTime LATEST_LUNCH_TIME = LocalTime.of(13, 30);
|
||||
private static final long DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH = 5L * 60;
|
||||
private static final int DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH = 5 * 60;
|
||||
private static final int MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH = 6 * 60;
|
||||
private static final long MAX_NUMBER_WORK_MINS = 8L * 60;
|
||||
private static final int MINS_PER_HOUR = 60;
|
||||
private static final int LINE_LENGTH = 100;
|
||||
private static final int MAX_NUMBER_WORK_MINS = 8 * 60;
|
||||
|
||||
|
||||
private enum DaySection {
|
||||
private static enum DaySection {
|
||||
MITTAG("-m", "Mittag"),
|
||||
ZAPFENSTREICH("-z", "Zapfenstreich");
|
||||
|
||||
@ -34,7 +31,7 @@ public class LoadingBar {
|
||||
private final String description;
|
||||
|
||||
|
||||
DaySection(String param, String description) {
|
||||
private DaySection(String param, String description) {
|
||||
this.param = param;
|
||||
this.description = description;
|
||||
}
|
||||
@ -95,7 +92,7 @@ public class LoadingBar {
|
||||
private static void handleZapfenstreich(String[] args, LocalTime startTime) {
|
||||
Integer lunchDuration = null;
|
||||
if (args.length == 2) {
|
||||
showLoadingBarZapfenstreich(startTime);
|
||||
showLoadingBarZapfenstreich(startTime, lunchDuration);
|
||||
return;
|
||||
}
|
||||
String nextArg = args[2];
|
||||
@ -206,7 +203,7 @@ public class LoadingBar {
|
||||
+ TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " " + TIME_FORMAT + " -+mm\n\n"
|
||||
+ "Mittagspause minimum in Minuten:\n"
|
||||
+ " - bis " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min ("
|
||||
+ MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH / MINS_PER_HOUR + " std): 0\n"
|
||||
+ MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH / 60 + " std): 0\n"
|
||||
+ " - bis " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min + "
|
||||
+ MIN_LUNCH_DURATION + " min: Arbeitszeit - " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min\n"
|
||||
+ " - ab " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min + " + MIN_LUNCH_DURATION + " min: "
|
||||
@ -234,19 +231,14 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime) {
|
||||
showLoadingBarZapfenstreich(startTime, -1, 0);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime, Integer manualLunchDuration) {
|
||||
showLoadingBarZapfenstreich(startTime, manualLunchDuration, 0);
|
||||
}
|
||||
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime, Integer manualLunchDuration, int endTimeOffset) {
|
||||
long minLunchDuration = getMinLunchDuration(endTimeOffset);
|
||||
long realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
int minLunchDuration = getMinLunchDuration(startTime, endTimeOffset);
|
||||
int realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
LocalTime trueEndTime = startTime.plusMinutes(MAX_NUMBER_WORK_MINS + realLunchDuration + endTimeOffset);
|
||||
realShowLoadingBarZapfenstreich(startTime, realLunchDuration, trueEndTime);
|
||||
}
|
||||
@ -254,8 +246,8 @@ public class LoadingBar {
|
||||
|
||||
private static void showLoadingBarZapfenstreich(LocalTime startTime, Integer manualLunchDuration, LocalTime manualEndTime) {
|
||||
LocalTime trueEndTime = manualEndTime;
|
||||
long minLunchDuration = getMinLunchDuration(startTime, trueEndTime);
|
||||
long realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
int minLunchDuration = getMinLunchDuration(startTime, trueEndTime);
|
||||
int realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
if (trueEndTime == null) {
|
||||
trueEndTime = startTime.plusMinutes(MAX_NUMBER_WORK_MINS + realLunchDuration);
|
||||
}
|
||||
@ -263,17 +255,17 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static long getMinLunchDuration(int endTimeOffset) {
|
||||
private static int getMinLunchDuration(LocalTime startTime, 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;
|
||||
int totalDuration = MAX_NUMBER_WORK_MINS + endTimeOffset;
|
||||
int effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
}
|
||||
|
||||
|
||||
private static long getMinLunchDuration(LocalTime startTime, LocalTime endTime) {
|
||||
private static int getMinLunchDuration(LocalTime startTime, LocalTime endTime) {
|
||||
if (endTime == null) {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
@ -283,20 +275,20 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private static long getMinLunchDuration(long effectiveLunchDuration) {
|
||||
private static int getMinLunchDuration(int effectiveLunchDuration) {
|
||||
if (effectiveLunchDuration < 0) {
|
||||
effectiveLunchDuration = 0;
|
||||
}
|
||||
return Math.min(effectiveLunchDuration, MIN_LUNCH_DURATION);
|
||||
return effectiveLunchDuration < MIN_LUNCH_DURATION ? effectiveLunchDuration : MIN_LUNCH_DURATION;
|
||||
}
|
||||
|
||||
|
||||
private static long getRealLunchDuration(Integer manualLunchDuration, long minLunchDuration) {
|
||||
private static int getRealLunchDuration(Integer manualLunchDuration, int minLunchDuration) {
|
||||
return manualLunchDuration != null && manualLunchDuration >= minLunchDuration ? manualLunchDuration : minLunchDuration;
|
||||
}
|
||||
|
||||
|
||||
private static void realShowLoadingBarZapfenstreich(LocalTime startTime, long manualLunchDuration, LocalTime endTime) {
|
||||
private static void realShowLoadingBarZapfenstreich(LocalTime startTime, int manualLunchDuration, LocalTime endTime) {
|
||||
if (manualLunchDuration > 0) {
|
||||
var totalWorkTime = LocalTime.MIDNIGHT.plusMinutes(startTime.until(endTime, ChronoUnit.MINUTES) - manualLunchDuration);
|
||||
System.out.print("Arbeitszeit: " + TIME_FORMATTER.format(totalWorkTime) + "; ");
|
||||
@ -337,7 +329,7 @@ public class LoadingBar {
|
||||
long remainingMinutes = initialMinutes - passedMinutes;
|
||||
int numberOfEquals = (int) wholePercentage;
|
||||
var sb = new StringBuilder("[");
|
||||
for (int i = 0; i < LINE_LENGTH; i++) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if (i < numberOfEquals) {
|
||||
sb.append("=");
|
||||
} else {
|
||||
@ -354,6 +346,6 @@ public class LoadingBar {
|
||||
|
||||
|
||||
private static String minutesToTimeString(long minutes) {
|
||||
return LocalTime.of((int) minutes / MINS_PER_HOUR, (int) minutes % MINS_PER_HOUR).format(TIME_FORMATTER);
|
||||
return LocalTime.of((int) minutes / 60, (int) minutes % 60).format(TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
@ -6,14 +6,12 @@ import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SimpleLoadingBar {
|
||||
class SimpleLoadingBar {
|
||||
|
||||
private static final String TIME_FORMAT = "HH:mm";
|
||||
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_FORMAT);
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile("(?>[01]\\d|2[0-4]):[0-5]\\d");
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile("(?>[01][0-9]|2[0-4]):[0-5][0-9]");
|
||||
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
||||
private static final int MINS_PER_HOUR = 60;
|
||||
private static final int LINE_LENGTH = 100;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
@ -31,7 +29,7 @@ public class SimpleLoadingBar {
|
||||
if (args.length > 1) {
|
||||
startTime = firstTime;
|
||||
nextArg = args[1];
|
||||
if ("-msg".equals(nextArg)) {
|
||||
if (nextArg.equals("-msg")) {
|
||||
title = args.length > 2 ? args[2] : title;
|
||||
} else {
|
||||
verifyTimeFormat(nextArg, "Zweites Argument");
|
||||
@ -49,7 +47,7 @@ public class SimpleLoadingBar {
|
||||
return;
|
||||
}
|
||||
// if there are 3 arguments, the third will be discarded.
|
||||
boolean hasTitleArg = args.length > 3 && "-msg".equals(args[2]);
|
||||
boolean hasTitleArg = args.length > 3 && args[2].equals("-msg");
|
||||
title = hasTitleArg ? args[3] : title;
|
||||
title = title.isBlank() ? fallbackTitle : title;
|
||||
showLoadingBar(startTime, endTime, title);
|
||||
@ -57,11 +55,11 @@ public class SimpleLoadingBar {
|
||||
|
||||
|
||||
private static void printHelp() {
|
||||
System.out.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"
|
||||
+ TIME_FORMAT + " -msg <Nachricht>\n"
|
||||
System.out.println(new StringBuilder().append("Mögliche Argumente für LoadingBar:\n")
|
||||
.append("Startzeit, Endzeit, Endnachricht (Optional)\n")
|
||||
.append(TIME_FORMAT).append(" ").append(TIME_FORMAT).append("-msg <Nachricht>\n")
|
||||
.append("Endzeit (Startzeit = jetzt), Endnachricht (Optional)\n")
|
||||
.append(TIME_FORMAT).append("-msg <Nachricht>\n")
|
||||
);
|
||||
}
|
||||
|
||||
@ -85,6 +83,7 @@ public class SimpleLoadingBar {
|
||||
}
|
||||
|
||||
|
||||
|
||||
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");
|
||||
@ -118,7 +117,7 @@ public class SimpleLoadingBar {
|
||||
long remainingMinutes = initialMinutes - passedMinutes;
|
||||
int numberOfEquals = (int) wholePercentage;
|
||||
var sb = new StringBuilder("[");
|
||||
for (int i = 0; i < LINE_LENGTH; i++) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if (i < numberOfEquals) {
|
||||
sb.append("=");
|
||||
} else {
|
||||
@ -135,12 +134,15 @@ public class SimpleLoadingBar {
|
||||
|
||||
|
||||
private static String minutesToTimeString(long minutes) {
|
||||
return LocalTime.of((int) minutes / MINS_PER_HOUR, (int) minutes % MINS_PER_HOUR).format(TIME_FORMATTER);
|
||||
return LocalTime.of((int) minutes / 60, (int) minutes % 60).format(TIME_FORMATTER);
|
||||
}
|
||||
|
||||
|
||||
private static String formatTitle(String title) {
|
||||
String separator = "*".repeat(title.length());
|
||||
return separator + "\n" + title + "\n" + separator;
|
||||
var sb = new StringBuilder();
|
||||
for (int i = 0; i < title.length(); i++) {
|
||||
sb.append("*");
|
||||
}
|
||||
return sb.toString() + "\n" + title + "\n" + sb.toString();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user