Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/SInfoResultType.java @ 8949:09e4a4909814
Distinguish between csv and pdf column headers
author | gernotbelger |
---|---|
date | Wed, 14 Mar 2018 18:34:34 +0100 |
parents | a4f1ac81f26d |
children | b0aeed4c97c1 |
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 // FIXME: use pgetPdfHeader instead of getCsvHeader |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
88 |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
89 meanBedHeight(null, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER, SInfoI18NStrings.CSV_MEAN_BED_HEIGHT_HEADER_SHORT) { |
8948 | 90 @Override |
91 public String exportValue(final CallContext context, final Object value) { | |
92 final double doubleValue = asDouble(value); | |
93 return exportDoubleValue(context, doubleValue); | |
94 } | |
95 | |
96 @Override | |
97 protected NumberFormat createFormatter(final CallContext context) { | |
98 return Formatter.getMeanBedHeight(context); | |
99 } | |
100 }, | |
101 | |
102 soundingLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_SOUNDING_HEADER) { | |
103 @Override | |
104 public String exportValue(final CallContext context, final Object value) { | |
105 return exportStringValue(value); | |
106 } | |
107 | |
108 @Override | |
109 protected NumberFormat createFormatter(final CallContext context) { | |
110 throw new UnsupportedOperationException(); | |
111 } | |
112 }, | |
113 | |
114 flowdepthmin(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MIN_HEADER) { | |
115 @Override | |
116 public String exportValue(final CallContext context, final Object value) { | |
117 final double doubleValue = asDouble(value); | |
118 return exportDoubleValue(context, doubleValue); | |
119 } | |
120 | |
121 @Override | |
122 protected NumberFormat createFormatter(final CallContext context) { | |
123 return Formatter.getFlowDepth(context); | |
124 } | |
125 }, | |
126 | |
127 flowdepthmax(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTH_MAX_HEADER) { | |
128 @Override | |
129 public String exportValue(final CallContext context, final Object value) { | |
130 final double doubleValue = asDouble(value); | |
131 return exportDoubleValue(context, doubleValue); | |
132 } | |
133 | |
134 @Override | |
135 protected NumberFormat createFormatter(final CallContext context) { | |
136 return Formatter.getFlowDepth(context); | |
137 } | |
138 }, | |
139 | |
140 gaugeLabel(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_GAUGE_HEADER) { | |
141 @Override | |
142 public String exportValue(final CallContext context, final Object value) { | |
143 return exportStringValue(value); | |
144 } | |
145 | |
146 @Override | |
147 protected NumberFormat createFormatter(final CallContext context) { | |
148 throw new UnsupportedOperationException(); | |
149 } | |
150 }, | |
151 | |
152 location(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_LOCATION_HEADER) { | |
153 @Override | |
154 public String exportValue(final CallContext context, final Object value) { | |
155 return exportStringValue(value); | |
156 } | |
157 | |
158 @Override | |
159 protected NumberFormat createFormatter(final CallContext context) { | |
160 throw new UnsupportedOperationException(); | |
161 } | |
162 }, | |
163 soilkind(SInfoI18NStrings.UNIT_NONE, SInfoI18NStrings.CSV_TKHKIND_HEADER) { | |
164 @Override | |
165 public String exportValue(final CallContext context, final Object value) { | |
166 | |
167 if (value == null) | |
168 return StringUtils.EMPTY; | |
169 | |
170 final SoilKind kind = (SoilKind) value; | |
171 | |
172 final String key = SInfoI18NStrings.PREFIX_TKH_KIND + kind.name(); | |
173 return Resources.getMsg(context.getMeta(), key, key); | |
174 } | |
175 | |
176 @Override | |
177 protected NumberFormat createFormatter(final CallContext context) { | |
178 throw new UnsupportedOperationException(); | |
179 } | |
180 }, | |
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 }, | |
193 d50(null, null) { | |
194 @Override | |
195 public String exportValue(final CallContext context, final Object value) { | |
196 final double doubleValue = asDouble(value); | |
197 return exportDoubleValue(context, doubleValue); | |
198 } | |
199 | |
200 @Override | |
201 protected NumberFormat createFormatter(final CallContext context) { | |
202 throw new UnsupportedOperationException(); | |
203 } | |
204 }, | |
205 velocity(null, null) { | |
206 @Override | |
207 public String exportValue(final CallContext context, final Object value) { | |
208 final double doubleValue = asDouble(value); | |
209 return exportDoubleValue(context, doubleValue); | |
210 } | |
211 | |
212 @Override | |
213 protected NumberFormat createFormatter(final CallContext context) { | |
214 throw new UnsupportedOperationException(); | |
215 } | |
216 }, | |
217 tau(null, null) { | |
218 @Override | |
219 public String exportValue(final CallContext context, final Object value) { | |
220 final double doubleValue = asDouble(value); | |
221 return exportDoubleValue(context, doubleValue); | |
222 } | |
223 | |
224 @Override | |
225 protected NumberFormat createFormatter(final CallContext context) { | |
226 throw new UnsupportedOperationException(); | |
227 } | |
228 }, | |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
229 tkh(SInfoI18NStrings.UNIT_CM, SInfoI18NStrings.CSV_TKH_HEADER, SInfoI18NStrings.CSV_TKH_HEADER_SHORT) { |
8948 | 230 @Override |
231 public String exportValue(final CallContext context, final Object value) { | |
232 final double doubleValue = asDouble(value); | |
233 return exportDoubleValue(context, doubleValue); | |
234 } | |
235 | |
236 @Override | |
237 protected NumberFormat createFormatter(final CallContext context) { | |
238 return Formatter.getTkh(context); | |
239 } | |
240 }, | |
241 tkhup(SInfoI18NStrings.UNIT_CM, null) { | |
242 @Override | |
243 public String exportValue(final CallContext context, final Object value) { | |
244 final double doubleValue = asDouble(value); | |
245 return exportDoubleValue(context, doubleValue); | |
246 } | |
247 | |
248 @Override | |
249 protected NumberFormat createFormatter(final CallContext context) { | |
250 throw new UnsupportedOperationException(); | |
251 } | |
252 }, | |
253 tkhdown(SInfoI18NStrings.UNIT_CM, null) { | |
254 @Override | |
255 public String exportValue(final CallContext context, final Object value) { | |
256 final double doubleValue = asDouble(value); | |
257 return exportDoubleValue(context, doubleValue); | |
258 } | |
259 | |
260 @Override | |
261 protected NumberFormat createFormatter(final CallContext context) { | |
262 throw new UnsupportedOperationException(); | |
263 } | |
264 }, | |
265 flowdepthtkh(SInfoI18NStrings.UNIT_M, SInfoI18NStrings.CSV_FLOWDEPTHTKH_HEADER) { | |
266 @Override | |
267 public String exportValue(final CallContext context, final Object value) { | |
268 final double doubleValue = asDouble(value); | |
269 return exportDoubleValue(context, doubleValue); | |
270 } | |
271 | |
272 @Override | |
273 protected NumberFormat createFormatter(final CallContext context) { | |
274 return Formatter.getFlowDepth(context); | |
275 } | |
276 }; | |
277 | |
278 /* Cache for formatters because Formatter will always create new formats (which is very expensive) */ | |
279 private final Map<Locale, NumberFormat> formatters = new HashMap<>(); | |
280 | |
281 private final String unit; | |
282 | |
283 private final String csvHeader; | |
284 | |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
285 private final String pdfHeader; |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
286 |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
287 private SInfoResultType(final String unit, final String csvAndPdfHeader) { |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
288 this(unit, csvAndPdfHeader, csvAndPdfHeader); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
289 } |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
290 |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
291 private SInfoResultType(final String unit, final String csvHeader, final String pdfHeader) { |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
292 this.unit = unit; |
8948 | 293 this.csvHeader = csvHeader; |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
294 this.pdfHeader = pdfHeader; |
8948 | 295 } |
296 | |
297 public abstract String exportValue(final CallContext context, final Object value); | |
298 | |
299 protected final String exportStringValue(final Object value) { | |
300 | |
301 if (value == null) | |
302 return StringUtils.EMPTY; | |
303 | |
304 if (!(value instanceof String)) | |
305 throw new IllegalStateException(); | |
306 | |
307 return (String) value; | |
308 } | |
309 | |
310 protected final double asDouble(final Object value) { | |
311 if (value == null) | |
312 return Double.NaN; | |
313 | |
314 if (!(value instanceof Number)) | |
315 throw new IllegalStateException(); | |
316 | |
317 final Number number = (Number) value; | |
318 return number.doubleValue(); | |
319 } | |
320 | |
321 protected final String exportDoubleValue(final CallContext context, final double value) { | |
322 if (Double.isNaN(value)) | |
323 return StringUtils.EMPTY; | |
324 | |
325 final NumberFormat formatter = getFormatter(context); | |
326 return formatter.format(value); | |
327 } | |
328 | |
329 private NumberFormat getFormatter(final CallContext context) { | |
330 final CallMeta meta = context.getMeta(); | |
331 final Locale locale = Resources.getLocale(meta); | |
332 | |
333 if (!this.formatters.containsKey(locale)) | |
334 this.formatters.put(locale, createFormatter(context)); | |
335 | |
336 return this.formatters.get(locale); | |
337 } | |
338 | |
339 protected abstract NumberFormat createFormatter(CallContext context); | |
340 | |
341 public final String getCsvHeader() { | |
342 return this.csvHeader; | |
343 } | |
344 | |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
345 public String getPdfHeader(final CallMeta meta) { |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
346 return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
347 } |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
348 |
8948 | 349 public final String getUnit() { |
350 return this.unit; | |
351 } | |
352 } |