Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityMeasurementParser.java @ 8989:2693bfaf503d
Fixed several BigDecimal(double) creations by BigDecimal(String) parsing to avoid unnecessary decimal digits
author | mschaefer |
---|---|
date | Mon, 09 Apr 2018 09:07:00 +0200 |
parents | 4c3ccf2b0304 |
children |
comparison
equal
deleted
inserted
replaced
8988:ae76f618d990 | 8989:2693bfaf503d |
---|---|
6 * documentation coming with Dive4Elements River for details. | 6 * documentation coming with Dive4Elements River for details. |
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.importer.parsers; | 9 package org.dive4elements.river.importer.parsers; |
10 | 10 |
11 import org.dive4elements.river.importer.ImportFlowVelocityMeasurement; | |
12 import org.dive4elements.river.importer.ImportFlowVelocityMeasurementValue; | |
13 | |
14 import java.math.BigDecimal; | 11 import java.math.BigDecimal; |
15 | |
16 import java.text.DateFormat; | 12 import java.text.DateFormat; |
17 import java.text.NumberFormat; | 13 import java.text.NumberFormat; |
18 import java.text.ParseException; | |
19 import java.text.SimpleDateFormat; | 14 import java.text.SimpleDateFormat; |
20 | |
21 import java.util.ArrayList; | 15 import java.util.ArrayList; |
22 import java.util.List; | 16 import java.util.List; |
23 | 17 |
24 import org.apache.log4j.Logger; | 18 import org.apache.log4j.Logger; |
19 import org.dive4elements.river.importer.ImportFlowVelocityMeasurement; | |
20 import org.dive4elements.river.importer.ImportFlowVelocityMeasurementValue; | |
21 import org.dive4elements.river.importer.common.AbstractParser; | |
25 public class FlowVelocityMeasurementParser extends LineParser { | 22 public class FlowVelocityMeasurementParser extends LineParser { |
26 | 23 |
27 private static final Logger log = | 24 private static final Logger log = |
28 Logger.getLogger(FlowVelocityMeasurementParser.class); | 25 Logger.getLogger(FlowVelocityMeasurementParser.class); |
29 | 26 |
30 private static final NumberFormat nf = | 27 private static final NumberFormat nf = |
31 NumberFormat.getInstance(DEFAULT_LOCALE); | 28 NumberFormat.getInstance(DEFAULT_LOCALE); |
32 | 29 |
33 private static final DateFormat df = | 30 private static final DateFormat df = |
34 new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); | 31 new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
35 | 32 |
36 | 33 |
37 private List<ImportFlowVelocityMeasurement> measurements; | 34 private final List<ImportFlowVelocityMeasurement> measurements; |
38 | 35 |
39 private ImportFlowVelocityMeasurement current; | 36 private ImportFlowVelocityMeasurement current; |
40 | 37 |
41 | 38 |
42 public FlowVelocityMeasurementParser() { | 39 public FlowVelocityMeasurementParser() { |
43 measurements = new ArrayList<ImportFlowVelocityMeasurement>(); | 40 this.measurements = new ArrayList<>(); |
44 } | 41 } |
45 | 42 |
46 | 43 |
47 public List<ImportFlowVelocityMeasurement> getMeasurements() { | 44 public List<ImportFlowVelocityMeasurement> getMeasurements() { |
48 return measurements; | 45 return this.measurements; |
49 } | 46 } |
50 | 47 |
51 @Override | 48 @Override |
52 protected void reset() { | 49 protected void reset() { |
53 current = new ImportFlowVelocityMeasurement(); | 50 this.current = new ImportFlowVelocityMeasurement(); |
54 } | 51 } |
55 | 52 |
56 | 53 |
57 @Override | 54 @Override |
58 protected void finish() { | 55 protected void finish() { |
59 current.setDescription(fileName); | 56 this.current.setDescription(this.fileName); |
60 measurements.add(current); | 57 this.measurements.add(this.current); |
61 } | 58 } |
62 | 59 |
63 | 60 |
64 @Override | 61 @Override |
65 protected void handleLine(int lineNum, String line) { | 62 protected void handleLine(final int lineNum, final String line) { |
66 if (line.startsWith(START_META_CHAR)) { | 63 if (line.startsWith(START_META_CHAR)) { |
67 handleMetaLine(stripMetaLine(line)); | 64 handleMetaLine(stripMetaLine(line)); |
68 } | 65 } |
69 else { | 66 else { |
70 handleDataLine(line); | 67 handleDataLine(line); |
71 } | 68 } |
72 } | 69 } |
73 | 70 |
74 | 71 |
75 public void handleMetaLine(String line) { | 72 public void handleMetaLine(final String line) { |
76 } | 73 } |
77 | 74 |
78 | 75 |
79 public void handleDataLine(String line) { | 76 public void handleDataLine(final String line) { |
80 String[] cols = line.split(SEPERATOR_CHAR); | 77 final String[] cols = line.split(SEPERATOR_CHAR); |
81 | 78 |
82 if (cols.length < 8) { | 79 if (cols.length < 8) { |
83 log.warn("skip invalid data line: '" + line + "'"); | 80 log.warn("skip invalid data line: '" + line + "'"); |
84 return; | 81 return; |
85 } | 82 } |
86 | 83 |
87 try { | 84 try { |
88 double km = nf.parse(cols[1]).doubleValue(); | 85 final BigDecimal km = AbstractParser.parseDecimal(cols[1]); |
89 double w = nf.parse(cols[5]).doubleValue(); | 86 final BigDecimal w = AbstractParser.parseDecimal(cols[5]); |
90 double q = nf.parse(cols[6]).doubleValue(); | 87 final BigDecimal q = AbstractParser.parseDecimal(cols[6]); |
91 double v = nf.parse(cols[7]).doubleValue(); | 88 final BigDecimal v = AbstractParser.parseDecimal(cols[7]); |
92 | 89 |
93 String timestr = cols[3] + " " + cols[4]; | 90 final String timestr = cols[3] + " " + cols[4]; |
94 String description = cols.length > 8 ? cols[8] : null; | 91 final String description = cols.length > 8 ? cols[8] : null; |
95 | 92 |
96 current.addValue(new ImportFlowVelocityMeasurementValue( | 93 this.current.addValue(new ImportFlowVelocityMeasurementValue(df.parse(timestr), km, w, q, v, description)); |
97 df.parse(timestr), | |
98 new BigDecimal(km), | |
99 new BigDecimal(w), | |
100 new BigDecimal(q), | |
101 new BigDecimal(v), | |
102 description | |
103 )); | |
104 } | 94 } |
105 catch (ParseException pe) { | 95 catch (final Exception pe) { |
106 log.warn("Unparseable flow velocity values:", pe); | 96 log.warn("Unparseable flow velocity values:", pe); |
107 } | 97 } |
108 } | 98 } |
109 } | 99 } |
110 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 100 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |