diff flys-backend/src/main/java/de/intevation/flys/importer/ValueKey.java @ 503:bcc18293a547

Importer: Added unsharp lookup to avoid numerical problems. flys-backend/trunk@1859 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 09 May 2011 10:04:23 +0000
parents 04d449f7f0c9
children 6dc847194625
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ValueKey.java	Mon May 09 00:38:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ValueKey.java	Mon May 09 10:04:23 2011 +0000
@@ -2,8 +2,32 @@
 
 import java.math.BigDecimal;
 
+import java.util.Comparator;
+
 public class ValueKey
 {
+    public static final double EPSILON = 1e-6;
+
+    public static final Comparator<ValueKey> EPSILON_COMPARATOR =
+        new Comparator<ValueKey>() 
+    {
+        public int compare(ValueKey x, ValueKey y) {
+            int cmp = ValueKey.compare(x.a, y.a);
+            if (cmp != 0) return cmp;
+            return ValueKey.compare(x.b, y.b);
+        }
+    };
+
+    public static int compare(BigDecimal a, BigDecimal b) {
+        if (a == null && b == null) return  0;
+        if (a != null && b == null) return +1;
+        if (a == null && b != null) return -1;
+
+        double diff = a.doubleValue() - b.doubleValue();
+        if (diff < -EPSILON) return -1;
+        return diff > EPSILON ? +1 : 0;
+    }
+
     protected BigDecimal a;
     protected BigDecimal b;
 

http://dive4elements.wald.intevation.org