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 |
|
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 |
|
22 /** |
|
23 * @author Domenico Nardi Tironi |
|
24 */ |
|
25 public abstract class AbstractResultType implements IResultType { |
|
26 |
|
27 /* Cache for formatters because Formatter will always create new formats (which is very expensive) */ |
|
28 private final Map<Locale, NumberFormat> formatters = new HashMap<>(); |
|
29 |
|
30 private final String unit; |
|
31 |
|
32 private final String csvHeader; |
|
33 |
|
34 private final String pdfHeader; |
|
35 |
|
36 protected AbstractResultType(final String unit, final String csvHeader, final String pdfHeader) { |
|
37 this.unit = unit; |
|
38 this.csvHeader = csvHeader; |
|
39 this.pdfHeader = pdfHeader; |
|
40 |
|
41 } |
|
42 |
|
43 @Override |
|
44 public abstract String exportValue(final CallContext context, final Object value); |
|
45 |
|
46 protected final String exportStringValue(final Object value) { |
|
47 |
|
48 if (value == null) |
|
49 return StringUtils.EMPTY; |
|
50 |
|
51 if (!(value instanceof String)) |
|
52 throw new IllegalStateException(); |
|
53 |
|
54 return (String) value; |
|
55 } |
|
56 |
|
57 @Override |
|
58 public final double asDouble(final Object value) { |
|
59 if (value == null) |
|
60 return Double.NaN; |
|
61 |
|
62 if (!(value instanceof Number)) |
|
63 throw new IllegalStateException(); |
|
64 |
|
65 final Number number = (Number) value; |
|
66 return number.doubleValue(); |
|
67 } |
|
68 |
|
69 protected final String exportDoubleValue(final CallContext context, final double value) { |
|
70 if (Double.isNaN(value)) |
|
71 return StringUtils.EMPTY; |
|
72 |
|
73 final NumberFormat formatter = getFormatter(context); |
|
74 return formatter.format(value); |
|
75 } |
|
76 |
|
77 private NumberFormat getFormatter(final CallContext context) { |
|
78 final CallMeta meta = context.getMeta(); |
|
79 final Locale locale = Resources.getLocale(meta); |
|
80 |
|
81 if (!this.formatters.containsKey(locale)) |
|
82 this.formatters.put(locale, createFormatter(context)); |
|
83 |
|
84 return this.formatters.get(locale); |
|
85 } |
|
86 |
|
87 protected abstract NumberFormat createFormatter(CallContext context); |
|
88 |
|
89 public final String getCsvHeader() { |
|
90 return this.csvHeader; |
|
91 } |
|
92 |
|
93 public final String getCsvHeader(final CallMeta meta) { |
|
94 return Resources.getMsg(meta, this.csvHeader, this.csvHeader); |
|
95 } |
|
96 |
|
97 public final String getPdfHeader(final CallMeta meta) { |
|
98 return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader); |
|
99 } |
|
100 |
|
101 public final String getUnit() { |
|
102 return this.unit; |
|
103 } |
|
104 } |