All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yamcs.utils.DoubleRange Maven / Gradle / Ivy

There is a newer version: 5.10.1
Show newest version
package org.yamcs.utils;

import java.io.Serializable;

/**
 * A range of numbers [min, max) where both min and max can be inclusive or exclusive.
 *  
 *  Both min and max can be Double.NaN meaning that the range is open at that end.
 * @author nm
 *
 */
public class DoubleRange implements Serializable {
    

    private static final long serialVersionUID = 3L;

    final double min;
    final double max;
    final boolean minIncl;
    final boolean maxIncl;

    public DoubleRange(double min, double max, boolean minIncl, boolean maxIncl) {
        this.min = min;
        this.max = max;
        this.minIncl = minIncl;
        this.maxIncl = maxIncl;
    }

    public DoubleRange(double minInclusive, double maxInclusive) {
        this.min = minInclusive;
        this.max = maxInclusive;
        this.minIncl = true;
        this.maxIncl = true;
    }

    /**
     * Returns a range from the XTCE float range used for alarms which is in fact a union of two ranges
     * @param minExclusive
     * @param maxExclusive
     * @param minInclusive
     * @param maxInclusive
     * @return
     */
    public static DoubleRange fromXtceComplement(double minExclusive, double maxExclusive, double minInclusive, double maxInclusive) {
        double min = minExclusive;
        double max = maxExclusive;
        boolean minIncl = true;
        boolean maxIncl = true;

        if(!Double.isNaN(minInclusive)) {
            min = minInclusive;
            minIncl = false;
        }
        if(!Double.isNaN(maxInclusive)) {
            max = maxInclusive;
            maxIncl = false;
        }

        return new DoubleRange(min, max, minIncl, maxIncl);
    }

    public double getMax() {
        return max;
    }
    public double getMin() {
        return min;
    }

    public boolean isMinInclusive() {
        return minIncl;
    }
    public boolean isMaxInclusive() {
        return maxIncl;
    }
    /**
     * Checks if the value is in range.
     * 
     * @param v
     * @return <0 =0 or >0 if the value v is lower than min, between min and max or greater than max respectively.
     */
    public int inRange(double v) {
        if(!Double.isNaN(min) && ((minIncl && vmax) || (!maxIncl && v>=max))) {
            return 1;
        }

        return 0;
    }

    /**
     * E.g. a low limit of ]-Infinity, -22] and a high limit of [40, +Infinity[ 
     * intersect to [-22, 40] (which for practical purposes is actually the range
     * inside of which pvals are _not_ out of limits)
     */
    public DoubleRange intersectWith(DoubleRange other) {
        double xmin = Double.NEGATIVE_INFINITY;
        boolean xminExcl = true;
        if(!Double.isNaN(min) && min>xmin) {
            xmin = min;
            xminExcl = minIncl;
        }
        if(!Double.isNaN(other.min) && ((other.minIncl&&other.min>xmin) || other.min>=xmin)) {
            xmin = other.min;
            xminExcl = other.minIncl;
        }

        if(Double.isInfinite(xmin)) {
            xmin = Double.NaN;
        }

        double xmax = Double.POSITIVE_INFINITY;
        boolean xmaxExcl = true;
        if(!Double.isNaN(max) && max>> 32));
        result = prime * result + (maxIncl ? 1231 : 1237);
        temp = Double.doubleToLongBits(min);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + (minIncl ? 1231 : 1237);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DoubleRange other = (DoubleRange) obj;
        if (Double.doubleToLongBits(max) != Double.doubleToLongBits(other.max))
            return false;
        if (Double.doubleToLongBits(min) != Double.doubleToLongBits(other.min))
            return false;
        if (minIncl != other.minIncl)
            return false;
        if (maxIncl != other.maxIncl)
            return false;
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy