Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/vegetationzones/VegetationZoneServerClientXChange.java @ 9403:e2da9c8a7c57
VegetationZone umbenennen
author | gernotbelger |
---|---|
date | Wed, 15 Aug 2018 13:22:00 +0200 |
parents | artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/vegetationzones/VegetationZone.java@e511eb935ccd |
children | 515643b2c49a |
comparison
equal
deleted
inserted
replaced
9402:e511eb935ccd | 9403:e2da9c8a7c57 |
---|---|
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.vegetationzones; | |
11 | |
12 import java.util.ArrayList; | |
13 import java.util.List; | |
14 import java.util.TreeSet; | |
15 | |
16 import org.dive4elements.artifacts.CallContext; | |
17 import org.dive4elements.river.artifacts.resources.Resources; | |
18 import org.dive4elements.river.model.River; | |
19 | |
20 /** | |
21 * @author Domenico Nardi Tironi | |
22 * | |
23 */ | |
24 public class VegetationZoneServerClientXChange implements Comparable<VegetationZoneServerClientXChange> { | |
25 | |
26 // IMMER ABGLEICHEN MIT VegetationZoneServerClientXChange.class Server und Client | |
27 public static final boolean HAS_COLORS_EDITABLE = false; | |
28 | |
29 private static final String TABLE_CELL_SEPARATOR = "TABLE_CELL_SEPARATOR"; | |
30 private static final String TABLE_ROW_SEPARATOR = "TABLE_ROW_SEPARATOR"; | |
31 private final String zoneName; | |
32 private final int min_day_overflow; | |
33 private final int max_day_overflow; | |
34 private final String hexColor; | |
35 | |
36 public static List<VegetationZoneServerClientXChange> parse(final String zonesRaw) { | |
37 final List<VegetationZoneServerClientXChange> resultList = new ArrayList<>(); | |
38 | |
39 final List<String[]> results = new ArrayList<>(); | |
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 | |
51 final VegetationZoneServerClientXChange helper = new VegetationZoneServerClientXChange(zone[0], Integer.valueOf(zone[1]), Integer.valueOf(zone[2]), zone[3]); | |
52 resultList.add(helper); | |
53 } | |
54 | |
55 return resultList; | |
56 } | |
57 | |
58 public static VegetationZoneServerClientXChange createFromTableEntry(final String zone, final String min_day_overflow, final String max_day_overflow, final String hexColor) { | |
59 return new VegetationZoneServerClientXChange(zone, Integer.valueOf(min_day_overflow), Integer.valueOf(max_day_overflow), hexColor); // Error-Handling? | |
60 } | |
61 | |
62 private VegetationZoneServerClientXChange(final String zone, final Integer min_day_overflow, final Integer max_day_overflow, final String hexColor) { | |
63 this.zoneName = zone; | |
64 this.min_day_overflow = min_day_overflow; | |
65 this.max_day_overflow = max_day_overflow; | |
66 this.hexColor = hexColor; | |
67 } | |
68 | |
69 public int getMax_day_overflow() { | |
70 return this.max_day_overflow; | |
71 } | |
72 | |
73 public String getZoneName() { | |
74 if (this.zoneName == null || this.zoneName.equals("")) { | |
75 return "---"; | |
76 } | |
77 return this.zoneName; | |
78 } | |
79 | |
80 public String getHexColor() { | |
81 try { | |
82 final int test = Integer.decode(this.hexColor); | |
83 return this.hexColor; | |
84 } | |
85 catch (final NumberFormatException e) { | |
86 return "#ffffff"; | |
87 } | |
88 } | |
89 | |
90 public int getMin_day_overflow() { | |
91 return this.min_day_overflow; | |
92 } | |
93 | |
94 public static final List<VegetationZoneServerClientXChange> getStandardList(final River river, final CallContext context) { | |
95 | |
96 final List<VegetationZoneServerClientXChange> list = new ArrayList<>(); | |
97 for (final org.dive4elements.river.model.uinfo.VegetationZone vz : org.dive4elements.river.model.uinfo.VegetationZone.getValues(river)) { | |
98 final String zn = Resources.getMsg(context.getMeta(), "uinfo_vegetation_type_" + vz.getVegetationType().getId().toString()); | |
99 list.add(new VegetationZoneServerClientXChange(zn, vz.getMin_overflow_days(), vz.getMax_overflow_days(), vz.getColor())); | |
100 } | |
101 return list; | |
102 } | |
103 | |
104 public static final String parseListToDataString(final List<VegetationZoneServerClientXChange> list) { | |
105 | |
106 java.util.Collections.sort(list); | |
107 final StringBuilder builder = new StringBuilder(); | |
108 for (final VegetationZoneServerClientXChange zone : list) { | |
109 builder.append(zone.getZoneName()); | |
110 builder.append(TABLE_CELL_SEPARATOR); | |
111 builder.append(zone.getMin_day_overflow()); | |
112 builder.append(TABLE_CELL_SEPARATOR); | |
113 builder.append(zone.getMax_day_overflow()); | |
114 builder.append(TABLE_CELL_SEPARATOR); | |
115 builder.append(zone.getHexColor()); | |
116 builder.append(TABLE_ROW_SEPARATOR); | |
117 } | |
118 return builder.toString(); | |
119 | |
120 } | |
121 | |
122 @Override | |
123 public int compareTo(final VegetationZoneServerClientXChange o) { | |
124 final int basicCompare = Integer.valueOf(this.getMin_day_overflow()).compareTo(o.getMin_day_overflow()); | |
125 if (basicCompare == 0) | |
126 return Integer.valueOf(this.getMax_day_overflow()).compareTo(o.getMax_day_overflow()); // wenn min==min && max==max, alphabetisch sortieren? | |
127 | |
128 if (basicCompare == 0) { | |
129 return 1; // for treeSet | |
130 } | |
131 | |
132 return basicCompare; | |
133 } | |
134 | |
135 public static final boolean zonesAreOverlapping(final List<VegetationZoneServerClientXChange> list) { | |
136 for (final VegetationZoneServerClientXChange zone : list) { | |
137 for (final VegetationZoneServerClientXChange zoneOther : list) { | |
138 if (zone != zoneOther) { | |
139 final boolean overlaps = zone.overlaps(zoneOther); | |
140 if (overlaps) { | |
141 return overlaps; // cancel. only one zone has to overlap | |
142 } | |
143 } | |
144 } | |
145 } | |
146 | |
147 return false; | |
148 } | |
149 | |
150 public static final boolean hasGaps(final List<VegetationZoneServerClientXChange> list, final int lower, final int upper) { | |
151 | |
152 if ((upper - lower) == 0) | |
153 return true; | |
154 | |
155 final TreeSet<VegetationZoneServerClientXChange> treeList = new TreeSet<>(); | |
156 treeList.addAll(list); | |
157 int lowerCompare = lower; | |
158 for (final VegetationZoneServerClientXChange zone : treeList) { | |
159 if (zone.getLowerFromTo() > (lowerCompare + 1)) { // nicht inklusiv | |
160 return true; | |
161 } | |
162 lowerCompare = zone.getUpperFromTo(); | |
163 } | |
164 if ((lowerCompare) < upper) | |
165 return true; // am Ende nicht geschlossen | |
166 | |
167 return false; | |
168 } | |
169 | |
170 private boolean overlaps(final VegetationZoneServerClientXChange otherZone) { | |
171 final int otherLower = otherZone.getLowerFromTo(); | |
172 final int otherUpper = otherZone.getUpperFromTo(); | |
173 | |
174 final int upper = getUpperFromTo(); | |
175 final int lower = getLowerFromTo(); | |
176 final int otherSchwerpunkt = (otherLower + otherUpper) / 2; | |
177 if ((otherUpper <= upper && otherUpper >= lower)) { | |
178 return true; | |
179 } else if (otherLower >= lower && otherLower <= upper) { | |
180 return true; | |
181 } else if (otherSchwerpunkt >= (lower) && otherSchwerpunkt <= (upper)) { | |
182 return true; | |
183 } | |
184 return false; | |
185 } | |
186 | |
187 public Integer getLowerFromTo() { | |
188 return this.min_day_overflow < this.max_day_overflow ? this.min_day_overflow : this.max_day_overflow; // Math. is forbidden :-( | |
189 } | |
190 | |
191 public Integer getUpperFromTo() { | |
192 return this.min_day_overflow > this.max_day_overflow ? this.min_day_overflow : this.max_day_overflow;// Math. is forbidden :-( | |
193 } | |
194 | |
195 } |