Compare commits

..

No commits in common. "71daaaff157268ca373c4dbd7ebdc6fb513ddd11" and "4fc5648c4ff3ad0bfd12acda567a365eb0b15cca" have entirely different histories.

2 changed files with 23 additions and 73 deletions

View File

@ -1,8 +1,6 @@
package de.vorsorge.theo.util;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.MathContext; import java.math.RoundingMode;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -10,7 +8,6 @@ import java.text.DecimalFormat;
import java.time.Duration; import java.time.Duration;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
public class FabianUtil { public class FabianUtil {
@ -68,69 +65,54 @@ public class FabianUtil {
} }
protected synchronized BigDecimal getCounter() { protected BigDecimal getCounter() {
return counter; return counter;
} }
protected synchronized int getTimeLogDistance() { public void tickAndLog() {
return timeLogDistance;
}
public synchronized void tickAndLog() {
tick(); tick();
logIf(); logIf();
} }
protected synchronized void tick() { protected void tick() {
counter = counter.add(BigDecimal.ONE); counter = counter.add(BigDecimal.ONE);
} }
public synchronized void logAndTick() { public void logAndTick() {
logIf(); logIf();
tick(); tick();
} }
public synchronized void logIf() { public void logIf() {
if (shouldLog()) { if (counter.remainder(logDistance).signum() == 0 || isBored()) {
log(); log();
} }
} }
protected synchronized boolean shouldLog() { private boolean isBored() {
return counter.remainder(logDistance).signum() == 0 || isBored();
}
private synchronized boolean isBored() {
return getTimeSinceLastLogged() >= timeLogDistance;
}
protected synchronized long getTimeSinceLastLogged() {
ZonedDateTime time = lastLoggedAt == null ? startTime : lastLoggedAt; ZonedDateTime time = lastLoggedAt == null ? startTime : lastLoggedAt;
return time.until(ZonedDateTime.now(), ChronoUnit.SECONDS); return time.until(ZonedDateTime.now(), ChronoUnit.SECONDS) >= timeLogDistance;
} }
public synchronized void log() { public void log() {
Duration timePassed = getTimePassed(); Duration timePassed = getTimePassed();
System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen."); System.out.println(counter + " Stück erledigt. " + formatDuration(timePassed) + " vergangen.");
updateLastLoggedAt(); updateLastLoggedAt();
} }
protected synchronized void updateLastLoggedAt() { protected void updateLastLoggedAt() {
lastLoggedAt = ZonedDateTime.now(); lastLoggedAt = ZonedDateTime.now();
} }
protected synchronized Duration getTimePassed() { protected Duration getTimePassed() {
return Duration.ofSeconds(startTime.until(ZonedDateTime.now(), ChronoUnit.SECONDS)); return Duration.ofSeconds(startTime.until(ZonedDateTime.now(), ChronoUnit.SECONDS));
} }
@ -145,7 +127,7 @@ public class FabianUtil {
} }
public synchronized void logFinal() { public void logFinal() {
Duration timePassed = getTimePassed(); Duration timePassed = getTimePassed();
System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen."); System.out.println(counter + " Stück (100 %) erledigt. " + formatDuration(timePassed) + " vergangen.");
} }
@ -176,21 +158,16 @@ public class FabianUtil {
@Override @Override
protected synchronized boolean shouldLog() { public void log() {
return super.shouldLog() && getCounter().compareTo(total) <= 0;
}
@Override
public synchronized void log() {
BigDecimal counter = getCounter(); BigDecimal counter = getCounter();
BigDecimal percentage = BigDecimal.valueOf(100).divide(total, MathContext.DECIMAL64).multiply(counter); BigDecimal percentage = BigDecimal.valueOf(100).divide(total, 15, RoundingMode.HALF_UP).multiply(counter);
Duration timePassed = getTimePassed(); Duration timePassed = getTimePassed();
String remainingTimePart = ""; String remainingTimePart = "";
if (counter.signum() != 0) { if (counter.signum() != 0) {
BigDecimal secondsPassed = BigDecimal.valueOf(timePassed.getSeconds()); BigDecimal secondsPassed = BigDecimal.valueOf(timePassed.getSeconds());
BigDecimal totalSeconds = secondsPassed.divide(counter, MathContext.DECIMAL64).multiply(total); BigDecimal totalSeconds = secondsPassed.divide(counter, 15, RoundingMode.HALF_UP).multiply(total);
BigDecimal secondsToGo = totalSeconds.subtract(secondsPassed); BigDecimal secondsToGo = totalSeconds.subtract(secondsPassed);
Duration timeToGo = Duration.of(secondsToGo.longValue(), ChronoUnit.SECONDS); Duration timeToGo = Duration.of(secondsToGo.longValue(), ChronoUnit.SECONDS);
remainingTimePart = ", " + formatDuration(timeToGo) + " verbleibend"; remainingTimePart = ", " + formatDuration(timeToGo) + " verbleibend";
@ -201,25 +178,6 @@ public class FabianUtil {
+ formatDuration(timePassed) + " vergangen" + remainingTimePart + "."); + formatDuration(timePassed) + " vergangen" + remainingTimePart + ".");
updateLastLoggedAt(); updateLastLoggedAt();
} }
public void logContinuouslyTimeBased() {
new Thread(this::logTimeBased).start();
}
private synchronized void logTimeBased() {
while (getCounter().compareTo(total) != 0) {
logIf();
long timeSinceLastLogged = getTimeSinceLastLogged();
try {
TimeUnit.SECONDS.sleep(timeSinceLastLogged - getTimeLogDistance());
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return;
}
}
}
} }

