Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/Formatter.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 0336132ec9db |
children | 04309ca24614 |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.utils; | |
2 | |
3 import de.intevation.artifacts.CallContext; | |
4 import de.intevation.artifacts.CallMeta; | |
5 | |
6 import de.intevation.flys.artifacts.resources.Resources; | |
7 | |
8 import java.text.DateFormat; | |
9 import java.text.NumberFormat; | |
10 import java.text.SimpleDateFormat; | |
11 | |
12 import java.util.Locale; | |
13 | |
14 | |
15 public final class Formatter { | |
16 | |
17 // KMS IN ERROR REPORTS. | |
18 public static final int CALCULATION_REPORT_KM_MIN_DIGITS = 1; | |
19 public static final int CALCULATION_REPORT_KM_MAX_DIGITS = 3; | |
20 | |
21 // WATERLEVEL FORMATTER CONSTANTS | |
22 public static final int WATERLEVEL_KM_MIN_DIGITS = 3; | |
23 public static final int WATERLEVEL_KM_MAX_DIGITS = 3; | |
24 public static final int WATERLEVEL_W_MIN_DIGITS = 0; | |
25 public static final int WATERLEVEL_W_MAX_DIGITS = 2; | |
26 public static final int WATERLEVEL_Q_MIN_DIGITS = 0; | |
27 public static final int WATERLEVEL_Q_MAX_DIGITS = 2; | |
28 | |
29 | |
30 // COMPUTED DISCHARGE CURVE FORMATTER CONSTANTS | |
31 public static final int COMPUTED_DISCHARGE_W_MIN_DIGITS = 2; | |
32 public static final int COMPUTED_DISCHARGE_W_MAX_DIGITS = 2; | |
33 public static final int COMPUTED_DISCHARGE_Q_MIN_DIGITS = 0; | |
34 public static final int COMPUTED_DISCHARGE_Q_MAX_DIGITS = 2; | |
35 | |
36 | |
37 // HISTORICAL DISCHARGE CURVE FORMATTER CONSTANTS | |
38 public static final int HISTORICAL_DISCHARGE_W_MIN_DIGITS = 0; | |
39 public static final int HISTORICAL_DISCHARGE_W_MAX_DIGITS = 2; | |
40 public static final int HISTORICAL_DISCHARGE_Q_MIN_DIGITS = 0; | |
41 public static final int HISTORICAL_DISCHARGE_Q_MAX_DIGITS = 2; | |
42 | |
43 | |
44 // DURATION CURVE FORMATTER CONSTANTS | |
45 public static final int DURATION_W_MIN_DIGITS = 0; | |
46 public static final int DURATION_W_MAX_DIGITS = 2; | |
47 public static final int DURATION_Q_MIN_DIGITS = 0; | |
48 public static final int DURATION_Q_MAX_DIGITS = 1; | |
49 public static final int DURATION_D_MIN_DIGITS = 0; | |
50 public static final int DURATION_D_MAX_DIGITS = 0; | |
51 | |
52 | |
53 // FLOW VELOCITY FORMATTER CONSTANTS | |
54 public static final int FLOW_VELOCITY_KM_MIN_DIGITS = 3; | |
55 public static final int FLOW_VELOCITY_KM_MAX_DIGITS = 3; | |
56 public static final int FLOW_VELOCITY_VALUES_MIN_DIGITS = 2; | |
57 public static final int FLOW_VELOCITY_VALUES_MAX_DIGITS = 2; | |
58 public static final int FLOW_VELOCITY_Q_MIN_DIGITS = 0; | |
59 public static final int FLOW_VELOCITY_Q_MAX_DIGITS = 2; | |
60 | |
61 | |
62 // MIDDLE BED HEIGHT FORMATTER CONSTANTS | |
63 public static final int MIDDLE_BED_HEIGHT_KM_MIN_DIGITS = 3; | |
64 public static final int MIDDLE_BED_HEIGHT_KM_MAX_DIGITS = 3; | |
65 public static final int MIDDLE_BED_HEIGHT_HEIGHT_MIN_DIGITS = 3; | |
66 public static final int MIDDLE_BED_HEIGHT_HEIGHT_MAX_DIGITS = 3; | |
67 public static final int MIDDLE_BED_HEIGHT_UNCERT_MIN_DIGITS = 3; | |
68 public static final int MIDDLE_BED_HEIGHT_UNCERT_MAX_DIGITS = 3; | |
69 public static final int MIDDLE_BED_HEIGHT_DATAGAP_MIN_DIGITS = 2; | |
70 public static final int MIDDLE_BED_HEIGHT_DATAGAP_MAX_DIGITS = 2; | |
71 public static final int MIDDLE_BED_HEIGHT_SOUNDING_WIDTH_MIN_DIGITS = 0; | |
72 public static final int MIDDLE_BED_HEIGHT_SOUNDING_WIDTH_MAX_DIGITS = 0; | |
73 public static final int MIDDLE_BED_HEIGHT_WIDTH_MIN_DIGITS = 3; | |
74 public static final int MIDDLE_BED_HEIGHT_WIDTH_MAX_DIGITS = 3; | |
75 | |
76 public static final int FIX_DELTA_W_KM_MIN_DIGITS = 3; | |
77 public static final int FIX_DELTA_W_KM_MAX_DIGITS = 3; | |
78 public static final int FIX_DELTA_W_DELTA_W_MIN_DIGITS = 3; | |
79 public static final int FIX_DELTA_W_DELTA_W_MAX_DIGITS = 3; | |
80 public static final int FIX_DELTA_W_DELTA_Q_MIN_DIGITS = 0; | |
81 public static final int FIX_DELTA_W_DELTA_Q_MAX_DIGITS = 2; | |
82 | |
83 /** | |
84 * Creates a localised NumberFormatter with given range of decimal digits. | |
85 * @param m CallMeta to find the locale. | |
86 * @param min minimum number of decimal ("fraction") digits. | |
87 * @param max maximum number of decimal ("fraction") digits. | |
88 * @return A NumberFormat. Use #format(NUMBER) to get String representation | |
89 * of NUMBER. | |
90 */ | |
91 public static NumberFormat getFormatter(CallMeta m, int min, int max){ | |
92 Locale locale = Resources.getLocale(m); | |
93 NumberFormat nf = NumberFormat.getInstance(locale); | |
94 | |
95 nf.setMaximumFractionDigits(max); | |
96 nf.setMinimumFractionDigits(min); | |
97 | |
98 return nf; | |
99 } | |
100 | |
101 public static NumberFormat getFormatter(CallContext c, int min, int max){ | |
102 return getFormatter(c.getMeta(), min, max); | |
103 } | |
104 | |
105 | |
106 /** | |
107 * Returns a number formatter with no max or min digits set. | |
108 * | |
109 * @param c The CallContext. | |
110 * | |
111 * @return a number formatter. | |
112 */ | |
113 public static NumberFormat getRawFormatter(CallContext c) { | |
114 Locale locale = Resources.getLocale(c.getMeta()); | |
115 return NumberFormat.getInstance(locale); | |
116 } | |
117 | |
118 | |
119 /** | |
120 * Returns a date formatter with SHORT style. | |
121 */ | |
122 public static DateFormat getShortDateFormat(CallContext cc) { | |
123 Locale locale = Resources.getLocale(cc.getMeta()); | |
124 return DateFormat.getDateInstance(DateFormat.SHORT, locale); | |
125 } | |
126 | |
127 | |
128 /** | |
129 * Returns a date formatter with MEDIUM style. | |
130 */ | |
131 public static DateFormat getMediumDateFormat(CallContext cc) { | |
132 Locale locale = Resources.getLocale(cc.getMeta()); | |
133 return DateFormat.getDateInstance(DateFormat.MEDIUM, locale); | |
134 } | |
135 | |
136 | |
137 /** | |
138 * Returns the number formatter for kilometer values in waterlevel exports. | |
139 * | |
140 * @return the number formatter for kilometer values. | |
141 */ | |
142 public static NumberFormat getWaterlevelKM(CallContext context) { | |
143 return getFormatter( | |
144 context, | |
145 WATERLEVEL_KM_MIN_DIGITS, | |
146 WATERLEVEL_KM_MAX_DIGITS); | |
147 } | |
148 | |
149 public static NumberFormat getWaterlevelW(CallMeta meta) { | |
150 return getFormatter( | |
151 meta, | |
152 WATERLEVEL_W_MIN_DIGITS, | |
153 WATERLEVEL_W_MAX_DIGITS); | |
154 } | |
155 | |
156 /** | |
157 * Returns the number formatter for W values in waterlevel exports. | |
158 * | |
159 * @return the number formatter for W values. | |
160 */ | |
161 public static NumberFormat getWaterlevelW(CallContext context) { | |
162 return getFormatter( | |
163 context, | |
164 WATERLEVEL_W_MIN_DIGITS, | |
165 WATERLEVEL_W_MAX_DIGITS); | |
166 } | |
167 | |
168 | |
169 /** | |
170 * Returns the number formatter for Q values in waterlevel exports. | |
171 * | |
172 * @return the number formatter for Q values. | |
173 */ | |
174 public static NumberFormat getWaterlevelQ(CallContext context) { | |
175 return getFormatter( | |
176 context, | |
177 WATERLEVEL_Q_MIN_DIGITS, | |
178 WATERLEVEL_Q_MAX_DIGITS); | |
179 } | |
180 | |
181 public static NumberFormat getWaterlevelQ(CallMeta meta) { | |
182 return getFormatter( | |
183 meta, | |
184 WATERLEVEL_Q_MIN_DIGITS, | |
185 WATERLEVEL_Q_MAX_DIGITS); | |
186 } | |
187 | |
188 /** | |
189 * Returns the number formatter for W values in exports of computed | |
190 * discharge curves. | |
191 * | |
192 * @return the number formatter for W values. | |
193 */ | |
194 public static NumberFormat getComputedDischargeW(CallContext context) { | |
195 return getFormatter( | |
196 context, | |
197 COMPUTED_DISCHARGE_W_MIN_DIGITS, | |
198 COMPUTED_DISCHARGE_W_MAX_DIGITS); | |
199 } | |
200 | |
201 | |
202 /** | |
203 * Returns the number formatter for Q values in exports of computed | |
204 * discharge curves. | |
205 * | |
206 * @return the number formatter for Q values. | |
207 */ | |
208 public static NumberFormat getComputedDischargeQ(CallContext context) { | |
209 return getFormatter( | |
210 context, | |
211 COMPUTED_DISCHARGE_Q_MIN_DIGITS, | |
212 COMPUTED_DISCHARGE_Q_MAX_DIGITS); | |
213 } | |
214 | |
215 | |
216 /** | |
217 * Returns the number formatter for W values in exports of historical | |
218 * discharge curves. | |
219 * | |
220 * @return the number formatter for W values. | |
221 */ | |
222 public static NumberFormat getHistoricalDischargeW(CallContext context) { | |
223 return getFormatter( | |
224 context, | |
225 HISTORICAL_DISCHARGE_W_MIN_DIGITS, | |
226 HISTORICAL_DISCHARGE_W_MAX_DIGITS); | |
227 } | |
228 | |
229 | |
230 /** | |
231 * Returns the number formatter for Q values in exports of historical | |
232 * discharge curves. | |
233 * | |
234 * @return the number formatter for Q values. | |
235 */ | |
236 public static NumberFormat getHistoricalDischargeQ(CallContext context) { | |
237 return getFormatter( | |
238 context, | |
239 HISTORICAL_DISCHARGE_Q_MIN_DIGITS, | |
240 HISTORICAL_DISCHARGE_Q_MAX_DIGITS); | |
241 } | |
242 | |
243 | |
244 /** | |
245 * Returns the number formatter for W values in duration curve exports. | |
246 * | |
247 * @return the number formatter for W values. | |
248 */ | |
249 public static NumberFormat getDurationW(CallContext context) { | |
250 return getFormatter( | |
251 context, | |
252 DURATION_W_MIN_DIGITS, | |
253 DURATION_W_MAX_DIGITS); | |
254 } | |
255 | |
256 | |
257 /** | |
258 * Returns the number formatter for Q values in duration curve exports. | |
259 * | |
260 * @return the number formatter for W values. | |
261 */ | |
262 public static NumberFormat getDurationQ(CallContext context) { | |
263 return getFormatter( | |
264 context, | |
265 DURATION_Q_MIN_DIGITS, | |
266 DURATION_Q_MAX_DIGITS); | |
267 } | |
268 | |
269 | |
270 /** | |
271 * Returns the number formatter for D values in duration curve exports. | |
272 * | |
273 * @return the number formatter for W values. | |
274 */ | |
275 public static NumberFormat getDurationD(CallContext context) { | |
276 return getFormatter( | |
277 context, | |
278 DURATION_D_MIN_DIGITS, | |
279 DURATION_D_MAX_DIGITS); | |
280 } | |
281 | |
282 public static NumberFormat getCalculationKm(CallMeta meta) { | |
283 return getFormatter( | |
284 meta, | |
285 CALCULATION_REPORT_KM_MIN_DIGITS, | |
286 CALCULATION_REPORT_KM_MAX_DIGITS); | |
287 } | |
288 | |
289 | |
290 public static NumberFormat getFlowVelocityKM(CallContext context) { | |
291 return getFormatter( | |
292 context, | |
293 FLOW_VELOCITY_KM_MIN_DIGITS, | |
294 FLOW_VELOCITY_KM_MAX_DIGITS); | |
295 } | |
296 | |
297 | |
298 public static NumberFormat getFlowVelocityValues(CallContext context) { | |
299 return getFormatter( | |
300 context, | |
301 FLOW_VELOCITY_VALUES_MIN_DIGITS, | |
302 FLOW_VELOCITY_VALUES_MAX_DIGITS); | |
303 } | |
304 | |
305 | |
306 public static NumberFormat getFlowVelocityQ(CallContext context) { | |
307 return getFormatter( | |
308 context, | |
309 FLOW_VELOCITY_Q_MIN_DIGITS, | |
310 FLOW_VELOCITY_Q_MAX_DIGITS); | |
311 } | |
312 | |
313 | |
314 public static NumberFormat getMiddleBedHeightKM(CallContext context) { | |
315 return getFormatter( | |
316 context, | |
317 MIDDLE_BED_HEIGHT_KM_MIN_DIGITS, | |
318 MIDDLE_BED_HEIGHT_KM_MAX_DIGITS); | |
319 } | |
320 | |
321 | |
322 public static NumberFormat getMiddleBedHeightHeight(CallContext context) { | |
323 return getFormatter( | |
324 context, | |
325 MIDDLE_BED_HEIGHT_HEIGHT_MIN_DIGITS, | |
326 MIDDLE_BED_HEIGHT_HEIGHT_MAX_DIGITS); | |
327 } | |
328 | |
329 | |
330 public static NumberFormat getMiddleBedHeightUncert(CallContext context) { | |
331 return getFormatter( | |
332 context, | |
333 MIDDLE_BED_HEIGHT_UNCERT_MIN_DIGITS, | |
334 MIDDLE_BED_HEIGHT_UNCERT_MAX_DIGITS); | |
335 } | |
336 | |
337 | |
338 public static NumberFormat getMiddleBedHeightDataGap(CallContext context) { | |
339 return getFormatter( | |
340 context, | |
341 MIDDLE_BED_HEIGHT_DATAGAP_MIN_DIGITS, | |
342 MIDDLE_BED_HEIGHT_DATAGAP_MAX_DIGITS); | |
343 } | |
344 | |
345 | |
346 public static NumberFormat getMiddleBedHeightSounding(CallContext context) { | |
347 return getFormatter( | |
348 context, | |
349 MIDDLE_BED_HEIGHT_SOUNDING_WIDTH_MIN_DIGITS, | |
350 MIDDLE_BED_HEIGHT_SOUNDING_WIDTH_MAX_DIGITS); | |
351 } | |
352 | |
353 | |
354 public static NumberFormat getMiddleBedHeightWidth(CallContext context) { | |
355 return getFormatter( | |
356 context, | |
357 MIDDLE_BED_HEIGHT_WIDTH_MIN_DIGITS, | |
358 MIDDLE_BED_HEIGHT_WIDTH_MAX_DIGITS); | |
359 } | |
360 | |
361 public static NumberFormat getFixDeltaWKM(CallContext context) { | |
362 return getFormatter( | |
363 context, | |
364 FIX_DELTA_W_KM_MIN_DIGITS, | |
365 FIX_DELTA_W_KM_MAX_DIGITS); | |
366 } | |
367 | |
368 public static NumberFormat getFixDeltaWDeltaW(CallContext context) { | |
369 return getFormatter( | |
370 context, | |
371 FIX_DELTA_W_DELTA_W_MIN_DIGITS, | |
372 FIX_DELTA_W_DELTA_W_MAX_DIGITS); | |
373 } | |
374 | |
375 public static NumberFormat getFixDeltaWQ(CallContext context) { | |
376 return getFormatter( | |
377 context, | |
378 FIX_DELTA_W_DELTA_Q_MIN_DIGITS, | |
379 FIX_DELTA_W_DELTA_Q_MAX_DIGITS); | |
380 } | |
381 | |
382 public static NumberFormat getFixDeltaWW(CallContext context) { | |
383 return getFormatter( | |
384 context, | |
385 FIX_DELTA_W_DELTA_W_MIN_DIGITS, | |
386 FIX_DELTA_W_DELTA_W_MAX_DIGITS); | |
387 } | |
388 | |
389 public static NumberFormat getMeterFormat(CallContext context) { | |
390 return getFormatter( | |
391 context, | |
392 0, | |
393 2); | |
394 | |
395 } | |
396 | |
397 public static DateFormat getDateFormatter(CallMeta m, String pattern) { | |
398 Locale locale = Resources.getLocale(m); | |
399 return new SimpleDateFormat(pattern, locale); | |
400 } | |
401 } | |
402 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |