8948
|
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.sinfo.common; |
|
11 |
|
12 import java.text.NumberFormat; |
|
13 import java.util.HashMap; |
|
14 import java.util.Locale; |
|
15 import java.util.Map; |
|
16 |
|
17 import org.apache.commons.lang.StringUtils; |
|
18 import org.dive4elements.artifacts.CallContext; |
|
19 import org.dive4elements.artifacts.CallMeta; |
|
20 import org.dive4elements.river.artifacts.resources.Resources; |
|
21 import org.dive4elements.river.artifacts.sinfo.SInfoI18NStrings; |
|
22 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.SoilKind; |
|
23 import org.dive4elements.river.utils.Formatter; |
|
24 import org.dive4elements.river.utils.RiverUtils; |
|
25 |
|
26 /** |
|
27 * Result type for data that goes into {@link SInfoResultRow}s. |
|
28 * |
|
29 * @author Gernot Belger |
|
30 */ |
|
31 public enum SInfoResultType { |
|
32 |
|
33 station(SInfoI18NStrings.UNIT_KM, SInfoI18NStrings.CSV_KM_HEADER) { |
|
34 @Override |
|
35 public String exportValue(final CallContext context, final Object value) { |
|
36 final double doubleValue = asDouble(value); |
|
37 return exportDoubleValue(context, doubleValue); |
|
38 } |
|
39 |
|
40 @Override |
|
41 protected NumberFormat createFormatter(final CallContext context) { |
|
42 return Formatter.getWaterlevelKM(context); |
|
43 } |
|
44 }, |
|
45 |
|
46 waterlevel(null, SInfoI18NStrings.CSV_WATERLEVEL_HEADER) { |
|
47 @Override |
|
48 public String exportValue(final CallContext context, final Object value) { |
|
49 final double doubleValue = asDouble(value); |
|
50 return exportDoubleValue(context, doubleValue); |
|
51 } |
|
52 |
|
53 @Override |
|
54 protected NumberFormat createFormatter(final CallContext context) { |
|
55 // REMARK: other modules use Formatter.getWaterlevelW(context) instead, but that format with a variable number of |
|
56 // digits. |
|
57 return Formatter.getFlowDepth(context); |
|
58 } |
|
59 }, |
|
60 |
|
61 waterlevelLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_LABEL_HEADER) { |
|
62 @Override |
|
63 public String exportValue(final CallContext context, final Object value) { |
|
64 return exportStringValue(value); |
|
65 } |
|
66 |
|
67 @Override |
|
68 protected NumberFormat createFormatter(final CallContext context) { |
|
69 throw new UnsupportedOperationException(); |
|
70 } |
|
71 }, |
|
72 |
|
73 discharge(SInfoI18NStrings.UNIT_CUBIC_M, SInfoI18NStrings.CSV_DISCHARGE_HEADER) { |
|
74 @Override |
|
75 public String exportValue(final CallContext context, final Object value) { |
|
76 final double doubleValue = asDouble(value); |
|
77 final double roundedDischarge = RiverUtils.roundQ(doubleValue); |
|
78 return exportDoubleValue(context, roundedDischarge); |
|
79 } |
|
80 |
|
81 @Override |
|
82 protected NumberFormat createFormatter(final CallContext context) { |
|
83 return Formatter.getWaterlevelQ(context); |
|
84 } |
|
85 }, |
|
86 |
|
87 meanBedHeight(null, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER) { |
|
88 @Override |
|
89 public String exportValue(final CallContext context, final Object value) { |
|
90 final double doubleValue = asDouble(value); |
|
91 return exportDoubleValue(context, doubleValue); |
|
92 } |
|
93 |
|
94 @Override |
|
95 protected NumberFormat createFormatter(final CallContext context) { |
|
96 return Formatter.getMeanBedHeight(context); |
|
97 } |
|
98 }, |
|
99 |
|
100 soundingLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_SOUNDING_HEADER) { |
|
101 @Override |
|
102 public String exportValue(final CallContext context, final Object value) { |
|
103 return exportStringValue(value); |
|
104 } |
|
105 |
|
106 @Override |
|
107 protected NumberFormat createFormatter(final CallContext context) { |
|
108 throw new UnsupportedOperationException(); |
|
109 } |
|
110 }, |
|
111 |
|
112 flowdepthmin(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MIN_HEADER) { |
|
113 @Override |
|
114 public String exportValue(final CallContext context, final Object value) { |
|
115 final double doubleValue = asDouble(value); |
|
116 return exportDoubleValue(context, doubleValue); |
|
117 } |
|
118 |
|
119 @Override |
|
120 protected NumberFormat createFormatter(final CallContext context) { |
|
121 return Formatter.getFlowDepth(context); |
|
122 } |
|
123 }, |
|
124 |
|
125 flowdepthmax(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MAX_HEADER) { |
|
126 @Override |
|
127 public String exportValue(final CallContext context, final Object value) { |
|
128 final double doubleValue = asDouble(value); |
|
129 return exportDoubleValue(context, doubleValue); |
|
130 } |
|
131 |
|
132 @Override |
|
133 protected NumberFormat createFormatter(final CallContext context) { |
|
134 return Formatter.getFlowDepth(context); |
|
135 } |
|
136 }, |
|
137 |
|
138 gaugeLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_GAUGE_HEADER) { |
|
139 @Override |
|
140 public String exportValue(final CallContext context, final Object value) { |
|
141 return exportStringValue(value); |
|
142 } |
|
143 |
|
144 @Override |
|
145 protected NumberFormat createFormatter(final CallContext context) { |
|
146 throw new UnsupportedOperationException(); |
|
147 } |
|
148 }, |
|
149 |
|
150 location(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_LOCATION_HEADER) { |
|
151 @Override |
|
152 public String exportValue(final CallContext context, final Object value) { |
|
153 return exportStringValue(value); |
|
154 } |
|
155 |
|
156 @Override |
|
157 protected NumberFormat createFormatter(final CallContext context) { |
|
158 throw new UnsupportedOperationException(); |
|
159 } |
|
160 }, |
|
161 soilkind(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_TKHKIND_HEADER) { |
|
162 @Override |
|
163 public String exportValue(final CallContext context, final Object value) { |
|
164 |
|
165 if (value == null) |
|
166 return StringUtils.EMPTY; |
|
167 |
|
168 final SoilKind kind = (SoilKind) value; |
|
169 |
|
170 final String key = SInfoI18NStrings.PREFIX_TKH_KIND + kind.name(); |
|
171 return Resources.getMsg(context.getMeta(), key, key); |
|
172 } |
|
173 |
|
174 @Override |
|
175 protected NumberFormat createFormatter(final CallContext context) { |
|
176 throw new UnsupportedOperationException(); |
|
177 } |
|
178 }, |
|
179 flowdepth(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_HEADER) { |
|
180 @Override |
|
181 public String exportValue(final CallContext context, final Object value) { |
|
182 final double doubleValue = asDouble(value); |
|
183 return exportDoubleValue(context, doubleValue); |
|
184 } |
|
185 |
|
186 @Override |
|
187 protected NumberFormat createFormatter(final CallContext context) { |
|
188 return Formatter.getFlowDepth(context); |
|
189 } |
|
190 }, |
|
191 d50(null, null) { |
|
192 @Override |
|
193 public String exportValue(final CallContext context, final Object value) { |
|
194 final double doubleValue = asDouble(value); |
|
195 return exportDoubleValue(context, doubleValue); |
|
196 } |
|
197 |
|
198 @Override |
|
199 protected NumberFormat createFormatter(final CallContext context) { |
|
200 throw new UnsupportedOperationException(); |
|
201 } |
|
202 }, |
|
203 velocity(null, null) { |
|
204 @Override |
|
205 public String exportValue(final CallContext context, final Object value) { |
|
206 final double doubleValue = asDouble(value); |
|
207 return exportDoubleValue(context, doubleValue); |
|
208 } |
|
209 |
|
210 @Override |
|
211 protected NumberFormat createFormatter(final CallContext context) { |
|
212 throw new UnsupportedOperationException(); |
|
213 } |
|
214 }, |
|
215 tau(null, null) { |
|
216 @Override |
|
217 public String exportValue(final CallContext context, final Object value) { |
|
218 final double doubleValue = asDouble(value); |
|
219 return exportDoubleValue(context, doubleValue); |
|
220 } |
|
221 |
|
222 @Override |
|
223 protected NumberFormat createFormatter(final CallContext context) { |
|
224 throw new UnsupportedOperationException(); |
|
225 } |
|
226 }, |
|
227 tkh(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_TKH_HEADER) { |
|
228 @Override |
|
229 public String exportValue(final CallContext context, final Object value) { |
|
230 final double doubleValue = asDouble(value); |
|
231 return exportDoubleValue(context, doubleValue); |
|
232 } |
|
233 |
|
234 @Override |
|
235 protected NumberFormat createFormatter(final CallContext context) { |
|
236 return Formatter.getTkh(context); |
|
237 } |
|
238 }, |
|
239 tkhup(SInfoI18NStrings.UNIT_CM, null) { |
|
240 @Override |
|
241 public String exportValue(final CallContext context, final Object value) { |
|
242 final double doubleValue = asDouble(value); |
|
243 return exportDoubleValue(context, doubleValue); |
|
244 } |
|
245 |
|
246 @Override |
|
247 protected NumberFormat createFormatter(final CallContext context) { |
|
248 throw new UnsupportedOperationException(); |
|
249 } |
|
250 }, |
|
251 tkhdown(SInfoI18NStrings.UNIT_CM, null) { |
|
252 @Override |
|
253 public String exportValue(final CallContext context, final Object value) { |
|
254 final double doubleValue = asDouble(value); |
|
255 return exportDoubleValue(context, doubleValue); |
|
256 } |
|
257 |
|
258 @Override |
|
259 protected NumberFormat createFormatter(final CallContext context) { |
|
260 throw new UnsupportedOperationException(); |
|
261 } |
|
262 }, |
|
263 flowdepthtkh(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTHTKH_HEADER) { |
|
264 @Override |
|
265 public String exportValue(final CallContext context, final Object value) { |
|
266 final double doubleValue = asDouble(value); |
|
267 return exportDoubleValue(context, doubleValue); |
|
268 } |
|
269 |
|
270 @Override |
|
271 protected NumberFormat createFormatter(final CallContext context) { |
|
272 return Formatter.getFlowDepth(context); |
|
273 } |
|
274 }; |
|
275 |
|
276 /* Cache for formatters because Formatter will always create new formats (which is very expensive) */ |
|
277 private final Map<Locale, NumberFormat> formatters = new HashMap<>(); |
|
278 |
|
279 private final String unit; |
|
280 |
|
281 private final String csvHeader; |
|
282 |
|
283 private SInfoResultType(final String untit, final String csvHeader) { |
|
284 this.unit = untit; |
|
285 this.csvHeader = csvHeader; |
|
286 } |
|
287 |
|
288 public abstract String exportValue(final CallContext context, final Object value); |
|
289 |
|
290 protected final String exportStringValue(final Object value) { |
|
291 |
|
292 if (value == null) |
|
293 return StringUtils.EMPTY; |
|
294 |
|
295 if (!(value instanceof String)) |
|
296 throw new IllegalStateException(); |
|
297 |
|
298 return (String) value; |
|
299 } |
|
300 |
|
301 protected final double asDouble(final Object value) { |
|
302 if (value == null) |
|
303 return Double.NaN; |
|
304 |
|
305 if (!(value instanceof Number)) |
|
306 throw new IllegalStateException(); |
|
307 |
|
308 final Number number = (Number) value; |
|
309 return number.doubleValue(); |
|
310 } |
|
311 |
|
312 protected final String exportDoubleValue(final CallContext context, final double value) { |
|
313 if (Double.isNaN(value)) |
|
314 return StringUtils.EMPTY; |
|
315 |
|
316 final NumberFormat formatter = getFormatter(context); |
|
317 return formatter.format(value); |
|
318 } |
|
319 |
|
320 private NumberFormat getFormatter(final CallContext context) { |
|
321 final CallMeta meta = context.getMeta(); |
|
322 final Locale locale = Resources.getLocale(meta); |
|
323 |
|
324 if (!this.formatters.containsKey(locale)) |
|
325 this.formatters.put(locale, createFormatter(context)); |
|
326 |
|
327 return this.formatters.get(locale); |
|
328 } |
|
329 |
|
330 protected abstract NumberFormat createFormatter(CallContext context); |
|
331 |
|
332 public final String getCsvHeader() { |
|
333 return this.csvHeader; |
|
334 } |
|
335 |
|
336 public final String getUnit() { |
|
337 return this.unit; |
|
338 } |
|
339 } |