Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixZone.java @ 9237:972e10522ed6
salix.supraregional ui
author | gernotbelger |
---|---|
date | Tue, 10 Jul 2018 11:24:12 +0200 |
parents | |
children | 1c756bfee472 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/SalixZone.java Tue Jul 10 11:24:12 2018 +0200 @@ -0,0 +1,154 @@ +/** 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.uinfo.salix; + +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<>(); + + final List<String[]> results = new ArrayList<>(); + 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.getLowerFromTo() > lowerCompare) { + return true; + } + lowerCompare = zone.getUpperFromTo() + DELTA; + } + if ((lowerCompare + DELTA) < upper) + return true; // am Ende nicht geschlossen + + return false; + } + + private Double getLowerFromTo() { + return this.fromKm < this.toKm ? this.fromKm : this.toKm; // Math. is forbidden :-( + } + + private 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() - DELTA; + final double lower = getLowerFromTo() + DELTA; + 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 && otherSchwerpunkt < upper) { + return true; + } + return false; + } + + @Override + public int compareTo(final SalixZone o) { + final int basicCompare = this.getLowerFromTo().compareTo(o.getLowerFromTo()); + return basicCompare; + } + +} \ No newline at end of file