comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/DefaultBedHeightsConfig.java @ 9211:aca5a7a57a3a

SINFO-TKH: definition der standard sohlhöhen jetzt mit Gültigkeitsstrecke. Definitionsdateien aufgeteilt in eine pro Gewässer.
author gernotbelger
date Tue, 03 Jul 2018 13:09:46 +0200
parents
children eedb0bcf226b
comparison
equal deleted inserted replaced
9210:de55d9a94796 9211:aca5a7a57a3a
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
2 * Software engineering by
3 * Björnsen Beratende Ingenieure GmbH
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
5 *
6 * This file is Free Software under the GNU AGPL (>=v3)
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the
8 * documentation coming with Dive4Elements River for details.
9 */
10 package org.dive4elements.river.artifacts.sinfo.tkhstate;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Properties;
18 import java.util.Set;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.dive4elements.artifacts.common.utils.Config;
22 import org.dive4elements.river.artifacts.model.Calculation;
23 import org.dive4elements.river.model.River;
24
25 /**
26 * Represents the contents of the 'bedheights.properties' files.
27 *
28 * @author Gernot Belger
29 */
30 final class DefaultBedHeightsConfig {
31
32 private static final String CONFIG_FILE = "sinfo_tkh_sohlhoehen_%s.properties";
33
34
35 public static class DefaultBedHeight {
36
37 public final String description;
38 public final double startKm;
39 public final double endKm;
40
41 public DefaultBedHeight(final String description, final double startKm, final double endKm) {
42 this.description = description;
43 this.startKm = startKm;
44 this.endKm = endKm;
45 }
46 }
47
48 private static DefaultBedHeightsConfig INSTANCE = new DefaultBedHeightsConfig();
49
50 public static synchronized Collection<DefaultBedHeight> getDefaults(final River river, final Calculation problems) {
51 return INSTANCE.getBedHeightDefaultsForRiver(river, problems);
52 }
53
54 private final Map<String, Collection<DefaultBedHeight>> cache = new HashMap<>();
55
56 private DefaultBedHeightsConfig() {
57 }
58
59
60 private synchronized Collection<DefaultBedHeight> getBedHeightDefaultsForRiver(final River river, final Calculation problems) {
61
62 final String rivername = river.getName();
63 if (!this.cache.containsKey(rivername)) {
64 final Collection<DefaultBedHeight> newDefaults = loadBedHeightDefaultsForRiver(river, problems);
65 this.cache.put(rivername, null);
66 return newDefaults;
67 }
68
69 return this.cache.get(rivername);
70 }
71
72 private static Collection<DefaultBedHeight> loadBedHeightDefaultsForRiver(final River river, final Calculation problems) {
73
74 try {
75 final String rivername = river.getName();
76 final String filename = String.format(CONFIG_FILE, rivername);
77
78 final Properties properties = Config.loadProperties(filename);
79
80 final Set<String> keys = properties.stringPropertyNames();
81
82 final Collection<DefaultBedHeight> defaults = new ArrayList<>(keys.size());
83
84 for (final String key : keys) {
85
86 final String value = properties.getProperty(key);
87
88 final String[] split = StringUtils.split(StringUtils.trim(value), ';');
89
90 final double startKm = Double.parseDouble(split[0]);
91 final double endKm = Double.parseDouble(split[1]);
92
93 defaults.add(new DefaultBedHeight(key, startKm, endKm));
94 }
95
96 return defaults;
97 }
98 catch (final Exception e) {
99 e.printStackTrace();
100 problems.addProblem("sinfo.bedheightsfinder.configfile.loaderror", CONFIG_FILE, e.getMessage());
101 return Collections.emptyList();
102 }
103 }
104 }

http://dive4elements.wald.intevation.org