view 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
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.sinfo.tkhstate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.dive4elements.artifacts.common.utils.Config;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.model.River;

/**
 * Represents the contents of the 'bedheights.properties' files.
 *
 * @author Gernot Belger
 */
final class DefaultBedHeightsConfig {

    private static final String CONFIG_FILE = "sinfo_tkh_sohlhoehen_%s.properties";


    public static class DefaultBedHeight {

        public final String description;
        public final double startKm;
        public final double endKm;

        public DefaultBedHeight(final String description, final double startKm, final double endKm) {
            this.description = description;
            this.startKm = startKm;
            this.endKm = endKm;
        }
    }

    private static DefaultBedHeightsConfig INSTANCE = new DefaultBedHeightsConfig();

    public static synchronized Collection<DefaultBedHeight> getDefaults(final River river, final Calculation problems) {
        return INSTANCE.getBedHeightDefaultsForRiver(river, problems);
    }

    private final Map<String, Collection<DefaultBedHeight>> cache = new HashMap<>();

    private DefaultBedHeightsConfig() {
    }


    private synchronized Collection<DefaultBedHeight> getBedHeightDefaultsForRiver(final River river, final Calculation problems) {

        final String rivername = river.getName();
        if (!this.cache.containsKey(rivername)) {
            final Collection<DefaultBedHeight> newDefaults = loadBedHeightDefaultsForRiver(river, problems);
            this.cache.put(rivername, null);
            return newDefaults;
        }

        return this.cache.get(rivername);
    }

    private static Collection<DefaultBedHeight> loadBedHeightDefaultsForRiver(final River river, final Calculation problems) {

        try {
            final String rivername = river.getName();
            final String filename = String.format(CONFIG_FILE, rivername);

            final Properties properties = Config.loadProperties(filename);

            final Set<String> keys = properties.stringPropertyNames();

            final Collection<DefaultBedHeight> defaults = new ArrayList<>(keys.size());

            for (final String key : keys) {

                final String value = properties.getProperty(key);

                final String[] split = StringUtils.split(StringUtils.trim(value), ';');

                final double startKm = Double.parseDouble(split[0]);
                final double endKm = Double.parseDouble(split[1]);

                defaults.add(new DefaultBedHeight(key, startKm, endKm));
            }

            return defaults;
        }
        catch (final Exception e) {
            e.printStackTrace();
            problems.addProblem("sinfo.bedheightsfinder.configfile.loaderror", CONFIG_FILE, e.getMessage());
            return Collections.emptyList();
        }
    }
}

http://dive4elements.wald.intevation.org