comparison flys-artifacts/src/main/java/de/intevation/flys/utils/Formatter.java @ 3786:4adc35aa655c

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

http://dive4elements.wald.intevation.org