Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractResultType.java @ 9491:d7d22ea8573d
Temporarily: log sinfo meanbedheight formatting
author | mschaefer |
---|---|
date | Wed, 19 Sep 2018 17:53:53 +0200 |
parents | d32b11d585cd |
children | 4deb6b24f072 |
rev | line source |
---|---|
8999 | 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.common; | |
11 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
12 import java.text.DateFormat; |
8999 | 13 import java.text.NumberFormat; |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
14 import java.util.Date; |
8999 | 15 import java.util.HashMap; |
16 import java.util.Locale; | |
17 import java.util.Map; | |
18 | |
19 import org.apache.commons.lang.StringUtils; | |
20 import org.dive4elements.artifacts.CallContext; | |
21 import org.dive4elements.artifacts.CallMeta; | |
22 import org.dive4elements.river.artifacts.resources.Resources; | |
23 | |
24 /** | |
25 * @author Domenico Nardi Tironi | |
26 */ | |
27 public abstract class AbstractResultType implements IResultType { | |
28 | |
29 /* Cache for formatters because Formatter will always create new formats (which is very expensive) */ | |
30 private final Map<Locale, NumberFormat> formatters = new HashMap<>(); | |
31 | |
32 private final String unit; | |
33 | |
34 private final String csvHeader; | |
35 | |
36 private final String pdfHeader; | |
37 | |
9448 | 38 private static String YES = null; |
39 private static String NO = null; | |
40 | |
8999 | 41 protected AbstractResultType(final String unit, final String csvHeader, final String pdfHeader) { |
42 this.unit = unit; | |
43 this.csvHeader = csvHeader; | |
44 this.pdfHeader = pdfHeader; | |
45 | |
46 } | |
47 | |
48 @Override | |
49 public abstract String exportValue(final CallContext context, final Object value); | |
50 | |
51 protected final String exportStringValue(final Object value) { | |
52 | |
53 if (value == null) | |
54 return StringUtils.EMPTY; | |
55 | |
56 if (!(value instanceof String)) | |
57 throw new IllegalStateException(); | |
58 | |
59 return (String) value; | |
60 } | |
61 | |
9448 | 62 protected final String exportBooleanValue(final CallContext context, final Object value) { |
63 | |
64 if (value == null) | |
65 return StringUtils.EMPTY; // DISKUSSION; könnte auch "Nein" sein | |
66 | |
67 if (!(value instanceof Boolean)) | |
68 throw new IllegalStateException(); | |
69 | |
70 if ((boolean) value) { | |
71 if (YES == null) | |
72 YES = Resources.getMsg(context.getMeta(), String.valueOf(value)); | |
73 return YES; | |
74 } else { | |
75 if (NO == null) | |
76 NO = Resources.getMsg(context.getMeta(), String.valueOf(value)); | |
77 return NO; | |
78 } | |
79 } | |
80 | |
8999 | 81 @Override |
82 public final double asDouble(final Object value) { | |
83 if (value == null) | |
84 return Double.NaN; | |
85 | |
86 if (!(value instanceof Number)) | |
87 throw new IllegalStateException(); | |
88 | |
89 final Number number = (Number) value; | |
90 return number.doubleValue(); | |
91 } | |
92 | |
93 protected final String exportDoubleValue(final CallContext context, final double value) { | |
94 if (Double.isNaN(value)) | |
95 return StringUtils.EMPTY; | |
96 | |
97 final NumberFormat formatter = getFormatter(context); | |
98 return formatter.format(value); | |
99 } | |
100 | |
9491 | 101 protected NumberFormat getFormatter(final CallContext context) { |
8999 | 102 final CallMeta meta = context.getMeta(); |
103 final Locale locale = Resources.getLocale(meta); | |
104 | |
105 if (!this.formatters.containsKey(locale)) | |
106 this.formatters.put(locale, createFormatter(context)); | |
107 | |
108 return this.formatters.get(locale); | |
109 } | |
110 | |
9176
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9168
diff
changeset
|
111 protected abstract NumberFormat createFormatter(CallContext context); |
1614cb14308f
Work on calculations for S-Info flood duration workflow
mschaefer
parents:
9168
diff
changeset
|
112 |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
113 protected final String exportDateValue(final CallContext context, final Date value) { |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
114 final Locale locale = Resources.getLocale(context.getMeta()); |
9168
d2472018740c
Fixed dateformat with 4-digit year, added query method for collision years of a river
mschaefer
parents:
9157
diff
changeset
|
115 final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
116 return df.format(value); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
117 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
118 |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
8999
diff
changeset
|
119 @Override |
8999 | 120 public final String getCsvHeader() { |
121 return this.csvHeader; | |
122 } | |
123 | |
124 public final String getCsvHeader(final CallMeta meta) { | |
125 return Resources.getMsg(meta, this.csvHeader, this.csvHeader); | |
126 } | |
127 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
128 @Override |
8999 | 129 public final String getPdfHeader(final CallMeta meta) { |
130 return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader); | |
131 } | |
132 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
133 @Override |
8999 | 134 public final String getUnit() { |
135 return this.unit; | |
136 } | |
137 } |