view backend/src/main/java/org/dive4elements/river/importer/ImportRange.java @ 9709:b74f817435fe

comment removed
author dnt_bjoernsen <d.tironi@bjoernsen.de>
date Wed, 27 Jan 2021 11:47:38 +0100
parents e541938dd3ab
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.importer;

import java.math.BigDecimal;

import org.apache.log4j.Logger;
import org.dive4elements.river.model.Range;
import org.dive4elements.river.model.River;

/** A range that is about to be imported. */
public class ImportRange
implements   Comparable<ImportRange>
{
    /** Private log. */
    private static Logger log = Logger.getLogger(ImportRange.class);

    protected BigDecimal a;
    protected BigDecimal b;

    protected Range peer;

    public ImportRange() {
    }

    public ImportRange(final BigDecimal a) {
        this.a = a;
        this.b = null;
    }

    public ImportRange(BigDecimal a, BigDecimal b) {

        // enforce a<b and set only a for zero-length ranges
        if (a != null && b == null) {
            this.a = a;
            this.b = null;
        }
        else if (a == null && b != null) {
            this.a = b;
            this.b = null;
        }
        else if (a == null && b == null) {
            throw new IllegalArgumentException("Both a and b are null.");
        }
        else if (a == b) {
            this.a = a;
            this.b = null;
        }
        else {
            if (a.compareTo(b) > 0) {
                final BigDecimal t = a; a = b; b = t;
            }
            this.a = a;
            this.b = b;
        }
    }

    private static final int compare(final BigDecimal a, final BigDecimal b) {
        if (a == null && b == null) {
            return 0;
        }
        if (a == null && b != null) {
            return -1;
        }
        if (a != null && b == null) {
            return +1;
        }
        return a.compareTo(b);
    }

    @Override
    public int compareTo(final ImportRange other) {
        final int cmp = compare(this.a, other.a);
        if (cmp != 0) return cmp;
        return compare(this.b, other.b);
    }

    public BigDecimal getA() {
        return this.a;
    }

    public void setA(final BigDecimal a) {
        if (this.b != null && a.compareTo(this.b) >= 0) {
            throw new IllegalArgumentException(
                    "a (" + a + ") must be smaller than b (" + this.b + ").");
        }
        this.a = a;
    }

    public BigDecimal getB() {
        return this.b;
    }

    public void setB(final BigDecimal b) {
        if (b != null && b.compareTo(this.a) <= 0) {
            throw new IllegalArgumentException(
                    "b (" + b + ") must be greater than a (" + this.a + ") or null.");
        }
        this.b = b;
    }

    /**
     * Difference of a and b
     *
     * @return b - a, or NaN if a or b null
     */
    public double difference() {
        if ((this.a != null) && (this.b != null))
            return this.b.subtract(this.a).doubleValue();
        else
            return Double.NaN;
    }

    public Range getPeer(final River river) {
        if (this.peer == null) {
            this.peer = ImporterSession.getInstance().getRange(river, this.a, this.b);
        }
        return this.peer;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org