    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
    006 *
    007 * http://www.object-refinery.com/orsoncharts/index.html
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * Orson Charts home page:
    028 *
    029 * http://www.object-refinery.com/orsoncharts/index.html
    030 *
    031 */
    032
    033package com.orsoncharts;
    034
    035import java.io.Serializable;
    036
    037import com.orsoncharts.util.ArgChecks;
    038
    039/**
    040 * Represents a range of data values (instances are immutable).
    041 * <br><br>
    042 * NOTE: This class is serializable, but the serialization format is subject
    043 * to change in future releases and should not be relied upon for persisting
    044 * instances of this class.
    045 */
    046@SuppressWarnings("serial")
    047public class Range implements Serializable {
    048
    049    /** The lower bound of the range. */
    050    private double min;
    051
    052    /** The upper bound of the range. */
    053    private double max;
    054
    055    /**
    056     * Creates a new range instance.
    057     *
    058     * @param min  the lower bound of the range.
    059     * @param max  the upper bound of the range.
    060     */
    061    public Range(double min, double max) {
    062        if (min > max) {
    063            throw new IllegalArgumentException("Requires min <= max.");
    064        }
    065        this.min = min;
    066        this.max = max;
    067    }
    068
    069    /**
    070     * Returns the lower bound of the range.
    071     *
    072     * @return The lower bound of the range.
    073     */
    074    public double getMin() {
    075        return this.min;
    076    }
    077
    078    /**
    079     * Returns the upper bound of the range.
    080     *
    081     * @return The upper bound of the range.
    082     */
    083    public double getMax() {
    084        return this.max;
    085    }
    086
    087    /**
    088     * Returns the length of the range.
    089     *
    090     * @return The length of the range.
    091     */
    092    public double getLength() {
    093        return this.max - this.min;
    094    }
    095
    096    /**
    097     * Returns {@code true} if the range includes the specified value,
    098     * and {@code false} otherwise.
    099     *
    100     * @param value  the value.
    101     *
    102     * @return A boolean.
    103     */
    104    public boolean contains(double value) {
    105        return value >= this.min && value <= this.max;
    106    }
    107
    108    /**
    109     * Returns either (a) the supplied value, if it falls within the range, or
    110     * (b) the range minimum or maximum value, whichever is closest to value.
    111     *
    112     * @param value  the value.
    113     *
    114     * @return The pegged value.
    115     */
    116    public double peggedValue(double value) {
    117        return Math.max(this.min, Math.min(this.max, value));
    118    }
    119
    120    /**
    121     * Returns {@code true} if the range intersects the interval defined
    122     * by the two bounds (the order of the bounds is not important), and
    123     * {@code false} otherwise.
    124     *
    125     * @param bound1  the first boundary value.
    126     * @param bound2  the second boundary value.
    127     *
    128     * @return A boolean.
    129     */
    130    public boolean intersects(double bound1, double bound2) {
    131        double lowerBound = Math.min(bound1, bound2);
    132        double upperBound = Math.max(bound1, bound2);
    133        if (upperBound < this.min) {
    134            return false;
    135        }
    136        if (lowerBound > this.max) {
    137            return false;
    138        }
    139        return true;
    140    }
    141
    142    /**
    143     * Returns {@code true} if this range intersects with
    144     * the specified range, and {@code false} otherwise.
    145     *
    146     * @param range  the range ({@code null} not permitted).
    147     *
    148     * @return A boolean.
    149     *
    150     * @since 1.2
    151     */
    152    public boolean intersects(Range range) {
    153        ArgChecks.nullNotPermitted(range, "range");
    154        return intersects(range.getMin(), range.getMax());
    155    }
    156
    157    /**
    158     * Returns the value as a percentage along the range.
    159     *
    160     * @param value  the value.
    161     *
    162     * @return The percentage.
    163     */
    164    public double percent(double value) {
    165        return (value - this.min) / getLength();
    166    }
    167
    168    /**
    169     * Returns the value as a percentage along the range, with optionally the
    170     * result inverted (that is, p becomes 1.0 - p).
    171     *
    172     * @param value  the value.
    173     * @param inverted  invert the result?
    174     *
    175     * @return The percentage.
    176     *
    177     * @since 1.5
    178     */
    179    public double percent(double value, boolean inverted) {
    180        double p = percent(value);
    181        if (inverted) {
    182            p = 1.0 - p;
    183        }
    184        return p;
    185    }
    186
    187    /**
    188     * Returns the value corresponding to the specified percentage.
    189     *
    190     * @param percent  the percentage along the range.
    191     *
    192     * @return The value.
    193     *
    194     * @since 1.1
    195     */
    196    public double value(double percent) {
    197        return this.min + percent * (this.max - this.min);
    198    }
    199
    200    /**
    201     * Tests this instance for equality with an arbitrary object.
    202     *
    203     * @param obj  the object ({@code null} permitted).
    204     *
    205     * @return A boolean.
    206     */
    207    @Override
    208    public boolean equals(Object obj) {
    209        if (obj == this) {
    210            return true;
    211        }
    212        if (!(obj instanceof Range)) {
    213            return false;
    214        }
    215        Range that = (Range) obj;
    216        if (this.min != that.min) {
    217            return false;
    218        }
    219        if (this.max != that.max) {
    220            return false;
    221        }
    222        return true;
    223    }
    224
    225    /**
    226     * Returns a hash code for this instance.
    227     *
    228     * @return A hash code.
    229     */
    230    @Override
    231    public int hashCode() {
    232        int hash = 7;
    233        hash = 43 * hash + (int) (Double.doubleToLongBits(this.min)
    234                ^ (Double.doubleToLongBits(this.min) >>> 32));
    235        hash = 43 * hash + (int) (Double.doubleToLongBits(this.max)
    236                ^ (Double.doubleToLongBits(this.max) >>> 32));
    237        return hash;
    238    }
    239
    240    /**
    241     * Returns a string representation of this instance, primarily for
    242     * debugging purposes.
    243     *
    244     * @return A string.
    245     */
    246    @Override
    247    public String toString() {
    248        return "Range[" + this.min + ", " + this.max + "]";
    249    }
    250}