view flys-artifacts/src/main/java/de/intevation/flys/utils/EpsilonComparator.java @ 3018:fb07f936b5ad

New helper. flys-artifacts/trunk@4584 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 05 Jun 2012 13:28:11 +0000
parents 0330bff1b382
children
line wrap: on
line source
package de.intevation.flys.utils;

import java.io.Serializable;

import java.util.Comparator;

public class EpsilonComparator
implements   Serializable, Comparator<Double>
{
    public static final double EPSILON = 1e-5;

    public static final EpsilonComparator INSTANCE =
        new EpsilonComparator(EPSILON);

    protected double epsilon;

    public EpsilonComparator() {
        this(EPSILON);
    }

    public EpsilonComparator(double epsilon) {
        this.epsilon = Math.abs(epsilon);
    }

    private static final double value(Double x) {
        return x != null ? x.doubleValue() : 0.0;
    }

    @Override
    public int compare(Double a, Double b) {
        double diff = value(a) - value(b);
        if (diff < -epsilon) return -1;
        return diff > epsilon ? +1 : 0;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org