Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java @ 8951:322b0e6298ea
Work on SINFO FlowDepth-Development
author | gernotbelger |
---|---|
date | Fri, 16 Mar 2018 18:08:38 +0100 |
parents | b0aeed4c97c1 |
children | 7c1611b5a59e |
rev | line source |
---|---|
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 | |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
87 meanBedHeight(null, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER_SHORT) { |
8948 | 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 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
161 |
8948 | 162 soilkind(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_TKHKIND_HEADER) { |
163 @Override | |
164 public String exportValue(final CallContext context, final Object value) { | |
165 | |
166 if (value == null) | |
167 return StringUtils.EMPTY; | |
168 | |
169 final SoilKind kind = (SoilKind) value; | |
170 | |
171 final String key = SInfoI18NStrings.PREFIX_TKH_KIND + kind.name(); | |
172 return Resources.getMsg(context.getMeta(), key, key); | |
173 } | |
174 | |
175 @Override | |
176 protected NumberFormat createFormatter(final CallContext context) { | |
177 throw new UnsupportedOperationException(); | |
178 } | |
179 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
180 |
8948 | 181 flowdepth(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_HEADER) { |
182 @Override | |
183 public String exportValue(final CallContext context, final Object value) { | |
184 final double doubleValue = asDouble(value); | |
185 return exportDoubleValue(context, doubleValue); | |
186 } | |
187 | |
188 @Override | |
189 protected NumberFormat createFormatter(final CallContext context) { | |
190 return Formatter.getFlowDepth(context); | |
191 } | |
192 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
193 |
8948 | 194 d50(null, null) { |
195 @Override | |
196 public String exportValue(final CallContext context, final Object value) { | |
197 final double doubleValue = asDouble(value); | |
198 return exportDoubleValue(context, doubleValue); | |
199 } | |
200 | |
201 @Override | |
202 protected NumberFormat createFormatter(final CallContext context) { | |
203 throw new UnsupportedOperationException(); | |
204 } | |
205 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
206 |
8948 | 207 velocity(null, null) { |
208 @Override | |
209 public String exportValue(final CallContext context, final Object value) { | |
210 final double doubleValue = asDouble(value); | |
211 return exportDoubleValue(context, doubleValue); | |
212 } | |
213 | |
214 @Override | |
215 protected NumberFormat createFormatter(final CallContext context) { | |
216 throw new UnsupportedOperationException(); | |
217 } | |
218 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
219 |
8948 | 220 tau(null, null) { |
221 @Override | |
222 public String exportValue(final CallContext context, final Object value) { | |
223 final double doubleValue = asDouble(value); | |
224 return exportDoubleValue(context, doubleValue); | |
225 } | |
226 | |
227 @Override | |
228 protected NumberFormat createFormatter(final CallContext context) { | |
229 throw new UnsupportedOperationException(); | |
230 } | |
231 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
232 |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
233 tkh(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_TKH_HEADER, SInfoI18NStrings.CSV_TKH_HEADER_SHORT) { |
8948 | 234 @Override |
235 public String exportValue(final CallContext context, final Object value) { | |
236 final double doubleValue = asDouble(value); | |
237 return exportDoubleValue(context, doubleValue); | |
238 } | |
239 | |
240 @Override | |
241 protected NumberFormat createFormatter(final CallContext context) { | |
242 return Formatter.getTkh(context); | |
243 } | |
244 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
245 |
8948 | 246 tkhup(SInfoI18NStrings.UNIT_CM, null) { |
247 @Override | |
248 public String exportValue(final CallContext context, final Object value) { | |
249 final double doubleValue = asDouble(value); | |
250 return exportDoubleValue(context, doubleValue); | |
251 } | |
252 | |
253 @Override | |
254 protected NumberFormat createFormatter(final CallContext context) { | |
255 throw new UnsupportedOperationException(); | |
256 } | |
257 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
258 |
8948 | 259 tkhdown(SInfoI18NStrings.UNIT_CM, null) { |
260 @Override | |
261 public String exportValue(final CallContext context, final Object value) { | |
262 final double doubleValue = asDouble(value); | |
263 return exportDoubleValue(context, doubleValue); | |
264 } | |
265 | |
266 @Override | |
267 protected NumberFormat createFormatter(final CallContext context) { | |
268 throw new UnsupportedOperationException(); | |
269 } | |
270 }, | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
271 |
8948 | 272 flowdepthtkh(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTHTKH_HEADER) { |
273 @Override | |
274 public String exportValue(final CallContext context, final Object value) { | |
275 final double doubleValue = asDouble(value); | |
276 return exportDoubleValue(context, doubleValue); | |
277 } | |
278 | |
279 @Override | |
280 protected NumberFormat createFormatter(final CallContext context) { | |
281 return Formatter.getFlowDepth(context); | |
282 } | |
8951 | 283 }, |
284 flowdepthDevelopment(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_FLOWDEPTH_DEVELOPMENT_HEADER) { | |
285 @Override | |
286 public String exportValue(final CallContext context, final Object value) { | |
287 final double doubleValue = asDouble(value); | |
288 return exportDoubleValue(context, doubleValue); | |
289 } | |
290 | |
291 @Override | |
292 protected NumberFormat createFormatter(final CallContext context) { | |
293 return Formatter.getTkh(context); | |
294 } | |
295 }, | |
296 flowdepthDevelopmentPerYear(SInfoI18NStrings.UNIT_CM_A, SInfoI18NStrings.CSV_FLOWDEPTH_DEVELOPMENT_PER_YEAR_HEADER) { | |
297 @Override | |
298 public String exportValue(final CallContext context, final Object value) { | |
299 final double doubleValue = asDouble(value); | |
300 return exportDoubleValue(context, doubleValue); | |
301 } | |
302 | |
303 @Override | |
304 protected NumberFormat createFormatter(final CallContext context) { | |
305 return Formatter.getFlowDepthDevelopmentPerYear(context); | |
306 } | |
307 }, | |
308 waterlevelDifference(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_WATERLEVEL_DIFFERENCE_HEADER) { | |
309 @Override | |
310 public String exportValue(final CallContext context, final Object value) { | |
311 final double doubleValue = asDouble(value); | |
312 return exportDoubleValue(context, doubleValue); | |
313 } | |
314 | |
315 @Override | |
316 protected NumberFormat createFormatter(final CallContext context) { | |
317 return Formatter.getTkh(context); | |
318 } | |
319 }, | |
320 bedHeightDifference(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_DIFFERENCE_HEADER) { | |
321 @Override | |
322 public String exportValue(final CallContext context, final Object value) { | |
323 final double doubleValue = asDouble(value); | |
324 return exportDoubleValue(context, doubleValue); | |
325 } | |
326 | |
327 @Override | |
328 protected NumberFormat createFormatter(final CallContext context) { | |
329 return Formatter.getTkh(context); | |
330 } | |
331 }, | |
332 flowdepthCurrent(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_CURRENT_HEADER) { | |
333 @Override | |
334 public String exportValue(final CallContext context, final Object value) { | |
335 final double doubleValue = asDouble(value); | |
336 return exportDoubleValue(context, doubleValue); | |
337 } | |
338 | |
339 @Override | |
340 protected NumberFormat createFormatter(final CallContext context) { | |
341 return Formatter.getFlowDepth(context); | |
342 } | |
343 }, | |
344 flowdepthHistorical(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_HISTORICAL_HEADER) { | |
345 @Override | |
346 public String exportValue(final CallContext context, final Object value) { | |
347 final double doubleValue = asDouble(value); | |
348 return exportDoubleValue(context, doubleValue); | |
349 } | |
350 | |
351 @Override | |
352 protected NumberFormat createFormatter(final CallContext context) { | |
353 return Formatter.getFlowDepth(context); | |
354 } | |
8948 | 355 }; |
356 | |
357 /* Cache for formatters because Formatter will always create new formats (which is very expensive) */ | |
358 private final Map<Locale, NumberFormat> formatters = new HashMap<>(); | |
359 | |
360 private final String unit; | |
361 | |
362 private final String csvHeader; | |
363 | |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
364 private final String pdfHeader; |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
365 |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
366 private SInfoResultType(final String unit, final String csvAndPdfHeader) { |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
367 this(unit, csvAndPdfHeader, csvAndPdfHeader); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
368 } |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
369 |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
370 private SInfoResultType(final String unit, final String csvHeader, final String pdfHeader) { |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
371 this.unit = unit; |
8948 | 372 this.csvHeader = csvHeader; |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
373 this.pdfHeader = pdfHeader; |
8948 | 374 } |
375 | |
376 public abstract String exportValue(final CallContext context, final Object value); | |
377 | |
378 protected final String exportStringValue(final Object value) { | |
379 | |
380 if (value == null) | |
381 return StringUtils.EMPTY; | |
382 | |
383 if (!(value instanceof String)) | |
384 throw new IllegalStateException(); | |
385 | |
386 return (String) value; | |
387 } | |
388 | |
389 protected final double asDouble(final Object value) { | |
390 if (value == null) | |
391 return Double.NaN; | |
392 | |
393 if (!(value instanceof Number)) | |
394 throw new IllegalStateException(); | |
395 | |
396 final Number number = (Number) value; | |
397 return number.doubleValue(); | |
398 } | |
399 | |
400 protected final String exportDoubleValue(final CallContext context, final double value) { | |
401 if (Double.isNaN(value)) | |
402 return StringUtils.EMPTY; | |
403 | |
404 final NumberFormat formatter = getFormatter(context); | |
405 return formatter.format(value); | |
406 } | |
407 | |
408 private NumberFormat getFormatter(final CallContext context) { | |
409 final CallMeta meta = context.getMeta(); | |
410 final Locale locale = Resources.getLocale(meta); | |
411 | |
412 if (!this.formatters.containsKey(locale)) | |
413 this.formatters.put(locale, createFormatter(context)); | |
414 | |
415 return this.formatters.get(locale); | |
416 } | |
417 | |
418 protected abstract NumberFormat createFormatter(CallContext context); | |
419 | |
420 public final String getCsvHeader() { | |
421 return this.csvHeader; | |
422 } | |
423 | |
8950
b0aeed4c97c1
Implemented chart output for sinfo flow depth min/max calculation
gernotbelger
parents:
8949
diff
changeset
|
424 public final String getPdfHeader(final CallMeta meta) { |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
425 return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
426 } |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
427 |
8948 | 428 public final String getUnit() { |
429 return this.unit; | |
430 } | |
431 } |