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