comparison flys-backend/src/main/java/de/intevation/flys/importer/ValueKey.java @ 3820:8a75cf0841b1 pre2.7-2012-03-16

merged flys-backend/pre2.7-2012-03-16
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:59 +0200
parents 6dc847194625
children
comparison
equal deleted inserted replaced
3818:dc18457b1cef 3820:8a75cf0841b1
1 package de.intevation.flys.importer;
2
3 import java.math.BigDecimal;
4
5 import java.util.Comparator;
6
7 public class ValueKey
8 {
9 public static final double EPSILON = 1e-6;
10
11 public static final Comparator<ValueKey> EPSILON_COMPARATOR =
12 new Comparator<ValueKey>()
13 {
14 public int compare(ValueKey x, ValueKey y) {
15 int cmp = ValueKey.compare(x.a, y.a);
16 if (cmp != 0) return cmp;
17 return ValueKey.compare(x.b, y.b);
18 }
19 };
20
21 public static int compare(BigDecimal a, BigDecimal b) {
22 if (a == null && b == null) return 0;
23 if (a != null && b == null) return +1;
24 if (a == null && b != null) return -1;
25
26 double diff = a.doubleValue() - b.doubleValue();
27 if (diff < -EPSILON) return -1;
28 return diff > EPSILON ? +1 : 0;
29 }
30
31 protected BigDecimal a;
32 protected BigDecimal b;
33
34 public ValueKey() {
35 }
36
37 public ValueKey(BigDecimal a, BigDecimal b) {
38 this.a = a;
39 this.b = b;
40 }
41
42 @Override
43 public int hashCode() {
44 return ((a != null ? a.hashCode() : 0) << 16)
45 | (b != null ? b.hashCode() : 0);
46 }
47
48 @Override
49 public boolean equals(Object other) {
50 if (!(other instanceof ValueKey)) {
51 return false;
52 }
53 ValueKey o = (ValueKey)other;
54 return !(
55 (a == null && o.a != null)
56 || (a != null && o.a == null)
57 || (a != null && !a.equals(o.a))
58 || (b == null && o.b != null)
59 || (b != null && o.b == null)
60 || (b != null && !b.equals(o.b)));
61 }
62 }
63 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org