refactored tariffloader, added exception checks
This commit is contained in:
@@ -4,6 +4,7 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.NavigableMap;
|
import java.util.NavigableMap;
|
||||||
@@ -31,44 +32,49 @@ public final class TariffLoader {
|
|||||||
|
|
||||||
return new TariffConfiguration(animalBaseSums(props), tariffs(props), ageFactors(props),
|
return new TariffConfiguration(animalBaseSums(props), tariffs(props), ageFactors(props),
|
||||||
breedFactors(props));
|
breedFactors(props));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, BigDecimal> animalBaseSums(Properties props) {
|
private static Map<String, BigDecimal> animalBaseSums(Properties props) {
|
||||||
|
|
||||||
Map<String, BigDecimal> baseSums = new HashMap<>();
|
Map<String, BigDecimal> baseSums = new HashMap<>();
|
||||||
|
|
||||||
for (String key : props.stringPropertyNames()) {
|
for (String key : props.stringPropertyNames()) {
|
||||||
if (key.startsWith("baseSum.")) {
|
if (key.startsWith("baseSum.")) {
|
||||||
String animal = key.substring("baseSum.".length()).toUpperCase();
|
String animal = key.substring("baseSum.".length()).toUpperCase();
|
||||||
BigDecimal baseSum = new BigDecimal(props.getProperty(key));
|
baseSums.put(animal, parseDecimal(key, props.getProperty(key)));
|
||||||
baseSums.put(animal, baseSum);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return baseSums;
|
return baseSums;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, TariffOption> tariffs(Properties props) {
|
private static Map<String, TariffOption> tariffs(Properties props) {
|
||||||
|
|
||||||
return props.stringPropertyNames().stream().filter(key -> key.startsWith("tariffs."))
|
return props.stringPropertyNames().stream().filter(key -> key.startsWith("tariffs."))
|
||||||
.map(key -> {
|
.map(key -> {
|
||||||
String name = key.substring("tariffs.".length()).toUpperCase();
|
String name = key.substring("tariffs.".length()).toUpperCase();
|
||||||
String[] values = props.getProperty(key).split(",");
|
String raw = props.getProperty(key);
|
||||||
BigDecimal factor = new BigDecimal(values[0]);
|
String[] values = raw.split(",");
|
||||||
LocalDate availableFrom = LocalDate.parse(values[1]);
|
if (values.length < 2) {
|
||||||
|
throw new IllegalStateException("Ungültiges Format für Tarifoption '"
|
||||||
|
+ key + "': erwartet 'Faktor,Datum', gefunden: '" + raw + "'");
|
||||||
|
}
|
||||||
|
BigDecimal factor = parseDecimal(key, values[0]);
|
||||||
|
LocalDate availableFrom = parseDate(key, values[1]);
|
||||||
return new TariffOption(name, factor, availableFrom);
|
return new TariffOption(name, factor, availableFrom);
|
||||||
}).collect(Collectors.toMap(TariffOption::name, tariff -> tariff));
|
}).collect(Collectors.toMap(TariffOption::name, tariff -> tariff));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NavigableMap<Integer, BigDecimal> ageFactors(Properties props) {
|
private static NavigableMap<Integer, BigDecimal> ageFactors(Properties props) {
|
||||||
|
|
||||||
NavigableMap<Integer, BigDecimal> ageFactors = new TreeMap<>();
|
NavigableMap<Integer, BigDecimal> ageFactors = new TreeMap<>();
|
||||||
|
|
||||||
for (String key : props.stringPropertyNames()) {
|
for (String key : props.stringPropertyNames()) {
|
||||||
if (key.startsWith("age.")) {
|
if (key.startsWith("age.")) {
|
||||||
Integer age = Integer.parseInt(key.replace("age.", ""));
|
String agePart = key.substring("age.".length());
|
||||||
BigDecimal factor = new BigDecimal(props.getProperty(key));
|
int age;
|
||||||
ageFactors.put(age, factor);
|
try {
|
||||||
|
age = Integer.parseInt(agePart);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Ungültiger Alterswert in Schlüssel '" + key + "': '" + agePart + "'",
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
ageFactors.put(age, parseDecimal(key, props.getProperty(key)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,23 +82,38 @@ public final class TariffLoader {
|
|||||||
if (rawElderly == null) {
|
if (rawElderly == null) {
|
||||||
throw new IllegalStateException("Pflichtkonfiguration 'elderlyFactor' fehlt.");
|
throw new IllegalStateException("Pflichtkonfiguration 'elderlyFactor' fehlt.");
|
||||||
}
|
}
|
||||||
ageFactors.put(Integer.MAX_VALUE, new BigDecimal(rawElderly));
|
ageFactors.put(Integer.MAX_VALUE, parseDecimal("elderlyFactor", rawElderly));
|
||||||
return ageFactors;
|
return ageFactors;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, BigDecimal> breedFactors(Properties props) {
|
private static Map<String, BigDecimal> breedFactors(Properties props) {
|
||||||
|
|
||||||
Map<String, BigDecimal> breedFactors = new HashMap<>();
|
Map<String, BigDecimal> breedFactors = new HashMap<>();
|
||||||
|
|
||||||
for (String key : props.stringPropertyNames()) {
|
for (String key : props.stringPropertyNames()) {
|
||||||
if (key.startsWith("breed.")) {
|
if (key.startsWith("breed.")) {
|
||||||
String name = key.substring("breed.".length()).toUpperCase();
|
String name = key.substring("breed.".length()).toUpperCase();
|
||||||
BigDecimal factor = new BigDecimal(props.getProperty(key));
|
breedFactors.put(name, parseDecimal(key, props.getProperty(key)));
|
||||||
breedFactors.put(name, factor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return breedFactors;
|
return breedFactors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static BigDecimal parseDecimal(String key, String value) {
|
||||||
|
try {
|
||||||
|
return new BigDecimal(value.trim());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Ungültiger Zahlenwert für '" + key + "': '" + value + "'", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static LocalDate parseDate(String key, String value) {
|
||||||
|
try {
|
||||||
|
return LocalDate.parse(value.trim());
|
||||||
|
} catch (DateTimeParseException e) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Ungültiges Datum für '" + key + "': '" + value + "'", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private TariffLoader() {}
|
private TariffLoader() {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user