comparison backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.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
8 8
9 package org.dive4elements.river.importer.parsers; 9 package org.dive4elements.river.importer.parsers;
10 10
11 import java.io.File; 11 import java.io.File;
12 import java.io.IOException; 12 import java.io.IOException;
13
14 import java.math.BigDecimal; 13 import java.math.BigDecimal;
15 import java.text.NumberFormat; 14 import java.text.NumberFormat;
16 import java.text.ParseException;
17 import java.util.ArrayList; 15 import java.util.ArrayList;
18 import java.util.List; 16 import java.util.List;
19 import java.util.TreeSet; 17 import java.util.TreeSet;
20 import java.util.regex.Matcher; 18 import java.util.regex.Matcher;
21 import java.util.regex.Pattern; 19 import java.util.regex.Pattern;
22 20
23 import org.apache.log4j.Logger; 21 import org.apache.log4j.Logger;
24 22 import org.dive4elements.river.backend.utils.EpsilonComparator;
25 import org.dive4elements.river.importer.ImportDischargeZone; 23 import org.dive4elements.river.importer.ImportDischargeZone;
26 import org.dive4elements.river.importer.ImportFlowVelocityModel; 24 import org.dive4elements.river.importer.ImportFlowVelocityModel;
27 import org.dive4elements.river.importer.ImportFlowVelocityModelValue; 25 import org.dive4elements.river.importer.ImportFlowVelocityModelValue;
28 import org.dive4elements.river.backend.utils.EpsilonComparator; 26 import org.dive4elements.river.importer.common.AbstractParser;
29 27
30 28
31 public class FlowVelocityModelParser extends LineParser { 29 public class FlowVelocityModelParser extends LineParser {
32 30
33 private static final Logger log = 31 private static final Logger log =
34 Logger.getLogger(FlowVelocityModelParser.class); 32 Logger.getLogger(FlowVelocityModelParser.class);
35 33
36 private static final Pattern META_REGEX = 34 private static final Pattern META_REGEX =
37 Pattern.compile(".*Rechnung [unter ]*(.*) \\(Pegel (.*)\\).*"); 35 Pattern.compile(".*Rechnung [unter ]*(.*) \\(Pegel (.*)\\).*");
38 36
39 private static final Pattern META_GAUGE = 37 private static final Pattern META_GAUGE =
40 Pattern.compile("(.*) Q=(\\w*)m3/s"); 38 Pattern.compile("(.*) Q=(\\w*)m3/s");
41 39
42 private static final Pattern META_MAINVALUE_A = 40 private static final Pattern META_MAINVALUE_A =
43 Pattern.compile("([a-zA-Z]+)+(\\d+)*[\\w()]*"); 41 Pattern.compile("([a-zA-Z]+)+(\\d+)*[\\w()]*");
44 42
45 private static final Pattern META_MAINVALUE_B = 43 private static final Pattern META_MAINVALUE_B =
46 Pattern.compile( 44 Pattern.compile(
47 "(([a-zA-Z]+)+(\\d+)*)\\s*-\\s*(([a-zA-Z]+)+(\\d+)*\\S*)"); 45 "(([a-zA-Z]+)+(\\d+)*)\\s*-\\s*(([a-zA-Z]+)+(\\d+)*\\S*)");
48 46
49 private static final Pattern META_MAINVALUE_C = 47 private static final Pattern META_MAINVALUE_C =
50 Pattern.compile("([0-9]++)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*)"); 48 Pattern.compile("([0-9]++)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*)");
51 49
52 private static final Pattern META_MAINVALUE_D = 50 private static final Pattern META_MAINVALUE_D =
53 Pattern.compile( 51 Pattern.compile(
54 "(([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*))\\s*" 52 "(([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*))\\s*"
55 + "bis (([0-9]*)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*))"); 53 + "bis (([0-9]*)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*))");
56 54
57 private static final Pattern META_MAINVALUE_E = 55 private static final Pattern META_MAINVALUE_E =
58 Pattern.compile( 56 Pattern.compile(
59 "(([a-zA-Z]+)+(\\d+)*)\\s*bis (([a-zA-Z]+)+(\\d+)*\\S*)"); 57 "(([a-zA-Z]+)+(\\d+)*)\\s*bis (([a-zA-Z]+)+(\\d+)*\\S*)");
60 58
61 private static final NumberFormat nf = 59 private static final NumberFormat nf =
62 NumberFormat.getInstance(DEFAULT_LOCALE); 60 NumberFormat.getInstance(DEFAULT_LOCALE);
63 61
64 62
65 private List<ImportFlowVelocityModel> models; 63 private final List<ImportFlowVelocityModel> models;
66 64
67 private ImportFlowVelocityModel current; 65 private ImportFlowVelocityModel current;
68 66
69 protected String description; 67 protected String description;
70 68
71 protected TreeSet<Double> kmExists; 69 protected TreeSet<Double> kmExists;
72 70
73 71
74 public FlowVelocityModelParser() { 72 public FlowVelocityModelParser() {
75 models = new ArrayList<ImportFlowVelocityModel>(); 73 this.models = new ArrayList<>();
76 kmExists = new TreeSet<Double>(EpsilonComparator.CMP); 74 this.kmExists = new TreeSet<>(EpsilonComparator.CMP);
77 } 75 }
78 76
79 77
80 public List<ImportFlowVelocityModel> getModels() { 78 public List<ImportFlowVelocityModel> getModels() {
81 return models; 79 return this.models;
82 } 80 }
83 81
84 @Override 82 @Override
85 public void parse(File file) throws IOException { 83 public void parse(final File file) throws IOException {
86 description = file.getName(); 84 this.description = file.getName();
87 85
88 super.parse(file); 86 super.parse(file);
89 } 87 }
90 88
91 @Override 89 @Override
92 protected void reset() { 90 protected void reset() {
93 current = new ImportFlowVelocityModel(description); 91 this.current = new ImportFlowVelocityModel(this.description);
94 kmExists.clear(); 92 this.kmExists.clear();
95 } 93 }
96 94
97 95
98 @Override 96 @Override
99 protected void finish() { 97 protected void finish() {
100 models.add(current); 98 this.models.add(this.current);
101 // description = null; 99 // description = null;
102 } 100 }
103 101
104 102
105 @Override 103 @Override
106 protected void handleLine(int lineNum, String line) { 104 protected void handleLine(final int lineNum, final String line) {
107 if (line.startsWith(START_META_CHAR)) { 105 if (line.startsWith(START_META_CHAR)) {
108 handleMetaLine(stripMetaLine(line)); 106 handleMetaLine(stripMetaLine(line));
109 } 107 }
110 else { 108 else {
111 handleDataLine(line); 109 handleDataLine(line);
112 } 110 }
113 } 111 }
114 112
115 113
116 protected void handleMetaLine(String line) { 114 protected void handleMetaLine(final String line) {
117 Matcher m = META_REGEX.matcher(line); 115 final Matcher m = META_REGEX.matcher(line);
118 116
119 if (m.matches()) { 117 if (m.matches()) {
120 String mainValueStr = m.group(1); 118 final String mainValueStr = m.group(1);
121 log.debug("mainValueStr = '" + mainValueStr + "'"); 119 log.debug("mainValueStr = '" + mainValueStr + "'");
122 String gaugeStr = m.group(2); 120 final String gaugeStr = m.group(2);
123 121
124 Object[] valueData = handleMainValueString(mainValueStr); 122 final Object[] valueData = handleMainValueString(mainValueStr);
125 Object[] gaugeData = handleGaugeString(gaugeStr); 123 final Object[] gaugeData = handleGaugeString(gaugeStr);
126 124
127 if (valueData == null || valueData.length < 2) { 125 if (valueData == null || valueData.length < 2) {
128 log.warn("skip invalid MainValue part in '" + line + "'"); 126 log.warn("skip invalid MainValue part in '" + line + "'");
129 return; 127 return;
130 } 128 }
140 log.debug(" Value: " + gaugeData[1]); 138 log.debug(" Value: " + gaugeData[1]);
141 log.debug(" Lower: " + valueData[0]); 139 log.debug(" Lower: " + valueData[0]);
142 log.debug(" upper: " + valueData[1]); 140 log.debug(" upper: " + valueData[1]);
143 } 141 }
144 142
145 current.setDischargeZone(new ImportDischargeZone( 143 this.current.setDischargeZone(new ImportDischargeZone(
146 (String) gaugeData[0], 144 (String) gaugeData[0],
147 (BigDecimal) gaugeData[1], 145 (BigDecimal) gaugeData[1],
148 (String) valueData[0], 146 (String) valueData[0],
149 (String) valueData[1] 147 (String) valueData[1]
150 )); 148 ));
151 } 149 }
152 } 150 }
153 151
154 152
155 protected Object[] handleMainValueString(String mainValueStr) { 153 protected Object[] handleMainValueString(final String mainValueStr) {
156 Matcher mA = META_MAINVALUE_A.matcher(mainValueStr.trim()); 154 final Matcher mA = META_MAINVALUE_A.matcher(mainValueStr.trim());
157 if (mA.matches()) { 155 if (mA.matches()) {
158 log.debug("mainValueStr matches META_MAINVALUE_A"); 156 log.debug("mainValueStr matches META_MAINVALUE_A");
159 String name = mA.group(0); 157 final String name = mA.group(0);
160 158
161 return new Object[] { name, name }; 159 return new Object[] { name, name };
162 } 160 }
163 161
164 Matcher mB = META_MAINVALUE_B.matcher(mainValueStr.trim()); 162 final Matcher mB = META_MAINVALUE_B.matcher(mainValueStr.trim());
165 if (mB.matches()) { 163 if (mB.matches()) {
166 log.debug("mainValueStr matches META_MAINVALUE_B"); 164 log.debug("mainValueStr matches META_MAINVALUE_B");
167 String lower = mB.group(1); 165 final String lower = mB.group(1);
168 String upper = mB.group(4); 166 final String upper = mB.group(4);
169 167
170 return new Object[] { lower, upper }; 168 return new Object[] { lower, upper };
171 } 169 }
172 170
173 Matcher mC = META_MAINVALUE_C.matcher(mainValueStr.trim()); 171 final Matcher mC = META_MAINVALUE_C.matcher(mainValueStr.trim());
174 if (mC.matches()) { 172 if (mC.matches()) {
175 log.debug("mainValueStr matches META_MAINVALUE_C"); 173 log.debug("mainValueStr matches META_MAINVALUE_C");
176 String facA = mC.group(1); 174 final String facA = mC.group(1);
177 String nameA = mC.group(2); 175 final String nameA = mC.group(2);
178 String facB = mC.group(3); 176 final String facB = mC.group(3);
179 String nameB = mC.group(4); 177 final String nameB = mC.group(4);
180 178
181 String fac = facA != null ? facA : facB; 179 final String fac = facA != null ? facA : facB;
182 String name = nameA != null ? nameA : nameB; 180 final String name = nameA != null ? nameA : nameB;
183 181
184 String mainValue = fac + " " + name; 182 final String mainValue = fac + " " + name;
185 183
186 return new Object[] { mainValue, mainValue }; 184 return new Object[] { mainValue, mainValue };
187 } 185 }
188 186
189 Matcher mD = META_MAINVALUE_D.matcher(mainValueStr.trim()); 187 final Matcher mD = META_MAINVALUE_D.matcher(mainValueStr.trim());
190 if (mD.matches()) { 188 if (mD.matches()) {
191 log.debug("mainValueStr matches META_MAINVALUE_D"); 189 log.debug("mainValueStr matches META_MAINVALUE_D");
192 String loFacA = mD.group(2); 190 final String loFacA = mD.group(2);
193 String loNameA = mD.group(3); 191 final String loNameA = mD.group(3);
194 String loFacB = mD.group(4); 192 final String loFacB = mD.group(4);
195 String loNameB = mD.group(5); 193 final String loNameB = mD.group(5);
196 194
197 String upFacA = mD.group(7); 195 final String upFacA = mD.group(7);
198 String upNameA = mD.group(8); 196 final String upNameA = mD.group(8);
199 String upFacB = mD.group(9); 197 final String upFacB = mD.group(9);
200 String upNameB = mD.group(10); 198 final String upNameB = mD.group(10);
201 199
202 String loFac = loFacA != null ? loFacA : loFacB; 200 final String loFac = loFacA != null ? loFacA : loFacB;
203 String loName = loNameA != null ? loNameA : loNameB; 201 final String loName = loNameA != null ? loNameA : loNameB;
204 202
205 String upFac = upFacA != null ? upFacA : upFacB; 203 final String upFac = upFacA != null ? upFacA : upFacB;
206 String upName = upNameA != null ? upNameA : upNameB; 204 final String upName = upNameA != null ? upNameA : upNameB;
207 205
208 String loMainValue = loFac + " " + loName; 206 final String loMainValue = loFac + " " + loName;
209 String upMainValue = upFac + " " + upName; 207 final String upMainValue = upFac + " " + upName;
210 208
211 return new Object[] { loMainValue, upMainValue }; 209 return new Object[] { loMainValue, upMainValue };
212 } 210 }
213 211
214 Matcher mE = META_MAINVALUE_E.matcher(mainValueStr.trim()); 212 final Matcher mE = META_MAINVALUE_E.matcher(mainValueStr.trim());
215 if (mE.matches()) { 213 if (mE.matches()) {
216 log.debug("mainValueStr matches META_MAINVALUE_E"); 214 log.debug("mainValueStr matches META_MAINVALUE_E");
217 String lower = mE.group(1); 215 final String lower = mE.group(1);
218 String upper = mE.group(4); 216 final String upper = mE.group(4);
219 217
220 return new Object[] { lower, upper }; 218 return new Object[] { lower, upper };
221 } 219 }
222 220
223 log.debug("mainValueStr not matched"); 221 log.debug("mainValueStr not matched");
224 return null; 222 return null;
225 } 223 }
226 224
227 225
228 protected Object[] handleGaugeString(String gaugeStr) { 226 protected Object[] handleGaugeString(final String gaugeStr) {
229 Matcher m = META_GAUGE.matcher(gaugeStr); 227 final Matcher m = META_GAUGE.matcher(gaugeStr);
230 228
231 if (m.matches()) { 229 if (m.matches()) {
232 String name = m.group(1); 230 final String name = m.group(1);
233 String qStr = m.group(2); 231 final String qStr = m.group(2);
234 232
235 try { 233 try {
236 return new Object[] { 234 return new Object[] {
237 name, 235 name,
238 new BigDecimal(nf.parse(qStr).doubleValue()) }; 236 AbstractParser.parseDecimal(qStr) };
239 } 237 }
240 catch (ParseException pe) { 238 catch (final NumberFormatException pe) {
241 log.warn("Could not parse Q value: '" + qStr + "'"); 239 log.warn("Could not parse Q value: '" + qStr + "'");
242 } 240 }
243 } 241 }
244 242
245 return null; 243 return null;
246 } 244 }
247 245
248 246
249 protected void handleDataLine(String line) { 247 protected void handleDataLine(final String line) {
250 String[] cols = line.split(SEPERATOR_CHAR); 248 final String[] cols = line.split(SEPERATOR_CHAR);
251 249
252 if (cols.length < 5) { 250 if (cols.length < 5) {
253 log.warn("skip invalid data line: '" + line + "'"); 251 log.warn("skip invalid data line: '" + line + "'");
254 return; 252 return;
255 } 253 }
256 254
257 try { 255 try {
258 double km = nf.parse(cols[0]).doubleValue(); 256 final BigDecimal km = AbstractParser.parseDecimal(cols[0]);
259 257
260 Double key = Double.valueOf(km); 258 final Double key = Double.valueOf(km.doubleValue());
261 259
262 if (kmExists.contains(key)) { 260 if (this.kmExists.contains(key)) {
263 log.warn("duplicate station '" + km + "': -> ignored"); 261 log.warn("duplicate station '" + km + "': -> ignored");
264 return; 262 return;
265 } 263 }
266 264
267 double q = nf.parse(cols[1]).doubleValue(); 265 final BigDecimal q = AbstractParser.parseDecimal(cols[1]);
268 double total = nf.parse(cols[2]).doubleValue(); 266 final BigDecimal total = AbstractParser.parseDecimal(cols[2]);
269 double main = nf.parse(cols[3]).doubleValue(); 267 final BigDecimal main = AbstractParser.parseDecimal(cols[3]);
270 double stress = nf.parse(cols[4]).doubleValue(); 268 final BigDecimal stress = AbstractParser.parseDecimal(cols[4]);
271 269
272 current.addValue(new ImportFlowVelocityModelValue( 270 this.current.addValue(new ImportFlowVelocityModelValue(km, q, total, main, stress));
273 new BigDecimal(km), 271
274 new BigDecimal(q), 272 this.kmExists.add(key);
275 new BigDecimal(total), 273 }
276 new BigDecimal(main), 274 catch (final NumberFormatException pe) {
277 new BigDecimal(stress)
278 ));
279
280 kmExists.add(key);
281 }
282 catch (ParseException pe) {
283 log.warn("Unparseable flow velocity values:", pe); 275 log.warn("Unparseable flow velocity values:", pe);
284 } 276 }
285 } 277 }
286 } 278 }
287 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 279 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org