teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@8187: package org.dive4elements.river.backend.utils; sascha@4732: sascha@4732: import java.util.Comparator; tom@6971: import java.io.Serializable; sascha@4732: felix@4742: /** Comparator with some tolerance (epsilon). */ tom@6971: public class EpsilonComparator implements Comparator, Serializable sascha@4732: { sascha@4732: public static final double EPSILON = 1e-4; sascha@4732: felix@4742: /** Ready-made comparator with 1e-4 tolerance. */ sascha@4732: public static final EpsilonComparator CMP = new EpsilonComparator(EPSILON); sascha@4732: sascha@4732: private double epsilon; sascha@4732: sascha@4732: public EpsilonComparator(double epsilon) { sascha@4732: this.epsilon = epsilon; sascha@4732: } sascha@4732: sascha@4732: @Override sascha@4732: public int compare(Double a, Double b) { sascha@4732: double diff = a - b; sascha@4732: if (diff < -epsilon) return -1; sascha@4732: if (diff > epsilon) return +1; sascha@4732: return 0; sascha@4732: } sascha@4732: } sascha@4732: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :