Class Range

java.lang.Object
net.algart.math.Range

public final class Range extends Object

Numeric inclusive real range: a set of double numbers min()<=x<=max(). An advanced analog of org.apache.commons.lang.math.DoubleRange.

The minimum number (min()) is never greater than the maximum number (max()), and both numbers are never Double.NaN.

This class is immutable and thread-safe: there are no ways to modify settings of the created instance.

Author:
Daniel Alievsky
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    contains(double value)
    Returns true if and only if min()<=value<=max()
    boolean
    contains(Range range)
    Returns true if and only if min()<=range.min() and range.max()<=max().
    double
    cut(double value)
    Returns value<min()?min():value>max()?max():value.
    boolean
    Indicates whether some other range is equal to this instance.
    expand(double value)
    Returns an instance of this class describing the range StrictMath.min(this.min(),value) <= x <= StrictMath.max(this.max(),value), excepting the case when the passed value is NaN — in the last situation, returns this instance without changes.
    int
    Returns the hash code of this range.
    boolean
    Returns true if and only if min()<=range.max() and range.min()<=max().
    double
    max()
    Returns the maximum number in the range, inclusive.
    double
    min()
    Returns the minimum number in the range, inclusive.
    double
    Returns max()-min().
    Equivalent to IRange.valueOf(thisInstance), with the only difference that IllegalStateException is thrown instead of IllegalArgumentException for unallowed range.
    Equivalent to IRange.roundOf(thisInstance), with the only difference that IllegalStateException is thrown instead of IllegalArgumentException for unallowed range.
    Returns a brief string description of this object.
    static Range
    valueOf(double min, double max)
    Returns an instance of this class describing the range min<=x<=max.
    static Range
    valueOf(IRange iRange)
    Returns an instance of this class describing the same range as the given integer range.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • valueOf

      public static Range valueOf(double min, double max)
      Returns an instance of this class describing the range min<=x<=max. The min value must not be greater than max, and both arguments must not be Double.NaN.
      Parameters:
      min - the minimum number in the range, inclusive.
      max - the maximum number in the range, inclusive.
      Returns:
      the new range.
      Throws:
      IllegalArgumentException - if min > max or one of the arguments is Double.NaN.
    • valueOf

      public static Range valueOf(IRange iRange)
      Returns an instance of this class describing the same range as the given integer range. The long boundaries of the passed integer range are converted to double boundaries min() and max() of the returned range by standard Java typecast (double)longValue.
      Parameters:
      iRange - the integer range.
      Returns:
      the equivalent real range.
      Throws:
      NullPointerException - if the passed integer range is null.
    • min

      public double min()
      Returns the minimum number in the range, inclusive.
      Returns:
      the minimum number in the range.
    • max

      public double max()
      Returns the maximum number in the range, inclusive.
      Returns:
      the maximum number in the range.
    • size

      public double size()
      Returns max()-min().
      Returns:
      max()-min().
    • cut

      public double cut(double value)
      Returns value<min()?min():value>max()?max():value. In other words, returns the passed number if it is in this range or the nearest range bound in other cases.
      Parameters:
      value - some number.
      Returns:
      the passed number if it is in this range or the nearest range bound in other cases.
    • contains

      public boolean contains(double value)
      Returns true if and only if min()<=value<=max()
      Parameters:
      value - the checked value.
      Returns:
      true if the value is in this range.
    • contains

      public boolean contains(Range range)
      Returns true if and only if min()<=range.min() and range.max()<=max().
      Parameters:
      range - the checked range.
      Returns:
      true if the checked range is a subset of this range.
    • intersects

      public boolean intersects(Range range)
      Returns true if and only if min()<=range.max() and range.min()<=max().
      Parameters:
      range - the checked range.
      Returns:
      true if the checked range overlaps with this range.
    • expand

      public Range expand(double value)
      Returns an instance of this class describing the range StrictMath.min(this.min(),value) <= x <= StrictMath.max(this.max(),value), excepting the case when the passed value is NaN — in the last situation, returns this instance without changes. In other words, expands the current range to include the given value.
      Parameters:
      value - some value that should belong to the new range.
      Returns:
      the expanded range (or this range if Double.isNaN(value)).
    • toIntegerRange

      public IRange toIntegerRange()
      Equivalent to IRange.valueOf(thisInstance), with the only difference that IllegalStateException is thrown instead of IllegalArgumentException for unallowed range.
      Returns:
      the integer range with same (cast) bounds.
      Throws:
      IllegalStateException - in the same situations when IRange.valueOf(Range) method throws IllegalArgumentException.
    • toRoundedRange

      public IRange toRoundedRange()
      Equivalent to IRange.roundOf(thisInstance), with the only difference that IllegalStateException is thrown instead of IllegalArgumentException for unallowed range.
      Returns:
      the integer range with same (rounded) bounds.
      Throws:
      IllegalStateException - in the same situations when IRange.roundOf(Range) method throws IllegalArgumentException.
    • toString

      public String toString()
      Returns a brief string description of this object.

      The result of this method may depend on implementation and usually contains the minimum and maximum numbers in this range.

      Overrides:
      toString in class Object
      Returns:
      a brief string description of this object.
    • hashCode

      public int hashCode()
      Returns the hash code of this range.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of this range.
    • equals

      public boolean equals(Object obj)
      Indicates whether some other range is equal to this instance. Returns true if and only if obj instanceof Range, Double.doubleToLongBits(((Range)obj).min())==Double.doubleToLongBits(thisInstance.min()) and Double.doubleToLongBits(((Range)obj).max())==Double.doubleToLongBits(thisInstance.max()).
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to be compared for equality with this instance.
      Returns:
      true if the specified object is a range equal to this one.