diff backend/src/main/java/org/dive4elements/river/utils/EpsilonComparator.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-backend/src/main/java/org/dive4elements/river/utils/EpsilonComparator.java@18619c1e7c2a
children 4dd33b86dc61
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/utils/EpsilonComparator.java	Thu Apr 25 15:23:37 2013 +0200
@@ -0,0 +1,27 @@
+package org.dive4elements.river.utils;
+
+import java.util.Comparator;
+
+/** Comparator with some tolerance (epsilon). */
+public class EpsilonComparator implements Comparator<Double>
+{
+    public static final double EPSILON = 1e-4;
+
+    /** Ready-made comparator with 1e-4 tolerance. */
+    public static final EpsilonComparator CMP = new EpsilonComparator(EPSILON);
+
+    private double epsilon;
+
+    public EpsilonComparator(double epsilon) {
+        this.epsilon = epsilon;
+    }
+
+    @Override
+    public int compare(Double a, Double b) {
+        double diff = a - b;
+        if (diff < -epsilon) return -1;
+        if (diff >  epsilon) return +1;
+        return 0;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org