felix@4742: package de.intevation.flys.utils;
sascha@4732: 
sascha@4732: import java.util.Comparator;
sascha@4732: 
felix@4742: /** Comparator with some tolerance (epsilon). */
sascha@4732: public class EpsilonComparator implements Comparator<Double>
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 :