[PATCH] (issue1767) Fix lookup of closest value

Wald Commits scm-commit at wald.intevation.org
Wed Nov 5 18:17:07 CET 2014


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1415198644 -3600
# Node ID 5fa1c8be43db99af4c060b43eb3a260f874dcc22
# Parent  cc53aae06303596d20ce87890b940ba8ff1999af
(issue1767) Fix lookup of closest value.

    If no match can be found stay on the same value. If an equal
    match can be found use it.

diff -r cc53aae06303 -r 5fa1c8be43db gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java	Wed Nov 05 13:04:53 2014 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java	Wed Nov 05 15:44:04 2014 +0100
@@ -281,9 +281,9 @@
      *           values with equal distance to \param to.
      * @return value from in that is closest to to, -1 if none.
      */
-    public static double closest(Double[] in, double to, boolean up) {
+    public static Double closest(Double[] in, double to, boolean up) {
         if (in == null || in.length == 0) {
-            return -1;
+            return Double.NaN;
         }
         Arrays.sort(in);
         GWT.log ("Closest match for " + (up ? "next" : "previous") + " value to: " +
@@ -291,19 +291,19 @@
         if (up) {
             // take the first one that is larger
             for (int i = 0; i < in.length; i++) {
-                if (in[i] > to) {
+                if (in[i] >= to) {
                     return in[i];
                 }
             }
         } else {
             for (int i = in.length - 1; i >= 0; i--) {
-                if (in[i] < to) {
+                if (in[i] <= to) {
                     return in[i];
                 }
             }
         }
         GWT.log("Failed to find closest match");
-        return 0;
+        return Double.NaN;
     }
 
 
@@ -447,8 +447,15 @@
                 @Override
                 public void onSuccess(Map<Integer, Double[]> obj) {
                     Double[] kms = obj.get(dbid);
-                    double closest =
+                    Double closest =
                         CrossSectionChartThemePanel.closest(kms, enteredKm, up);
+                    if (Double.isNaN(closest)) {
+                        GWT.log("Failed to find a closest km. Staying on current km.");
+                        updateCollection();
+                        enable();
+                        return;
+                    }
+
                     GWT.log("Got single km close to " + enteredKm + " for " + dbid + ", it is "
                         + closest);
 


More information about the Dive4Elements-commits mailing list