- optimisations by SonarQube and IntelliJ Inspections

- own smaller optimisations
This commit is contained in:
fabianArbeit 2024-11-06 09:58:52 +01:00
parent 416789c88c
commit 8426fa4a33

View File

@ -1,5 +1,3 @@
package de.vorsorge.theo.util;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
@ -26,18 +24,43 @@ 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();
if (logDistance < 1) {
throw new RuntimeException("Log-Distanz darf nicht 0 oder negativ sein.");
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.");
}
this.logDistance = BigDecimal.valueOf(logDistance);
return n;
}
@ -82,17 +105,18 @@ public class FabianUtil {
protected String formatDuration(Duration dur) {
return formatLong(dur.toHours()) + ":" + formatInt(dur.toMinutesPart()) + ":" + formatInt(dur.toSecondsPart());
return formatNumber(dur.toHours()) + ":" + formatNumber(dur.toMinutesPart()) + ":" + formatNumber(dur.toSecondsPart());
}
private String formatLong(long n) {
return TIME_DECIMAL_FORMAT.format(n);
protected String formatNumber(Number n) {
return TIME_DECIMAL_FORMAT.format(n == null ? 0 : n);
}
private String formatInt(int n) {
return TIME_DECIMAL_FORMAT.format(n);
public void logFinal() {
Duration timePassed = getTimePassed();
System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
}
}
@ -104,12 +128,19 @@ 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) {
super(logDistance);
if (total < 1) {
throw new RuntimeException("Maximale Anzahl darf nicht 0 oder negativ sein.");
}
this.total = BigDecimal.valueOf(total);
this(logDistance, DEFAULT_TIME_LOG_DISTANCE, total);
}
public ProgressHandlerWithTotal(int logDistance, int timeLogDistance, int total) {
super(logDistance, timeLogDistance);
this.total = validateBD(total, "Gesamt-Anzahl");
}
@ -129,15 +160,10 @@ public class FabianUtil {
remainingTimePart = ", " + formatDuration(timeToGo) + " verbleibend";
}
System.out.println(counter + " Stück von " + total + " (" + PERCENTAGE_FORMAT.format(percentage.doubleValue()) + " %) erledigt. "
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 + ".");
}
public void logFinal() {
Duration timePassed = getTimePassed();
System.out.println(total + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
}
}