diff 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
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java	Sun Apr 08 18:09:32 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java	Mon Apr 09 09:07:00 2018 +0200
@@ -10,10 +10,8 @@
 
 import java.io.File;
 import java.io.IOException;
-
 import java.math.BigDecimal;
 import java.text.NumberFormat;
-import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.TreeSet;
@@ -21,48 +19,48 @@
 import java.util.regex.Pattern;
 
 import org.apache.log4j.Logger;
-
+import org.dive4elements.river.backend.utils.EpsilonComparator;
 import org.dive4elements.river.importer.ImportDischargeZone;
 import org.dive4elements.river.importer.ImportFlowVelocityModel;
 import org.dive4elements.river.importer.ImportFlowVelocityModelValue;
-import org.dive4elements.river.backend.utils.EpsilonComparator;
+import org.dive4elements.river.importer.common.AbstractParser;
 
 
 public class FlowVelocityModelParser extends LineParser {
 
     private static final Logger log =
-        Logger.getLogger(FlowVelocityModelParser.class);
+            Logger.getLogger(FlowVelocityModelParser.class);
 
     private static final Pattern META_REGEX =
-        Pattern.compile(".*Rechnung [unter ]*(.*) \\(Pegel (.*)\\).*");
+            Pattern.compile(".*Rechnung [unter ]*(.*) \\(Pegel (.*)\\).*");
 
     private static final Pattern META_GAUGE =
-        Pattern.compile("(.*) Q=(\\w*)m3/s");
+            Pattern.compile("(.*) Q=(\\w*)m3/s");
 
     private static final Pattern META_MAINVALUE_A =
-        Pattern.compile("([a-zA-Z]+)+(\\d+)*[\\w()]*");
+            Pattern.compile("([a-zA-Z]+)+(\\d+)*[\\w()]*");
 
     private static final Pattern META_MAINVALUE_B =
-        Pattern.compile(
-            "(([a-zA-Z]+)+(\\d+)*)\\s*-\\s*(([a-zA-Z]+)+(\\d+)*\\S*)");
+            Pattern.compile(
+                    "(([a-zA-Z]+)+(\\d+)*)\\s*-\\s*(([a-zA-Z]+)+(\\d+)*\\S*)");
 
     private static final Pattern META_MAINVALUE_C =
-        Pattern.compile("([0-9]++)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*)");
+            Pattern.compile("([0-9]++)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*)");
 
     private static final Pattern META_MAINVALUE_D =
-        Pattern.compile(
-            "(([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*))\\s*"
-            + "bis (([0-9]*)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*))");
+            Pattern.compile(
+                    "(([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*))\\s*"
+                            + "bis (([0-9]*)\\s?(\\S*)|([0-9]++,[0-9]++)\\s?(\\S*))");
 
     private static final Pattern META_MAINVALUE_E =
-        Pattern.compile(
-            "(([a-zA-Z]+)+(\\d+)*)\\s*bis (([a-zA-Z]+)+(\\d+)*\\S*)");
+            Pattern.compile(
+                    "(([a-zA-Z]+)+(\\d+)*)\\s*bis (([a-zA-Z]+)+(\\d+)*\\S*)");
 
     private static final NumberFormat nf =
-        NumberFormat.getInstance(DEFAULT_LOCALE);
+            NumberFormat.getInstance(DEFAULT_LOCALE);
 
 
-    private List<ImportFlowVelocityModel> models;
+    private final List<ImportFlowVelocityModel> models;
 
     private ImportFlowVelocityModel current;
 
@@ -72,38 +70,38 @@
 
 
     public FlowVelocityModelParser() {
-        models = new ArrayList<ImportFlowVelocityModel>();
-        kmExists = new TreeSet<Double>(EpsilonComparator.CMP);
+        this.models = new ArrayList<>();
+        this.kmExists = new TreeSet<>(EpsilonComparator.CMP);
     }
 
 
     public List<ImportFlowVelocityModel> getModels() {
-        return models;
+        return this.models;
     }
 
     @Override
-    public void parse(File file) throws IOException {
-        description = file.getName();
+    public void parse(final File file) throws IOException {
+        this.description = file.getName();
 
         super.parse(file);
     }
 
     @Override
     protected void reset() {
-        current = new ImportFlowVelocityModel(description);
-        kmExists.clear();
+        this.current = new ImportFlowVelocityModel(this.description);
+        this.kmExists.clear();
     }
 
 
     @Override
     protected void finish() {
-        models.add(current);
+        this.models.add(this.current);
         // description = null;
     }
 
 
     @Override
-    protected void handleLine(int lineNum, String line) {
+    protected void handleLine(final int lineNum, final String line) {
         if (line.startsWith(START_META_CHAR)) {
             handleMetaLine(stripMetaLine(line));
         }
@@ -113,16 +111,16 @@
     }
 
 
-    protected void handleMetaLine(String line) {
-        Matcher m = META_REGEX.matcher(line);
+    protected void handleMetaLine(final String line) {
+        final Matcher m = META_REGEX.matcher(line);
 
         if (m.matches()) {
-            String mainValueStr = m.group(1);
+            final String mainValueStr = m.group(1);
             log.debug("mainValueStr = '" + mainValueStr + "'");
-            String gaugeStr     = m.group(2);
+            final String gaugeStr     = m.group(2);
 
-            Object[] valueData = handleMainValueString(mainValueStr);
-            Object[] gaugeData = handleGaugeString(gaugeStr);
+            final Object[] valueData = handleMainValueString(mainValueStr);
+            final Object[] gaugeData = handleGaugeString(gaugeStr);
 
             if (valueData == null || valueData.length < 2) {
                 log.warn("skip invalid MainValue part in '" + line + "'");
@@ -142,102 +140,102 @@
                 log.debug("   upper: " + valueData[1]);
             }
 
-            current.setDischargeZone(new ImportDischargeZone(
-                (String) gaugeData[0],
-                (BigDecimal) gaugeData[1],
-                (String) valueData[0],
-                (String) valueData[1]
-            ));
+            this.current.setDischargeZone(new ImportDischargeZone(
+                    (String) gaugeData[0],
+                    (BigDecimal) gaugeData[1],
+                    (String) valueData[0],
+                    (String) valueData[1]
+                    ));
         }
     }
 
 
-    protected Object[] handleMainValueString(String mainValueStr) {
-        Matcher mA = META_MAINVALUE_A.matcher(mainValueStr.trim());
+    protected Object[] handleMainValueString(final String mainValueStr) {
+        final Matcher mA = META_MAINVALUE_A.matcher(mainValueStr.trim());
         if (mA.matches()) {
             log.debug("mainValueStr matches META_MAINVALUE_A");
-            String name = mA.group(0);
+            final String name = mA.group(0);
 
             return new Object[] { name, name };
         }
 
-        Matcher mB = META_MAINVALUE_B.matcher(mainValueStr.trim());
+        final Matcher mB = META_MAINVALUE_B.matcher(mainValueStr.trim());
         if (mB.matches()) {
             log.debug("mainValueStr matches META_MAINVALUE_B");
-            String lower = mB.group(1);
-            String upper = mB.group(4);
+            final String lower = mB.group(1);
+            final String upper = mB.group(4);
 
             return new Object[] { lower, upper };
         }
 
-        Matcher mC = META_MAINVALUE_C.matcher(mainValueStr.trim());
+        final Matcher mC = META_MAINVALUE_C.matcher(mainValueStr.trim());
         if (mC.matches()) {
             log.debug("mainValueStr matches META_MAINVALUE_C");
-            String facA  = mC.group(1);
-            String nameA = mC.group(2);
-            String facB  = mC.group(3);
-            String nameB = mC.group(4);
+            final String facA  = mC.group(1);
+            final String nameA = mC.group(2);
+            final String facB  = mC.group(3);
+            final String nameB = mC.group(4);
 
-            String fac  = facA  != null ? facA  : facB;
-            String name = nameA != null ? nameA : nameB;
+            final String fac  = facA  != null ? facA  : facB;
+            final String name = nameA != null ? nameA : nameB;
 
-            String mainValue = fac + " " + name;
+            final String mainValue = fac + " " + name;
 
             return new Object[] { mainValue, mainValue };
         }
 
-        Matcher mD = META_MAINVALUE_D.matcher(mainValueStr.trim());
+        final Matcher mD = META_MAINVALUE_D.matcher(mainValueStr.trim());
         if (mD.matches()) {
             log.debug("mainValueStr matches META_MAINVALUE_D");
-            String loFacA  = mD.group(2);
-            String loNameA = mD.group(3);
-            String loFacB  = mD.group(4);
-            String loNameB = mD.group(5);
+            final String loFacA  = mD.group(2);
+            final String loNameA = mD.group(3);
+            final String loFacB  = mD.group(4);
+            final String loNameB = mD.group(5);
 
-            String upFacA  = mD.group(7);
-            String upNameA = mD.group(8);
-            String upFacB  = mD.group(9);
-            String upNameB = mD.group(10);
+            final String upFacA  = mD.group(7);
+            final String upNameA = mD.group(8);
+            final String upFacB  = mD.group(9);
+            final String upNameB = mD.group(10);
 
-            String loFac  = loFacA  != null ? loFacA  : loFacB;
-            String loName = loNameA != null ? loNameA : loNameB;
+            final String loFac  = loFacA  != null ? loFacA  : loFacB;
+            final String loName = loNameA != null ? loNameA : loNameB;
 
-            String upFac  = upFacA  != null ? upFacA  : upFacB;
-            String upName = upNameA != null ? upNameA : upNameB;
+            final String upFac  = upFacA  != null ? upFacA  : upFacB;
+            final String upName = upNameA != null ? upNameA : upNameB;
 
-            String loMainValue = loFac + " " + loName;
-            String upMainValue = upFac + " " + upName;
+            final String loMainValue = loFac + " " + loName;
+            final String upMainValue = upFac + " " + upName;
 
             return new Object[] { loMainValue, upMainValue };
         }
 
-        Matcher mE = META_MAINVALUE_E.matcher(mainValueStr.trim());
+        final Matcher mE = META_MAINVALUE_E.matcher(mainValueStr.trim());
         if (mE.matches()) {
             log.debug("mainValueStr matches META_MAINVALUE_E");
-            String lower = mE.group(1);
-            String upper = mE.group(4);
+            final String lower = mE.group(1);
+            final String upper = mE.group(4);
 
             return new Object[] { lower, upper };
         }
 
-    log.debug("mainValueStr not matched");
+        log.debug("mainValueStr not matched");
         return null;
     }
 
 
-    protected Object[] handleGaugeString(String gaugeStr) {
-        Matcher m = META_GAUGE.matcher(gaugeStr);
+    protected Object[] handleGaugeString(final String gaugeStr) {
+        final Matcher m = META_GAUGE.matcher(gaugeStr);
 
         if (m.matches()) {
-            String name = m.group(1);
-            String qStr = m.group(2);
+            final String name = m.group(1);
+            final String qStr = m.group(2);
 
             try {
                 return new Object[] {
-                    name,
-                    new BigDecimal(nf.parse(qStr).doubleValue()) };
+                        name,
+                        AbstractParser.parseDecimal(qStr) };
             }
-            catch (ParseException pe) {
+            catch (final NumberFormatException pe) {
                 log.warn("Could not parse Q value: '" + qStr + "'");
             }
         }
@@ -246,8 +244,8 @@
     }
 
 
-    protected void handleDataLine(String line) {
-        String[] cols = line.split(SEPERATOR_CHAR);
+    protected void handleDataLine(final String line) {
+        final String[] cols = line.split(SEPERATOR_CHAR);
 
         if (cols.length < 5) {
             log.warn("skip invalid data line: '" + line + "'");
@@ -255,31 +253,25 @@
         }
 
         try {
-            double km = nf.parse(cols[0]).doubleValue();
+            final BigDecimal km = AbstractParser.parseDecimal(cols[0]);
 
-            Double key = Double.valueOf(km);
+            final Double key = Double.valueOf(km.doubleValue());
 
-            if (kmExists.contains(key)) {
+            if (this.kmExists.contains(key)) {
                 log.warn("duplicate station '" + km + "': -> ignored");
                 return;
             }
 
-            double q      = nf.parse(cols[1]).doubleValue();
-            double total  = nf.parse(cols[2]).doubleValue();
-            double main   = nf.parse(cols[3]).doubleValue();
-            double stress = nf.parse(cols[4]).doubleValue();
+            final BigDecimal q = AbstractParser.parseDecimal(cols[1]);
+            final BigDecimal total = AbstractParser.parseDecimal(cols[2]);
+            final BigDecimal main = AbstractParser.parseDecimal(cols[3]);
+            final BigDecimal stress = AbstractParser.parseDecimal(cols[4]);
 
-            current.addValue(new ImportFlowVelocityModelValue(
-                new BigDecimal(km),
-                new BigDecimal(q),
-                new BigDecimal(total),
-                new BigDecimal(main),
-                new BigDecimal(stress)
-            ));
+            this.current.addValue(new ImportFlowVelocityModelValue(km, q, total, main, stress));
 
-            kmExists.add(key);
+            this.kmExists.add(key);
         }
-        catch (ParseException pe) {
+        catch (final NumberFormatException pe) {
             log.warn("Unparseable flow velocity values:", pe);
         }
     }

http://dive4elements.wald.intevation.org