sascha@501: package de.intevation.flys.importer; sascha@501: sascha@501: import java.math.BigDecimal; sascha@501: sascha@503: import java.util.Comparator; sascha@503: sascha@501: public class ValueKey sascha@501: { sascha@503: public static final double EPSILON = 1e-6; sascha@503: sascha@503: public static final Comparator EPSILON_COMPARATOR = sascha@771: new Comparator() sascha@503: { sascha@503: public int compare(ValueKey x, ValueKey y) { sascha@503: int cmp = ValueKey.compare(x.a, y.a); sascha@503: if (cmp != 0) return cmp; sascha@503: return ValueKey.compare(x.b, y.b); sascha@503: } sascha@503: }; sascha@503: sascha@503: public static int compare(BigDecimal a, BigDecimal b) { sascha@503: if (a == null && b == null) return 0; sascha@503: if (a != null && b == null) return +1; sascha@503: if (a == null && b != null) return -1; sascha@503: sascha@503: double diff = a.doubleValue() - b.doubleValue(); sascha@503: if (diff < -EPSILON) return -1; sascha@503: return diff > EPSILON ? +1 : 0; sascha@503: } sascha@503: sascha@501: protected BigDecimal a; sascha@501: protected BigDecimal b; sascha@501: sascha@501: public ValueKey() { sascha@501: } sascha@501: sascha@501: public ValueKey(BigDecimal a, BigDecimal b) { sascha@501: this.a = a; sascha@501: this.b = b; sascha@501: } sascha@501: sascha@501: @Override sascha@501: public int hashCode() { sascha@501: return ((a != null ? a.hashCode() : 0) << 16) sascha@501: | (b != null ? b.hashCode() : 0); sascha@501: } sascha@501: sascha@501: @Override sascha@501: public boolean equals(Object other) { sascha@501: if (!(other instanceof ValueKey)) { sascha@501: return false; sascha@501: } sascha@501: ValueKey o = (ValueKey)other; sascha@501: return !( sascha@771: (a == null && o.a != null) sascha@771: || (a != null && o.a == null) sascha@771: || (a != null && !a.equals(o.a)) sascha@771: || (b == null && o.b != null) sascha@501: || (b != null && o.b == null) sascha@501: || (b != null && !b.equals(o.b))); sascha@501: } sascha@501: } sascha@501: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :