Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractResultType.java @ 9168:d2472018740c
Fixed dateformat with 4-digit year, added query method for collision years of a river
author | mschaefer |
---|---|
date | Thu, 21 Jun 2018 08:13:37 +0200 |
parents | f9bb5d0a6ff3 |
children | 1614cb14308f |
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 | |
38 protected AbstractResultType(final String unit, final String csvHeader, final String pdfHeader) { | |
39 this.unit = unit; | |
40 this.csvHeader = csvHeader; | |
41 this.pdfHeader = pdfHeader; | |
42 | |
43 } | |
44 | |
45 @Override | |
46 public abstract String exportValue(final CallContext context, final Object value); | |
47 | |
48 protected final String exportStringValue(final Object value) { | |
49 | |
50 if (value == null) | |
51 return StringUtils.EMPTY; | |
52 | |
53 if (!(value instanceof String)) | |
54 throw new IllegalStateException(); | |
55 | |
56 return (String) value; | |
57 } | |
58 | |
59 @Override | |
60 public final double asDouble(final Object value) { | |
61 if (value == null) | |
62 return Double.NaN; | |
63 | |
64 if (!(value instanceof Number)) | |
65 throw new IllegalStateException(); | |
66 | |
67 final Number number = (Number) value; | |
68 return number.doubleValue(); | |
69 } | |
70 | |
71 protected final String exportDoubleValue(final CallContext context, final double value) { | |
72 if (Double.isNaN(value)) | |
73 return StringUtils.EMPTY; | |
74 | |
75 final NumberFormat formatter = getFormatter(context); | |
76 return formatter.format(value); | |
77 } | |
78 | |
79 private NumberFormat getFormatter(final CallContext context) { | |
80 final CallMeta meta = context.getMeta(); | |
81 final Locale locale = Resources.getLocale(meta); | |
82 | |
83 if (!this.formatters.containsKey(locale)) | |
84 this.formatters.put(locale, createFormatter(context)); | |
85 | |
86 return this.formatters.get(locale); | |
87 } | |
88 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
89 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
|
90 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
|
91 final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
92 return df.format(value); |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
93 } |
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
94 |
8999 | 95 protected abstract NumberFormat createFormatter(CallContext context); |
96 | |
9150
23945061daec
gigantic refactoring: exporter, result, results
gernotbelger
parents:
8999
diff
changeset
|
97 @Override |
8999 | 98 public final String getCsvHeader() { |
99 return this.csvHeader; | |
100 } | |
101 | |
102 public final String getCsvHeader(final CallMeta meta) { | |
103 return Resources.getMsg(meta, this.csvHeader, this.csvHeader); | |
104 } | |
105 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
106 @Override |
8999 | 107 public final String getPdfHeader(final CallMeta meta) { |
108 return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader); | |
109 } | |
110 | |
9157
f9bb5d0a6ff3
Added the S-Info collision calculation and chart output
mschaefer
parents:
9150
diff
changeset
|
111 @Override |
8999 | 112 public final String getUnit() { |
113 return this.unit; | |
114 } | |
115 } |