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