comparison 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
comparison
equal deleted inserted replaced
499:cce054f27dac 500:d50cd3a632e0
1 package de.intevation.flys.importer; 1 package de.intevation.flys.importer;
2 2
3 import de.intevation.flys.model.WstColumnValue; 3 import de.intevation.flys.model.WstColumnValue;
4 import de.intevation.flys.model.DischargeTableValue; 4 import de.intevation.flys.model.DischargeTableValue;
5 import de.intevation.flys.model.Range;
5 6
7 import java.math.BigDecimal;
6 8
7 public class IdValueKey { 9 public class IdValueKey {
8 10
9 protected int id; 11 protected int id;
10 protected double a; 12 protected BigDecimal a;
11 protected double b; 13 protected BigDecimal b;
12 14
13 15
14 public IdValueKey(WstColumnValue value) { 16 public IdValueKey(WstColumnValue value) {
15 this.id = value.getWstColumn().getId(); 17 this.id = value.getWstColumn().getId();
16 this.a = value.getPosition().doubleValue(); 18 this.a = value.getPosition();
17 this.b = value.getW().doubleValue(); 19 this.b = value.getW();
18 } 20 }
19 21
20 public IdValueKey(DischargeTableValue value) { 22 public IdValueKey(DischargeTableValue value) {
21 this.id = value.getDischargeTable().getId(); 23 this.id = value.getDischargeTable().getId();
22 this.a = value.getQ().doubleValue(); 24 this.a = value.getQ();
23 this.b = value.getW().doubleValue(); 25 this.b = value.getW();
26 }
27
28 public IdValueKey(Range value) {
29 this.id = value.getRiver().getId();
30 this.a = value.getA();
31 this.b = value.getB();
24 } 32 }
25 33
26 34
27 public IdValueKey(int id, double a, double b) { 35 public IdValueKey(int id, BigDecimal a, BigDecimal b) {
28 this.id = id; 36 this.id = id;
29 this.a = a; 37 this.a = a;
30 this.b = b; 38 this.b = b;
31 } 39 }
32 40
33 private static final int hashDouble(double d) { 41 private static final int hashCode(BigDecimal d) {
34 long v = Double.doubleToLongBits(d); 42 return d != null ? d.hashCode() : 0;
35 return (int)(v^(v>>>32));
36 } 43 }
37 44
38 public int hashCode() { 45 public int hashCode() {
39 int a = id; 46 return id | (hashCode(a) << 10) | (hashCode(b) << 20);
40 int b = hashDouble(a);
41 int c = hashDouble(b);
42
43 return (a | (b << 10) | (c << 20));
44 } 47 }
45 48
46 49
47 public boolean equals(Object obj) { 50 public boolean equals(Object obj) {
48 if (!(obj instanceof IdValueKey)) { 51 if (!(obj instanceof IdValueKey)) {
49 return false; 52 return false;
50 } 53 }
51 54
52 IdValueKey other = (IdValueKey) obj; 55 IdValueKey other = (IdValueKey) obj;
53 56
54 return id == other.id 57 return !((id != other.id)
55 && a == other.a 58 || (a == null && other.a != null)
56 && b == other.b; 59 || (a != null && other.a == null)
60 || (a != null && !a.equals(other))
61 || (b == null && other.b != null)
62 || (b != null && other.b == null)
63 || (b != null && !b.equals(other)));
57 } 64 }
58 } 65 }
59 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 66 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org