Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ValueKey.java @ 7730:e1b831fe435a slt-simplify-cross-sections
Merged default into slt-simplify-cross-sections branch and updated package and class names.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Mon, 20 Jan 2014 14:04:20 +0100 |
parents | 4c3ccf2b0304 |
children |
comparison
equal
deleted
inserted
replaced
5084:ca45dd039b54 | 7730:e1b831fe435a |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.importer; | |
10 | |
11 import java.math.BigDecimal; | |
12 | |
13 import java.util.Comparator; | |
14 | |
15 public class ValueKey | |
16 { | |
17 public static final double EPSILON = 1e-6; | |
18 | |
19 public static final Comparator<ValueKey> EPSILON_COMPARATOR = | |
20 new Comparator<ValueKey>() | |
21 { | |
22 public int compare(ValueKey x, ValueKey y) { | |
23 int cmp = ValueKey.compare(x.a, y.a); | |
24 if (cmp != 0) return cmp; | |
25 return ValueKey.compare(x.b, y.b); | |
26 } | |
27 }; | |
28 | |
29 public static int compare(BigDecimal a, BigDecimal b) { | |
30 if (a == null && b == null) return 0; | |
31 if (a != null && b == null) return +1; | |
32 if (a == null && b != null) return -1; | |
33 | |
34 double diff = a.doubleValue() - b.doubleValue(); | |
35 if (diff < -EPSILON) return -1; | |
36 return diff > EPSILON ? +1 : 0; | |
37 } | |
38 | |
39 protected BigDecimal a; | |
40 protected BigDecimal b; | |
41 | |
42 public ValueKey() { | |
43 } | |
44 | |
45 public ValueKey(BigDecimal a, BigDecimal b) { | |
46 this.a = a; | |
47 this.b = b; | |
48 } | |
49 | |
50 @Override | |
51 public int hashCode() { | |
52 return ((a != null ? a.hashCode() : 0) << 16) | |
53 | (b != null ? b.hashCode() : 0); | |
54 } | |
55 | |
56 @Override | |
57 public boolean equals(Object other) { | |
58 if (!(other instanceof ValueKey)) { | |
59 return false; | |
60 } | |
61 ValueKey o = (ValueKey)other; | |
62 return !( | |
63 (a == null && o.a != null) | |
64 || (a != null && o.a == null) | |
65 || (a != null && !a.equals(o.a)) | |
66 || (b == null && o.b != null) | |
67 || (b != null && o.b == null) | |
68 || (b != null && !b.equals(o.b))); | |
69 } | |
70 } | |
71 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |