changeset 8064:50102c41151e

Sediment load: Ensure that the lists of values are always ordered station wise. This was a hidden bug. :-/
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 30 Jul 2014 18:54:14 +0200
parents 41d9014bfa02
children fdb26fe898dc
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java
diffstat 1 files changed, 30 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java	Wed Jul 30 18:34:48 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java	Wed Jul 30 18:54:14 2014 +0200
@@ -11,6 +11,7 @@
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -69,6 +70,17 @@
         return densities;
     }
 
+    private static final Comparator<SedimentDensityValue> BY_KM =
+        new Comparator<SedimentDensityValue>() {
+            @Override
+            public int compare(SedimentDensityValue a, SedimentDensityValue b) {
+                double diff = a.getKm() - b.getKm();
+                if (diff < 0.0) return -1;
+                if (diff > 0.0) return +1;
+                return 0;
+            }
+        };
+
     public void addDensity(double km, double density, int year) {
 
         logger.debug("adding " + year);
@@ -82,7 +94,17 @@
             densities.put(key, list);
         }
 
-        list.add(new SedimentDensityValue(km, density, year));
+        // Keep list sorted by km.
+        SedimentDensityValue sdv = new SedimentDensityValue(km, density, year);
+        int index = Collections.binarySearch(list, sdv, BY_KM);
+
+        if (index < 0) {
+            // index = -(insertion point) - 1
+            // -(index + 1) = insertion point
+            index = -(index + 1);
+        }
+
+        list.add(index, sdv);
     }
 
     /**
@@ -102,6 +124,7 @@
                 ? getDensityAtKm(entry.getValue(), km)
                 : SEDIMNET_DENSITY_FACTOR;
         }
+
         Iterator<Map.Entry<Integer, List<SedimentDensityValue>>> iter =
             densities.entrySet().iterator();
 
@@ -146,19 +169,16 @@
     /** Get points  km,density (sorted by km), for a given year. */
     public double[][] getDensities(int year)
     {
-        TreeMap<Double, Double> map = new TreeMap<Double,Double>();
         List<SedimentDensityValue> list = densities.get(year);
         if (list == null) {
             return new double[2][0];
         }
-        for (SedimentDensityValue sdv: densities.get(year)) {
-            map.put(sdv.getKm(), sdv.getDensity());
-        }
-        double[][] points = new double[2][map.size()];
+        // List is sorted in km.
+        double[][] points = new double[2][list.size()];
         int i = 0;
-        for (Map.Entry<Double, Double> kmDens: map.entrySet()) {
-            points[0][i] = kmDens.getKey();
-            points[1][i] = kmDens.getValue();
+        for (SedimentDensityValue sdv: list) {
+            points[0][i] = sdv.getKm();
+            points[1][i] = sdv.getDensity();
             i++;
         }
 
@@ -166,14 +186,13 @@
     }
 
     /** Get value at km, interpolated. */
-    private double getDensityAtKm(
+    private static double getDensityAtKm(
         List<SedimentDensityValue> values,
         double km
     ) {
         SedimentDensityValue prev = null;
         SedimentDensityValue next = null;
         for (SedimentDensityValue sdv: values) {
-            logger.debug("year: " + sdv.getYear());
             if (Math.abs(sdv.getKm() - km) < 0.00001) {
                 return prev.getDensity();
             }

http://dive4elements.wald.intevation.org