[PATCH] (issue1767) Fix closet next value selection

Wald Commits scm-commit at wald.intevation.org
Tue Nov 4 17:09:18 CET 2014


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1415117344 -3600
# Node ID 2772a0fc740082c648dfe9929fca98052f0a82fe
# Parent  5dd6cc9fec1e764e36a0227c57be7983553ae459
(issue1767) Fix closet next value selection.

    This simplyfies the code and thus fixes the overly
    complicated old version.

diff -r 5dd6cc9fec1e -r 2772a0fc7400 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	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;
     }
 
 


More information about the Dive4Elements-commits mailing list