View File

@ -1,5 +1,3 @@
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -21,12 +19,11 @@ public class LoadingBar {
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00"); private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
private static final int MIN_LUNCH_DURATION = 30; private static final int MIN_LUNCH_DURATION = 30;
private static final LocalTime LATEST_LUNCH_TIME = LocalTime.of(13, 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 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 MINS_PER_HOUR = 60;
private static final long DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH = 5L * MINS_PER_HOUR;
private static final int MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH = 6 * MINS_PER_HOUR;
private static final long MAX_NUMBER_WORK_MINS = 8L * MINS_PER_HOUR;
private static final int LINE_LENGTH = 100; private static final int LINE_LENGTH = 100;
private static final MathContext MC_INTEGER = new MathContext(1, RoundingMode.HALF_EVEN);
private enum DaySection { private enum DaySection {
@ -336,11 +333,9 @@ public class LoadingBar {
private static String fillLoadingBar(long initialMinutes, long passedMinutes, boolean progressive) { private static String fillLoadingBar(long initialMinutes, long passedMinutes, boolean progressive) {
BigDecimal wholePercentage = BigDecimal.valueOf(100) double wholePercentage = ((double) passedMinutes / initialMinutes) * 100;
.divide(BigDecimal.valueOf(initialMinutes), MathContext.DECIMAL64)
.multiply(BigDecimal.valueOf(passedMinutes));
long remainingMinutes = initialMinutes - passedMinutes; long remainingMinutes = initialMinutes - passedMinutes;
int numberOfEquals = wholePercentage.intValue(); int numberOfEquals = (int) wholePercentage;
var sb = new StringBuilder("["); var sb = new StringBuilder("[");
for (int i = 0; i < LINE_LENGTH; i++) { for (int i = 0; i < LINE_LENGTH; i++) {
if (i < numberOfEquals) { if (i < numberOfEquals) {
@ -359,9 +354,6 @@ public class LoadingBar {
private static String minutesToTimeString(long minutes) { private static String minutesToTimeString(long minutes) {
var minutesBD = BigDecimal.valueOf(minutes); return LocalTime.of((int) minutes / MINS_PER_HOUR, (int) minutes % MINS_PER_HOUR).format(TIME_FORMATTER);
int hours = minutesBD.divide(BigDecimal.valueOf(MINS_PER_HOUR), MC_INTEGER).intValue();
int rest_minutes = minutesBD.remainder(BigDecimal.valueOf(MINS_PER_HOUR), MC_INTEGER).intValue();
return LocalTime.of(hours, rest_minutes).format(TIME_FORMATTER);
} }
} }