[PATCH] (issue1763) Reduce code-duplication (patch by Sascha Teichmann)

Wald Commits scm-commit at wald.intevation.org
Thu Apr 16 18:38:24 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1429202299 -7200
# Node ID ea676691e53382d7acd5fe8dacb17da274240196
# Parent  e1186c5ab169494322914ab8c7f91c634204b4c9
(issue1763) Reduce code-duplication (patch by Sascha Teichmann).

diff -r e1186c5ab169 -r ea676691e533 backend/src/main/java/org/dive4elements/river/importer/parsers/BedHeightParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/BedHeightParser.java	Wed Apr 15 18:36:11 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/BedHeightParser.java	Thu Apr 16 18:38:19 2015 +0200
@@ -383,6 +383,20 @@
         return false;
     }
 
+    private Double parse(String []values, int idx, String msg)  {
+
+        if (idx >= 0 && idx < values.length && !values[idx].isEmpty()) {
+            try {
+                return nf.parse(values[idx]).doubleValue();
+            }
+            catch (ParseException e) {
+                log.warn("BSP: unparseable " + msg + " '" + values[idx] + "'");
+            }
+        }
+
+        return null;
+    }
+
     protected void handleDataLine(ImportBedHeight obj, String line) {
         String[] values = line.split(SEPERATOR_CHAR, 0);
 
@@ -408,55 +422,13 @@
             return;
         }
 
-        Double height = null;
-        if (values[1].length() > 0) {
-            try {
-                height = new Double(nf.parse(values[1]).doubleValue());
-            }
-            catch (ParseException e) {
-                log.warn("BSP: unparseable height '" + values[1] + "'");
-            }
-        }
-
-        Double uncertainty = null;
-        if (values.length > 2 && values[2].length() > 0) {
-            try {
-                uncertainty = new Double(nf.parse(values[2]).doubleValue());
-            }
-            catch (ParseException e) {
-                log.warn("BSP: unparseable uncertainty value '"
-                    + values[2] + "'");
-            }
-        }
-
-        Double dataGap = null;
-        if (values.length > 3 && values[3].length() > 0) {
-            try {
-                dataGap = new Double(nf.parse(values[3]).doubleValue());
-            }
-            catch (ParseException e) {
-                log.warn("BSP: unparseable data gap '" + values[3] + "'");
-            }
-        }
-
-        Double soundingWidth = null;
-        if (values.length > 4 && values[4].length() > 0) {
-            try {
-                soundingWidth = new Double(nf.parse(values[4]).doubleValue());
-            }
-            catch (ParseException e) {
-                log.warn("BSP: unparseable sounding width '" +
-                    values[4] + "'");
-            }
-        }
-
         ImportBedHeightValue value = new ImportBedHeightValue(
             (ImportBedHeight) obj,
             km,
-            height,
-            uncertainty,
-            dataGap,
-            soundingWidth);
+            parse(values, 1, "height"),
+            parse(values, 2, "uncertainty"),
+            parse(values, 3, "data gap"),
+            parse(values, 4, "sounding width"));
 
         obj.addValue(value);
     }


More information about the Dive4Elements-commits mailing list