diff backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.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 8fbc0649da13
children c43d8c1a4455
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Sun Apr 08 18:09:32 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Mon Apr 09 09:07:00 2018 +0200
@@ -8,19 +8,17 @@
 
 package org.dive4elements.river.importer.parsers;
 
-import java.math.BigDecimal;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
 import org.apache.log4j.Logger;
-
-import org.dive4elements.river.model.MeasurementStation;
-
 import org.dive4elements.river.importer.ImportMeasurementStation;
 import org.dive4elements.river.importer.ImportRange;
 import org.dive4elements.river.importer.ImportTimeInterval;
+import org.dive4elements.river.importer.common.AbstractParser;
+import org.dive4elements.river.model.MeasurementStation;
 
 
 public class MeasurementStationsParser extends LineParser {
@@ -29,7 +27,7 @@
 
         private static final long serialVersionUID = 1L;
 
-        public MeasurementStationParserException(String msg) {
+        public MeasurementStationParserException(final String msg) {
             super(msg);
         }
     }
@@ -39,14 +37,14 @@
     public static final int MAX_COMMENT_LENGTH = 512;
 
     private static final Logger log = Logger
-        .getLogger(MeasurementStationsParser.class);
+            .getLogger(MeasurementStationsParser.class);
 
     private List<ImportMeasurementStation> measurementStations;
     private ImportMeasurementStation current;
 
     @Override
     protected void reset() {
-        this.measurementStations = new ArrayList<ImportMeasurementStation>();
+        this.measurementStations = new ArrayList<>();
     }
 
     @Override
@@ -54,114 +52,109 @@
     }
 
     @Override
