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