diff flys-backend/src/main/java/de/intevation/flys/importer/IdValueKey.java @ 500:d50cd3a632e0

Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too. flys-backend/trunk@1854 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 08 May 2011 22:41:07 +0000
parents 8ab04de0b879
children 04d449f7f0c9
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/IdValueKey.java	Sun May 08 21:34:43 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/IdValueKey.java	Sun May 08 22:41:07 2011 +0000
@@ -2,45 +2,48 @@
 
 import de.intevation.flys.model.WstColumnValue;
 import de.intevation.flys.model.DischargeTableValue;
+import de.intevation.flys.model.Range;
 
+import java.math.BigDecimal;
 
 public class IdValueKey {
 
-    protected int    id;
-    protected double a;
-    protected double b;
+    protected int        id;
+    protected BigDecimal a;
+    protected BigDecimal b;
 
 
     public IdValueKey(WstColumnValue value) {
         this.id = value.getWstColumn().getId();
-        this.a  = value.getPosition().doubleValue();
-        this.b  = value.getW().doubleValue();
+        this.a  = value.getPosition();
+        this.b  = value.getW();
     }
 
     public IdValueKey(DischargeTableValue value) {
         this.id = value.getDischargeTable().getId();
-        this.a  = value.getQ().doubleValue();
-        this.b  = value.getW().doubleValue();
+        this.a  = value.getQ();
+        this.b  = value.getW();
+    }
+
+    public IdValueKey(Range value) {
+        this.id = value.getRiver().getId();
+        this.a  = value.getA();
+        this.b  = value.getB();
     }
 
 
-    public IdValueKey(int id, double a, double b) {
+    public IdValueKey(int id, BigDecimal a, BigDecimal b) {
         this.id = id;
         this.a  = a;
         this.b  = b;
     }
 
-    private static final int hashDouble(double d) {
-        long v = Double.doubleToLongBits(d);
-        return (int)(v^(v>>>32));
+    private static final int hashCode(BigDecimal d) {
+        return d != null ? d.hashCode() : 0;
     }
 
     public int hashCode() {
-        int a = id;
-        int b = hashDouble(a);
-        int c = hashDouble(b);
-
-        return (a | (b << 10) | (c << 20));
+        return id | (hashCode(a) << 10) | (hashCode(b) << 20);
     }
 
 
@@ -51,9 +54,13 @@
 
         IdValueKey other = (IdValueKey) obj;
 
-        return id == other.id
-            && a  == other.a
-            && b  == other.b;
+        return !((id != other.id) 
+            || (a == null && other.a != null) 
+            || (a != null && other.a == null) 
+            || (a != null && !a.equals(other)) 
+            || (b == null && other.b != null) 
+            || (b != null && other.b == null) 
+            || (b != null && !b.equals(other)));
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org