9119
|
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; |
9260
|
15 import java.util.TreeSet; |
9119
|
16 |
|
17 /** |
|
18 * @author Domenico Nardi Tironi |
|
19 * |
|
20 */ |
9403
|
21 public class VegetationZoneServerClientXChange implements Comparable<VegetationZoneServerClientXChange> { |
9119
|
22 |
9403
|
23 // IMMER ABGLEICHEN MIT VegetationZoneServerClientXChange.class IM SERVER |
9260
|
24 public static final boolean HAS_COLORS_EDITABLE = false; |
|
25 |
9119
|
26 private static final String TABLE_CELL_SEPARATOR = "TABLE_CELL_SEPARATOR"; |
|
27 private static final String TABLE_ROW_SEPARATOR = "TABLE_ROW_SEPARATOR"; |
|
28 private final String zoneName; |
|
29 private final int min_day_overflow; |
|
30 private final int max_day_overflow; |
9260
|
31 private final String hexColor; |
9119
|
32 |
9403
|
33 public static List<VegetationZoneServerClientXChange> parse(final String zonesRaw) { |
|
34 final List<VegetationZoneServerClientXChange> resultList = new ArrayList<VegetationZoneServerClientXChange>(); |
9119
|
35 |
|
36 final List<String[]> results = new ArrayList<String[]>(); |
|
37 if (zonesRaw.contains(TABLE_ROW_SEPARATOR)) { |
|
38 final String[] rows = zonesRaw.split(TABLE_ROW_SEPARATOR); |
|
39 for (final String row : rows) { |
|
40 if (row.contains(TABLE_CELL_SEPARATOR)) { |
|
41 final String[] result = row.split(TABLE_CELL_SEPARATOR); |
|
42 results.add(result); |
|
43 } |
|
44 } |
|
45 } |
|
46 for (final String[] zone : results) { |
|
47 |
9499
|
48 final VegetationZoneServerClientXChange helper = new VegetationZoneServerClientXChange(zone[0], Integer.valueOf(zone[1]), Integer.valueOf(zone[2]), |
|
49 zone[3]); |
9119
|
50 resultList.add(helper); |
|
51 } |
|
52 |
|
53 return resultList; |
|
54 } |
|
55 |
9499
|
56 public static VegetationZoneServerClientXChange createFromTableEntry(final String zone, final String min_day_overflow, final String max_day_overflow, |
|
57 final String hexColor) { |
9403
|
58 return new VegetationZoneServerClientXChange(zone, Integer.valueOf(min_day_overflow), Integer.valueOf(max_day_overflow), hexColor); // Error-Handling? |
9119
|
59 } |
|
60 |
9403
|
61 private VegetationZoneServerClientXChange(final String zone, final Integer min_day_overflow, final Integer max_day_overflow, final String hexColor) { |
9119
|
62 this.zoneName = zone; |
|
63 this.min_day_overflow = min_day_overflow; |
|
64 this.max_day_overflow = max_day_overflow; |
9260
|
65 this.hexColor = hexColor; |
9119
|
66 } |
|
67 |
|
68 public int getMax_day_overflow() { |
|
69 return this.max_day_overflow; |
|
70 } |
|
71 |
|
72 public String getZoneName() { |
9260
|
73 if (this.zoneName == null || this.zoneName.equals("")) { |
|
74 return "---"; |
|
75 } |
9119
|
76 return this.zoneName; |
|
77 } |
|
78 |
9260
|
79 public String getHexColor() { |
|
80 try { |
|
81 final int test = Integer.decode(this.hexColor); |
|
82 return this.hexColor; |
|
83 } |
|
84 catch (final NumberFormatException e) { |
|
85 return "#ffffff"; |
|
86 } |
|
87 } |
|
88 |
9119
|
89 public int getMin_day_overflow() { |
|
90 return this.min_day_overflow; |
|
91 } |
|
92 |
9403
|
93 public static final List<VegetationZoneServerClientXChange> getStandardList() { |
9119
|
94 |
9403
|
95 final List<VegetationZoneServerClientXChange> list = new ArrayList<VegetationZoneServerClientXChange>(); |
|
96 list.add(new VegetationZoneServerClientXChange("Zonaler Wald", 0, 5, "#336600")); |
|
97 list.add(new VegetationZoneServerClientXChange("Hartholzaue, trocken", 6, 40, "#00cc00")); |
|
98 list.add(new VegetationZoneServerClientXChange("Hartholzaue, feucht", 41, 80, "#66ff33")); |
|
99 list.add(new VegetationZoneServerClientXChange("Silberweidenwald", 81, 140, "#008080")); |
|
100 list.add(new VegetationZoneServerClientXChange("Weidengebüsch", 141, 200, "#33cccc")); |
|
101 list.add(new VegetationZoneServerClientXChange("Uferröhricht", 201, 260, "#ffa8ff")); |
|
102 list.add(new VegetationZoneServerClientXChange("Uferpioniere", 261, 320, "#ff0000")); |
|
103 list.add(new VegetationZoneServerClientXChange("Vegetationslos", 321, 364, "#b2b2b2")); |
|
104 list.add(new VegetationZoneServerClientXChange("Wasserfläche", 365, 365, "#0066ff")); |
9119
|
105 |
|
106 return list; |
|
107 } |
|
108 |
9403
|
109 public static final String parseListToDataString(final List<VegetationZoneServerClientXChange> list) { |
9119
|
110 |
|
111 java.util.Collections.sort(list); |
|
112 final StringBuilder builder = new StringBuilder(); |
9403
|
113 for (final VegetationZoneServerClientXChange zone : list) { |
9119
|
114 builder.append(zone.getZoneName()); |
|
115 builder.append(TABLE_CELL_SEPARATOR); |
|
116 builder.append(zone.getMin_day_overflow()); |
|
117 builder.append(TABLE_CELL_SEPARATOR); |
|
118 builder.append(zone.getMax_day_overflow()); |
9260
|
119 builder.append(TABLE_CELL_SEPARATOR); |
|
120 builder.append(zone.getHexColor()); |
9119
|
121 builder.append(TABLE_ROW_SEPARATOR); |
|
122 } |
|
123 return builder.toString(); |
|
124 |
|
125 } |
|
126 |
|
127 @Override |
9403
|
128 public int compareTo(final VegetationZoneServerClientXChange o) { |
9119
|
129 final int basicCompare = Integer.valueOf(this.getMin_day_overflow()).compareTo(o.getMin_day_overflow()); |
|
130 if (basicCompare == 0) |
|
131 return Integer.valueOf(this.getMax_day_overflow()).compareTo(o.getMax_day_overflow()); // wenn min==min && max==max, alphabetisch sortieren? |
9260
|
132 |
|
133 if (basicCompare == 0) { |
|
134 return 1; // for treeSet |
|
135 } |
|
136 |
9119
|
137 return basicCompare; |
|
138 } |
|
139 |
9403
|
140 public static final boolean zonesAreOverlapping(final List<VegetationZoneServerClientXChange> list) { |
|
141 for (final VegetationZoneServerClientXChange zone : list) { |
|
142 for (final VegetationZoneServerClientXChange zoneOther : list) { |
9260
|
143 if (zone != zoneOther) { |
|
144 final boolean overlaps = zone.overlaps(zoneOther); |
|
145 if (overlaps) { |
|
146 return overlaps; // cancel. only one zone has to overlap |
|
147 } |
|
148 } |
|
149 } |
|
150 } |
|
151 |
|
152 return false; |
|
153 } |
|
154 |
9403
|
155 public static final boolean hasGaps(final List<VegetationZoneServerClientXChange> list, final int lower, final int upper) { |
9260
|
156 |
|
157 if ((upper - lower) == 0) |
|
158 return true; |
|
159 |
9403
|
160 final TreeSet<VegetationZoneServerClientXChange> treeList = new TreeSet<VegetationZoneServerClientXChange>(); |
9260
|
161 treeList.addAll(list); |
|
162 int lowerCompare = lower; |
9403
|
163 for (final VegetationZoneServerClientXChange zone : treeList) { |
9499
|
164 if (zone.getLowerFromTo() > (lowerCompare)) { // nicht inklusiv |
9260
|
165 return true; |
|
166 } |
|
167 lowerCompare = zone.getUpperFromTo(); |
|
168 } |
|
169 if ((lowerCompare) < upper) |
|
170 return true; // am Ende nicht geschlossen |
|
171 |
|
172 return false; |
|
173 } |
|
174 |
9403
|
175 private boolean overlaps(final VegetationZoneServerClientXChange otherZone) { |
9260
|
176 final int otherLower = otherZone.getLowerFromTo(); |
|
177 final int otherUpper = otherZone.getUpperFromTo(); |
|
178 |
|
179 final int upper = getUpperFromTo(); |
|
180 final int lower = getLowerFromTo(); |
9499
|
181 // final int otherSchwerpunkt = (otherLower + otherUpper) / 2; |
|
182 if ((otherUpper <= upper && otherUpper > lower)) { |
9260
|
183 return true; |
9499
|
184 } else if (otherLower >= lower && otherLower < upper) { |
9260
|
185 return true; |
9499
|
186 } else if (otherLower == lower && otherUpper == upper) { |
9260
|
187 return true; |
|
188 } |
|
189 return false; |
|
190 } |
|
191 |
|
192 public Integer getLowerFromTo() { |
|
193 return this.min_day_overflow < this.max_day_overflow ? this.min_day_overflow : this.max_day_overflow; // Math. is forbidden :-( |
|
194 } |
|
195 |
|
196 public Integer getUpperFromTo() { |
|
197 return this.min_day_overflow > this.max_day_overflow ? this.min_day_overflow : this.max_day_overflow;// Math. is forbidden :-( |
|
198 } |
|
199 |
|
200 } |