[PATCH 2 of 3] Parse via decimalformat instead of bigdecimal constructor (to cope with strings with chars after the actual number)

Wald Commits scm-commit at wald.intevation.org
Tue Apr 10 16:45:12 CEST 2018


# HG changeset patch
# User mschaefer
# Date 1523371412 -7200
# Node ID c43d8c1a44559efd72ac0d30558ead3b9086a1f8
# Parent  07dcedddf839b01a957d0a8430b85f847244e168
Parse via decimalformat instead of bigdecimal constructor (to cope with strings with chars after the actual number)

diff -r 07dcedddf839 -r c43d8c1a4455 backend/src/main/java/org/dive4elements/river/importer/common/AbstractParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/common/AbstractParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/common/AbstractParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -17,6 +17,7 @@
 import java.io.InputStreamReader;
 import java.io.LineNumberReader;
 import java.math.BigDecimal;
+import java.text.DecimalFormat;
 import java.text.NumberFormat;
 import java.text.ParseException;
 import java.util.ArrayList;
@@ -73,6 +74,13 @@
 
     private static NumberFormat numberFormat = NumberFormat.getInstance(Locale.ROOT);
 
+    private static DecimalFormat bigDecimalFormat;
+
+    static {
+        bigDecimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.ROOT);
+        bigDecimalFormat.setParseBigDecimal(true);
+    }
+
     /**
      * Path of the file or directory to import from
      */
@@ -265,8 +273,8 @@
     /**
      * Parses a number string as a BigDecimal, replacing a comma with a dot first
      */
-    public static BigDecimal parseDecimal(final String text) throws NumberFormatException {
-        return new BigDecimal(text.replace(',', '.'));
+    public static BigDecimal parseDecimal(final String text) throws ParseException {
+        return (BigDecimal) bigDecimalFormat.parse(text.replace(',', '.'));
     }
 
     /**
diff -r 07dcedddf839 -r c43d8c1a4455 backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityModelParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 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;
@@ -235,7 +236,7 @@
                         name,
                         AbstractParser.parseDecimal(qStr) };
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Could not parse Q value: '" + qStr + "'");
             }
         }
@@ -271,7 +272,7 @@
 
             this.kmExists.add(key);
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Unparseable flow velocity values:", pe);
         }
     }
diff -r 07dcedddf839 -r c43d8c1a4455 backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -126,7 +126,7 @@
             }
 
         }
-        catch (final NumberFormatException e) {
+        catch (final ParseException e) {
             log.error("Unparseable station in line " + lineNum +
                     ". Error: " + e.getMessage());
             return null;
diff -r 07dcedddf839 -r c43d8c1a4455 backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/MorphologicalWidthParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -10,6 +10,7 @@
 
 import java.math.BigDecimal;
 import java.text.NumberFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -113,7 +114,7 @@
                     desc
                     ));
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("MWP: unparseable number in data row: " + line);
         }
     }
diff -r 07dcedddf839 -r c43d8c1a4455 backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 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.regex.Matcher;
@@ -113,7 +114,7 @@
 
                 return true;
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Unparseable numbers in: '" + line + "'");
             }
         }
@@ -159,7 +160,7 @@
                 shoreOffset = AbstractParser.parseDecimal(vals[1]);
             }
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Unparseable numbers in '" + line + "'");
         }
 
@@ -173,7 +174,7 @@
             try {
                 year = AbstractParser.parseDecimal(this.yearString);
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Unparseable year string");
             }
         }
diff -r 07dcedddf839 -r c43d8c1a4455 backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 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.regex.Matcher;
@@ -243,13 +244,13 @@
                                 station,
                                 AbstractParser.parseDecimal(value));
                     }
-                    catch (final NumberFormatException pe) {
+                    catch (final ParseException pe) {
                         log.warn("Could not parse value: '" + value + "'");
                     }
                 }
             }
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Could not parse station: '" + line + "'");
         }
     }
diff -r 07dcedddf839 -r c43d8c1a4455 backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java	Tue Apr 10 16:40:52 2018 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java	Tue Apr 10 16:43:32 2018 +0200
@@ -12,6 +12,7 @@
 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.regex.Matcher;
@@ -195,7 +196,7 @@
 
                 return true;
             }
-            catch (final NumberFormatException pe) {
+            catch (final ParseException pe) {
                 log.warn("Unparseable Q range: '" + line + "'");
             }
         }
@@ -223,7 +224,7 @@
                 this.currentRange.setA(station);
             }
         }
-        catch (final NumberFormatException pe) {
+        catch (final ParseException pe) {
             log.warn("Unparseable number in data row: " + line);
         }
     }


More information about the Dive4Elements-commits mailing list