Package net.algart.math
Class Range
java.lang.Object
net.algart.math.Range
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 TypeMethodDescriptionboolean
contains
(double value) boolean
double
cut
(double value) boolean
Indicates whether some other range is equal to this instance.expand
(double value) int
hashCode()
Returns the hash code of this range.boolean
intersects
(Range range) double
max()
Returns the maximum number in the range, inclusive.double
min()
Returns the minimum number in the range, inclusive.double
size()
Equivalent toIRange.valueOf
(thisInstance), with the only difference that IllegalStateException is thrown instead of IllegalArgumentException for unallowed range.Equivalent toIRange.roundOf
(thisInstance), with the only difference that IllegalStateException is thrown instead of IllegalArgumentException for unallowed range.toString()
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
Returns an instance of this class describing the same range as the given integer range.
-
Method Details
-
valueOf
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
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 boundariesmin()
andmax()
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() -
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) - Parameters:
value
- the checked value.- Returns:
- true if the value is in this range.
-
contains
- Parameters:
range
- the checked range.- Returns:
- true if the checked range is a subset of this range.
-
intersects
- Parameters:
range
- the checked range.- Returns:
- true if the checked range overlaps with this range.
-
expand
Returns an instance of this class describing the rangeStrictMath.min(this. , 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.min()
,value) <= x <= StrictMath.max(this.max()
,value)- Parameters:
value
- some value that should belong to the new range.- Returns:
- the expanded range (or this range if Double.isNaN(value)).
-
toIntegerRange
Equivalent toIRange.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 whenIRange.valueOf(Range)
method throws IllegalArgumentException.
-
toRoundedRange
Equivalent toIRange.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 whenIRange.roundOf(Range)
method throws IllegalArgumentException.
-
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.
-
hashCode
public int hashCode()Returns the hash code of this range. -
equals
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()).
-