comparison backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.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 5e38e2924c07
children c43d8c1a4455
comparison
equal deleted inserted replaced
8988:ae76f618d990 8989:2693bfaf503d
7 */ 7 */
8 8
9 package org.dive4elements.river.importer.parsers; 9 package org.dive4elements.river.importer.parsers;
10 10
11 import java.math.BigDecimal; 11 import java.math.BigDecimal;
12
13 import java.text.NumberFormat; 12 import java.text.NumberFormat;
14 import java.text.ParseException;
15
16 import java.util.ArrayList; 13 import java.util.ArrayList;
17 import java.util.List; 14 import java.util.List;
18 import java.util.regex.Matcher; 15 import java.util.regex.Matcher;
19 import java.util.regex.Pattern; 16 import java.util.regex.Pattern;
20 17
21 import org.apache.log4j.Logger; 18 import org.apache.log4j.Logger;
22
23 import org.dive4elements.river.importer.ImportMorphWidth; 19 import org.dive4elements.river.importer.ImportMorphWidth;
24 import org.dive4elements.river.importer.ImportMorphWidthValue; 20 import org.dive4elements.river.importer.ImportMorphWidthValue;
25 import org.dive4elements.river.importer.ImportUnit; 21 import org.dive4elements.river.importer.ImportUnit;
22 import org.dive4elements.river.importer.common.AbstractParser;
26 23
27 24
28 public class MorphologicalWidthParser extends LineParser { 25 public class MorphologicalWidthParser extends LineParser {
29 26
30 private static final Logger log = 27 private static final Logger log =
31 Logger.getLogger(MorphologicalWidthParser.class); 28 Logger.getLogger(MorphologicalWidthParser.class);
32 29
33 public static final NumberFormat nf = NumberFormat.getInstance( 30 public static final NumberFormat nf = NumberFormat.getInstance(
34 DEFAULT_LOCALE); 31 DEFAULT_LOCALE);
35 32
36 public static final Pattern META_UNIT = 33 public static final Pattern META_UNIT =
37 Pattern.compile("^Einheit: \\[(.*)\\].*"); 34 Pattern.compile("^Einheit: \\[(.*)\\].*");
38 35
39 protected List<ImportMorphWidth> morphWidths; 36 protected List<ImportMorphWidth> morphWidths;
40 37
41 protected ImportMorphWidth current; 38 protected ImportMorphWidth current;
42 39
43 40
44 public MorphologicalWidthParser() { 41 public MorphologicalWidthParser() {
45 morphWidths = new ArrayList<ImportMorphWidth>(); 42 this.morphWidths = new ArrayList<>();
46 } 43 }
47 44
48 45
49 @Override 46 @Override
50 protected void reset() { 47 protected void reset() {
51 current = new ImportMorphWidth(); 48 this.current = new ImportMorphWidth();
52 } 49 }
53 50
54 51
55 @Override 52 @Override
56 protected void finish() { 53 protected void finish() {
57 if (current != null) { 54 if (this.current != null) {
58 morphWidths.add(current); 55 this.morphWidths.add(this.current);
59 } 56 }
60 } 57 }
61 58
62 59
63 @Override 60 @Override
64 protected void handleLine(int lineNum, String line) { 61 protected void handleLine(final int lineNum, final String line) {
65 if (line.startsWith(START_META_CHAR)) { 62 if (line.startsWith(START_META_CHAR)) {
66 handleMetaLine(stripMetaLine(line)); 63 handleMetaLine(stripMetaLine(line));
67 } 64 }
68 else { 65 else {
69 handleDataLine(line); 66 handleDataLine(line);
70 } 67 }
71 } 68 }
72 69
73 70
74 protected void handleMetaLine(String line) { 71 protected void handleMetaLine(final String line) {
75 if (handleMetaUnit(line)) { 72 if (handleMetaUnit(line)) {
76 return; 73 return;
77 } 74 }
78 else { 75 else {
79 log.warn("MWP: Unknown meta line: '" + line + "'"); 76 log.warn("MWP: Unknown meta line: '" + line + "'");
80 } 77 }
81 } 78 }
82 79
83 80
84 protected boolean handleMetaUnit(String line) { 81 protected boolean handleMetaUnit(final String line) {
85 Matcher m = META_UNIT.matcher(line); 82 final Matcher m = META_UNIT.matcher(line);
86 83
87 if (m.matches()) { 84 if (m.matches()) {
88 String unit = m.group(1); 85 final String unit = m.group(1);
89 86
90 current.setUnit(new ImportUnit(unit)); 87 this.current.setUnit(new ImportUnit(unit));
91 88
92 return true; 89 return true;
93 } 90 }
94 91
95 return false; 92 return false;
96 } 93 }
97 94
98 95
99 protected void handleDataLine(String line) { 96 protected void handleDataLine(final String line) {
100 String[] vals = line.split(SEPERATOR_CHAR); 97 final String[] vals = line.split(SEPERATOR_CHAR);
101 98
102 if (vals == null || vals.length < 2) { 99 if (vals == null || vals.length < 2) {
103 log.warn("MWP: skip invalid data line: '" + line + "'"); 100 log.warn("MWP: skip invalid data line: '" + line + "'");
104 return; 101 return;
105 } 102 }
106 103
107 try { 104 try {
108 BigDecimal km = new BigDecimal(nf.parse(vals[0]).doubleValue()); 105 final BigDecimal km = AbstractParser.parseDecimal(vals[0]);
109 BigDecimal width = new BigDecimal(nf.parse(vals[1]).doubleValue()); 106 final BigDecimal width = AbstractParser.parseDecimal(vals[1]);
110 107
111 String desc = vals.length > 2 ? vals[2] : null; 108 final String desc = vals.length > 2 ? vals[2] : null;
112 109
113 current.addValue(new ImportMorphWidthValue( 110 this.current.addValue(new ImportMorphWidthValue(
114 km, 111 km,
115 width, 112 width,
116 desc 113 desc
117 )); 114 ));
118 } 115 }
119 catch (ParseException pe) { 116 catch (final NumberFormatException pe) {
120 log.warn("MWP: unparseable number in data row: " + line); 117 log.warn("MWP: unparseable number in data row: " + line);
121 } 118 }
122 } 119 }
123 120
124 121
125 public List<ImportMorphWidth> getMorphologicalWidths() { 122 public List<ImportMorphWidth> getMorphologicalWidths() {
126 return morphWidths; 123 return this.morphWidths;
127 } 124 }
128 } 125 }
129 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 126 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org