changeset 8467:5fa1c8be43db

(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.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 05 Nov 2014 15:44:04 +0100
parents cc53aae06303
children 67ae70259011
files gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java
diffstat 1 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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);
 

http://dive4elements.wald.intevation.org