sascha@3011: package de.intevation.flys.utils; sascha@3011: sascha@3011: import java.io.Serializable; sascha@3011: sascha@3011: import java.util.Comparator; sascha@3011: sascha@3011: public class EpsilonComparator sascha@3011: implements Serializable, Comparator sascha@3011: { sascha@3011: public static final double EPSILON = 1e-5; sascha@3011: sascha@3011: public static final EpsilonComparator INSTANCE = sascha@3011: new EpsilonComparator(EPSILON); sascha@3011: sascha@3011: protected double epsilon; sascha@3011: sascha@3011: public EpsilonComparator() { sascha@3011: this(EPSILON); sascha@3011: } sascha@3011: sascha@3011: public EpsilonComparator(double epsilon) { sascha@3011: this.epsilon = Math.abs(epsilon); sascha@3011: } sascha@3011: sascha@3011: private static final double value(Double x) { sascha@3011: return x != null ? x.doubleValue() : 0.0; sascha@3011: } sascha@3011: sascha@3011: @Override sascha@3011: public int compare(Double a, Double b) { sascha@3011: double diff = value(a) - value(b); sascha@3011: if (diff < epsilon) return -1; sascha@3011: return diff > epsilon ? +1 : 0; sascha@3011: } sascha@3011: } sascha@3011: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :