changeset 8465:2772a0fc7400

(issue1767) Fix closet next value selection. This simplyfies the code and thus fixes the overly complicated old version.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 04 Nov 2014 17:09:04 +0100
parents 5dd6cc9fec1e
children cc53aae06303
files gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java
diffstat 1 files changed, 17 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java	Tue Nov 04 15:32:56 2014 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/CrossSectionChartThemePanel.java	Tue Nov 04 17:09:04 2014 +0100
@@ -44,6 +44,7 @@
 import org.dive4elements.river.client.shared.model.ThemeList;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -284,30 +285,25 @@
         if (in == null || in.length == 0) {
             return -1;
         }
-        if (in[0] == to) {
-            return to;
-        }
-        for (int i = 0; i < in.length; i++) {
-            GWT.log ("Close? " + in[i]);
-        }
-
-        double minDiff = Math.abs(to - in[0]);
-        double bestMatch = in[0];
-        for (int i = 1; i < in.length; i++) {
-            if (in[i] == to) {
-                return to;
+        Arrays.sort(in);
+        GWT.log ("Closest match for " + (up ? "next" : "previous") + " value to: " +
+                to + " candidates: " + Arrays.toString(in));
+        if (up) {
+            // take the first one that is larger
+            for (int i = 0; i < in.length; i++) {
+                if (in[i] > to) {
+                    return in[i];
+                }
             }
-            if ((in[i] < to && up)
-                || (in[i] > to && !up)) {
-                continue;
-            }
-            double diff = Math.abs(to - in[i]);
-            if (diff <= minDiff) {
-                minDiff = diff;
-                bestMatch = in[i];
+        } else {
+            for (int i = in.length - 1; i >= 0; i--) {
+                if (in[i] < to) {
+                    return in[i];
+                }
             }
         }
-        return bestMatch;
+        GWT.log("Failed to find closest match");
+        return 0;
     }
 
 

http://dive4elements.wald.intevation.org