view gwt-client/src/main/java/org/dive4elements/river/client/shared/model/SalixZone.java @ 9416:05405292a7ca

Navigationtheme panel now shows themes of dWt and WQ charts grayed out, if the current station is outside the valid range of the theme.
author gernotbelger
date Thu, 16 Aug 2018 16:28:03 +0200
parents da0bdbcd6f09
children 26ac94cff0b6
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.client.shared.model;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 * @author Domenico Nardi Tironi
 *
 */
public class SalixZone implements Comparable<SalixZone> {

    // IMMER ABGLEICHEN Server Client SalixZone.java
    private static final String TABLE_CELL_SEPARATOR = "TABLE_CELL_SEPARATOR";
    private static final String TABLE_ROW_SEPARATOR = "TABLE_ROW_SEPARATOR";
    private final int dwsplValue;
    private final double fromKm;
    private final double toKm;

    private final static double DELTA = 0.0001;

    public static List<SalixZone> parse(final String zonesRaw) {
        final List<SalixZone> resultList = new ArrayList<SalixZone>();

        final List<String[]> results = new ArrayList<String[]>();
        if (zonesRaw.contains(TABLE_ROW_SEPARATOR)) {
            final String[] rows = zonesRaw.split(TABLE_ROW_SEPARATOR);
            for (final String row : rows) {
                if (row.contains(TABLE_CELL_SEPARATOR)) {
                    final String[] result = row.split(TABLE_CELL_SEPARATOR);
                    results.add(result);
                }
            }
        }
        for (final String[] zone : results) {
            final SalixZone helper = new SalixZone(Integer.valueOf(zone[0]), Double.valueOf(zone[1]), Double.valueOf(zone[2]));
            resultList.add(helper);
        }
        return resultList;
    }

    public static SalixZone createFromTableEntry(final String dwspl, final String from, final String to) {
        return new SalixZone(Integer.valueOf(dwspl), Double.valueOf(from), Double.valueOf(to)); // Error-Handling?
    }

    private SalixZone(final int dwsplValue, final double fromKm, final double toKm) {
        this.dwsplValue = dwsplValue;
        this.fromKm = fromKm;
        this.toKm = toKm;
    }

    public Double getToKm() {
        return this.toKm;
    }

    public int getDwsplValue() {
        return this.dwsplValue;
    }

    public Double getFromKm() {
        return this.fromKm;
    }

    public static final String parseListToDataString(final List<SalixZone> list) {

        java.util.Collections.sort(list);
        final StringBuilder builder = new StringBuilder();
        for (final SalixZone zone : list) {
            builder.append(zone.getDwsplValue());
            builder.append(TABLE_CELL_SEPARATOR);
            builder.append(zone.getFromKm());
            builder.append(TABLE_CELL_SEPARATOR);
            builder.append(zone.getToKm());
            builder.append(TABLE_ROW_SEPARATOR);
        }
        return builder.toString();
    }

    public static final boolean zonesAreOverlapping(final List<SalixZone> list) {
        for (final SalixZone zone : list) {
            for (final SalixZone zoneOther : list) {
                if (zone != zoneOther) {
                    final boolean overlaps = zone.overlaps(zoneOther);
                    if (overlaps) {
                        return overlaps; // cancel. only one zone has to overlap
                    }
                }
            }
        }

        return false;
    }

    public static final boolean hasGaps(final List<SalixZone> list, final double lower, final double upper) {

        if (((upper - lower) > DELTA) && list.size() == 0)
            return true;

        final TreeSet<SalixZone> treeList = new TreeSet<SalixZone>();
        treeList.addAll(list);
        double lowerCompare = lower + DELTA;
        for (final SalixZone zone : treeList) {
            if ((zone.getUpperFromTo() - zone.getLowerFromTo()) > DELTA) {
                if (zone.getLowerFromTo() > lowerCompare) {
                    return true;
                }
                lowerCompare = zone.getUpperFromTo() + DELTA;
            }
        }
        if ((lowerCompare + DELTA) < upper)
            return true; // am Ende nicht geschlossen

        return false;
    }

    public Double getLowerFromTo() {
        return this.fromKm < this.toKm ? this.fromKm : this.toKm; // Math. is forbidden :-(
    }

    public Double getUpperFromTo() {
        return this.fromKm > this.toKm ? this.fromKm : this.toKm;// Math. is forbidden :-(
    }

    private boolean overlaps(final SalixZone otherZone) {
        final double otherLower = otherZone.getLowerFromTo();
        final double otherUpper = otherZone.getUpperFromTo();

        final double upper = getUpperFromTo();
        final double lower = getLowerFromTo();
        final double otherSchwerpunkt = (otherLower + otherUpper) / 2;
        if ((otherUpper < upper && otherUpper > lower)) {
            return true;
        } else if (otherLower > lower && otherLower < upper) {
            return true;
        } else if (otherSchwerpunkt > (lower - DELTA) && otherSchwerpunkt < (upper + DELTA)) {
            return true;
        }
        return false;
    }

    @Override
    public int compareTo(final SalixZone o) {
        final int basicCompare = this.getLowerFromTo().compareTo(o.getLowerFromTo());
        if (basicCompare == 0) {
            return 1; // necessary for the treeSet!
        }
        return basicCompare;
    }

    public static boolean isValidAnschlussRange(final double fromTest, final double toTest, final List<SalixZone> list, final double minKm) {
        final SalixZone zone = new SalixZone(0, fromTest, toTest);
        final double lower = zone.getLowerFromTo();
        final double anschluss = getAnschluss(list, minKm);
        final double differenceAbs = (lower - anschluss) > 0 ? (lower - anschluss) : (anschluss - lower); // no Math.abs allowed :-(
        if (differenceAbs > DELTA) {
            return false;
        }
        return true;
    }

    private static double getAnschluss(final List<SalixZone> list, final double minKm) {
        if (list.size() > 0) {
            return list.get(list.size() - 1).getUpperFromTo();
        }
        return minKm;
    }
}

http://dive4elements.wald.intevation.org