9237
|
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.uinfo.salix; |
|
11 |
|
12 import java.util.ArrayList; |
|
13 import java.util.List; |
|
14 import java.util.TreeSet; |
|
15 |
|
16 /** |
|
17 * @author Domenico Nardi Tironi |
|
18 * |
|
19 */ |
|
20 public class SalixZone implements Comparable<SalixZone> { |
|
21 |
|
22 // IMMER ABGLEICHEN Server Client SalixZone.java |
|
23 private static final String TABLE_CELL_SEPARATOR = "TABLE_CELL_SEPARATOR"; |
|
24 private static final String TABLE_ROW_SEPARATOR = "TABLE_ROW_SEPARATOR"; |
|
25 private final int dwsplValue; |
|
26 private final double fromKm; |
|
27 private final double toKm; |
|
28 |
|
29 private final static double DELTA = 0.0001; |
|
30 |
|
31 public static List<SalixZone> parse(final String zonesRaw) { |
|
32 final List<SalixZone> resultList = new ArrayList<>(); |
|
33 |
|
34 final List<String[]> results = new ArrayList<>(); |
|
35 if (zonesRaw.contains(TABLE_ROW_SEPARATOR)) { |
|
36 final String[] rows = zonesRaw.split(TABLE_ROW_SEPARATOR); |
|
37 for (final String row : rows) { |
|
38 if (row.contains(TABLE_CELL_SEPARATOR)) { |
|
39 final String[] result = row.split(TABLE_CELL_SEPARATOR); |
|
40 results.add(result); |
|
41 } |
|
42 } |
|
43 } |
|
44 for (final String[] zone : results) { |
|
45 final SalixZone helper = new SalixZone(Integer.valueOf(zone[0]), Double.valueOf(zone[1]), Double.valueOf(zone[2])); |
|
46 resultList.add(helper); |
|
47 } |
|
48 return resultList; |
|
49 } |
|
50 |
|
51 public static SalixZone createFromTableEntry(final String dwspl, final String from, final String to) { |
|
52 return new SalixZone(Integer.valueOf(dwspl), Double.valueOf(from), Double.valueOf(to)); // Error-Handling? |
|
53 } |
|
54 |
|
55 private SalixZone(final int dwsplValue, final double fromKm, final double toKm) { |
|
56 this.dwsplValue = dwsplValue; |
|
57 this.fromKm = fromKm; |
|
58 this.toKm = toKm; |
|
59 } |
|
60 |
|
61 public Double getToKm() { |
|
62 return this.toKm; |
|
63 } |
|
64 |
|
65 public int getDwsplValue() { |
|
66 return this.dwsplValue; |
|
67 } |
|
68 |
|
69 public Double getFromKm() { |
|
70 return this.fromKm; |
|
71 } |
|
72 |
|
73 public static final String parseListToDataString(final List<SalixZone> list) { |
|
74 |
|
75 java.util.Collections.sort(list); |
|
76 final StringBuilder builder = new StringBuilder(); |
|
77 for (final SalixZone zone : list) { |
|
78 builder.append(zone.getDwsplValue()); |
|
79 builder.append(TABLE_CELL_SEPARATOR); |
|
80 builder.append(zone.getFromKm()); |
|
81 builder.append(TABLE_CELL_SEPARATOR); |
|
82 builder.append(zone.getToKm()); |
|
83 builder.append(TABLE_ROW_SEPARATOR); |
|
84 } |
|
85 return builder.toString(); |
|
86 } |
|
87 |
|
88 public static final boolean zonesAreOverlapping(final List<SalixZone> list) { |
|
89 for (final SalixZone zone : list) { |
|
90 for (final SalixZone zoneOther : list) { |
|
91 if (zone != zoneOther) { |
|
92 final boolean overlaps = zone.overlaps(zoneOther); |
|
93 if (overlaps) { |
|
94 return overlaps; // cancel. only one zone has to overlap |
|
95 } |
|
96 } |
|
97 } |
|
98 } |
|
99 |
|
100 return false; |
|
101 } |
|
102 |
|
103 public static final boolean hasGaps(final List<SalixZone> list, final double lower, final double upper) { |
|
104 |
|
105 if (((upper - lower) > DELTA) && list.size() == 0) |
|
106 return true; |
|
107 |
|
108 final TreeSet<SalixZone> treeList = new TreeSet<SalixZone>(); |
|
109 treeList.addAll(list); |
|
110 double lowerCompare = lower + DELTA; |
|
111 for (final SalixZone zone : treeList) { |
|
112 if (zone.getLowerFromTo() > lowerCompare) { |
|
113 return true; |
|
114 } |
|
115 lowerCompare = zone.getUpperFromTo() + DELTA; |
|
116 } |
|
117 if ((lowerCompare + DELTA) < upper) |
|
118 return true; // am Ende nicht geschlossen |
|
119 |
|
120 return false; |
|
121 } |
|
122 |
|
123 private Double getLowerFromTo() { |
|
124 return this.fromKm < this.toKm ? this.fromKm : this.toKm; // Math. is forbidden :-( |
|
125 } |
|
126 |
|
127 private Double getUpperFromTo() { |
|
128 return this.fromKm > this.toKm ? this.fromKm : this.toKm;// Math. is forbidden :-( |
|
129 } |
|
130 |
|
131 private boolean overlaps(final SalixZone otherZone) { |
|
132 final double otherLower = otherZone.getLowerFromTo(); |
|
133 final double otherUpper = otherZone.getUpperFromTo(); |
|
134 |
|
135 final double upper = getUpperFromTo() - DELTA; |
|
136 final double lower = getLowerFromTo() + DELTA; |
|
137 final double otherSchwerpunkt = (otherLower + otherUpper) / 2; |
|
138 if ((otherUpper < upper && otherUpper > lower)) { |
|
139 return true; |
|
140 } else if (otherLower > lower && otherLower < upper) { |
|
141 return true; |
|
142 } else if (otherSchwerpunkt > lower && otherSchwerpunkt < upper) { |
|
143 return true; |
|
144 } |
|
145 return false; |
|
146 } |
|
147 |
|
148 @Override |
|
149 public int compareTo(final SalixZone o) { |
|
150 final int basicCompare = this.getLowerFromTo().compareTo(o.getLowerFromTo()); |
|
151 return basicCompare; |
|
152 } |
|
153 |
|
154 } |