-    protected void handleLine(int lineNum, String line) {
+    protected void handleLine(final int lineNum, final String line) {
         if (line == null || line.startsWith(START_META_CHAR)) {
             log.info("skip meta information at line " + lineNum);
             return;
         }
 
         try {
-            current = new ImportMeasurementStation();
+            this.current = new ImportMeasurementStation();
             handleDataLine(lineNum, line);
-            measurementStations.add(current);
+            this.measurementStations.add(this.current);
         }
-        catch (MeasurementStationParserException e) {
+        catch (final MeasurementStationParserException e) {
             log.warn("Problem in line " + lineNum + ": " + e.getMessage());
         }
     }
 
     public List<ImportMeasurementStation> getMeasurementStations() {
-        return measurementStations;
+        return this.measurementStations;
     }
 
-    protected void handleDataLine(int lineNum, String line)
-        throws MeasurementStationParserException {
-        String[] cols = line.split(SEPERATOR_CHAR);
+    protected void handleDataLine(final int lineNum, final String line)
+            throws MeasurementStationParserException {
+        final String[] cols = line.split(SEPERATOR_CHAR);
 
         if (cols == null || cols.length < MIN_COLUMNS) {
-            int num = cols != null ? cols.length : 0;
+            final int num = cols != null ? cols.length : 0;
             throw new MeasurementStationParserException("Not enough columns: "
-                + num);
+                    + num);
         }
 
-        current.name = getName(cols, lineNum);
-        current.range = getRange(cols, lineNum);
-        current.measurementType = getMeasurementType(cols, lineNum);
-        current.riverside = getRiverside(cols, lineNum);
-        current.gauge = getGauge(cols, lineNum);
-        current.observationTimerange = getObservationTimerange(cols, lineNum);
-        current.operator = getOperator(cols, lineNum);
-        current.comment = getComment(cols, lineNum);
+        this.current.name = getName(cols, lineNum);
+        this.current.range = getRange(cols, lineNum);
+        this.current.measurementType = getMeasurementType(cols, lineNum);
+        this.current.riverside = getRiverside(cols, lineNum);
+        this.current.gauge = getGauge(cols, lineNum);
+        this.current.observationTimerange = getObservationTimerange(cols, lineNum);
+        this.current.operator = getOperator(cols, lineNum);
+        this.current.comment = getComment(cols, lineNum);
     }
 
-    protected String getName(String[] cols, int lineNum)
-        throws MeasurementStationParserException {
+    protected String getName(final String[] cols, final int lineNum)
+            throws MeasurementStationParserException {
         if (cols[0] == null || cols[0].length() == 0) {
             throw new MeasurementStationParserException("invalid name in line "
-                + lineNum);
+                    + lineNum);
         }
 
         return cols[0];
     }
 
-    protected ImportRange getRange(String[] cols, int lineNum) {
-        String from = cols[1];
-        String to   = cols[4];
+    protected ImportRange getRange(final String[] cols, final int lineNum) {
+        final String from = cols[1];
+        final String to   = cols[4];
         if (from == null || from.length() == 0) {
             log.error("No station found in line" + lineNum);
             return null;
         }
 
         try {
-            double lower = getDouble(from);
-
             if (to == null || to.length() == 0) {
                 log.warn("No end km found in line " + lineNum);
-                return new ImportRange(new BigDecimal(lower));
+                return new ImportRange(AbstractParser.parseDecimal(from));
             }
 
             try {
-                double upper = getDouble(to);
-
-                return new ImportRange(new BigDecimal(lower),
-                    new BigDecimal(upper));
+                return new ImportRange(AbstractParser.parseDecimal(from), AbstractParser.parseDecimal(to));
             }
-            catch (ParseException e) {
+            catch (final NumberFormatException e) {
                 log.warn("Unparseable end km in line " + lineNum +
-                    ". Error: " + e.getMessage());
-                return new ImportRange(new BigDecimal(lower));
+                        ". Error: " + e.getMessage());
+                return new ImportRange(AbstractParser.parseDecimal(from));
             }
 
         }
-        catch (ParseException e) {
+        catch (final NumberFormatException e) {
             log.error("Unparseable station in line " + lineNum +
                     ". Error: " + e.getMessage());
             return null;
         }
     }
 
-    protected String getMeasurementType(String[] cols, int lineNum)
-        throws MeasurementStationParserException {
-        String mtype = cols[2].trim();
+    protected String getMeasurementType(final String[] cols, final int lineNum)
+            throws MeasurementStationParserException {
+        final String mtype = cols[2].trim();
         if (!(MeasurementStation.MEASUREMENT_TYPE_BEDLOAD.equals(mtype) ||
                 MeasurementStation.MEASUREMENT_TYPE_SUSP.equals(mtype))) {
             throw new MeasurementStationParserException(
-                "invalid measurement type in line " + lineNum);
+                    "invalid measurement type in line " + lineNum);
         }
 
         return mtype;
     }
 
-    protected String getRiverside(String[] cols, int lineNum) {
-        String col = cols[3];
+    protected String getRiverside(final String[] cols, final int lineNum) {
+        final String col = cols[3];
         if (col == null || col.length() == 0) {
             log.warn("No river side given in line " + lineNum);
         }
         return col;
     }
 
-    protected String getGauge(String[] cols, int lineNum) {
-        String col = cols[5];
+    protected String getGauge(final String[] cols, final int lineNum) {
+        final String col = cols[5];
         if (col == null || col.length() == 0) {
             log.warn("Invalid gauge found in line " + lineNum);
         }
@@ -169,45 +162,45 @@
     }
 
     protected ImportTimeInterval getObservationTimerange(
-        String[] cols,
-        int lineNum
-    ) {
-        String col = cols[7];
+            final String[] cols,
+            final int lineNum
+            ) {
+        final String col = cols[7];
         if (col == null || col.length() == 0) {
             log.warn("Observation time invalid in line " + lineNum);
             return null;
         }
 
         try {
-            Date date = getDate(col);
+            final Date date = getDate(col);
 
             if (date != null) {
                 return new ImportTimeInterval(date);
             }
             log.warn("Observation time invalid in line " + lineNum);
         }
-        catch (ParseException pe) {
+        catch (final ParseException pe) {
             log.warn("Unparseable observation time '" + col +
-                "' in line " + lineNum);
+                    "' in line " + lineNum);
         }
         return null;
     }
 
-    protected String getOperator(String[] cols, int lineNum) {
-        String col = cols[8];
+    protected String getOperator(final String[] cols, final int lineNum) {
+        final String col = cols[8];
         if (col == null || col.length() == 0) {
             log.warn("No operator given in line " + lineNum);
         }
         return col;
     }
 
-    protected String getComment(String[] cols, int lineNum) {
+    protected String getComment(final String[] cols, final int lineNum) {
         if (cols.length > MIN_COLUMNS) {
-            String col = cols[9];
+            final String col = cols[9];
             if (col.length() > MAX_COMMENT_LENGTH) {
                 log.warn("Comment in line " + lineNum +
-                    " longer than allowed " + MAX_COMMENT_LENGTH +
-                    " characters. Truncated.");
+                        " longer than allowed " + MAX_COMMENT_LENGTH +
+                        " characters. Truncated.");
                 return col.substring(0, MAX_COMMENT_LENGTH);
             }
             return col;

http://dive4elements.wald.intevation.org