E
- the type of range elements as a subclass of Number
.public class NumberRange<E extends Number & Comparable<? super E>> extends Range<E>
NumberRange
has no unit of measurement. For a range of physical measurements
with unit of measure, see MeasurementRange
.
NumberRange
has some capability to convert different number types before to
perform operations. In order to provide both this flexibility and the safety of generic
types, most operations in this class are defined in two versions:
Range
parent class
(contains
, intersect
,
intersects
, union
and
subtract
) requires argument or range elements
of type <E>
. No type conversion is performed.Any
suffix
(containsAny
, intersectAny
,
intersectsAny
, unionAny
and
subtractAny
) are more lenient on the argument or range element
type <E>
. Widening conversions are performed as needed.<?>
).
Other methods defined in this class:
create(…)
static methods for every numeric primitive types.castTo(Class)
for casting the range values to an other type.NumberRange
is the SIS class closest to the
mathematical definition of interval.
It is closely related, while not identical, to the ISO 19123 (Coverage geometry and functions)
definition of "ranges". At the difference of the parent Range
class, which can be used only with
discrete coverages, the NumberRange
class can
also be used with continuous coverages.
MeasurementRange
subclasses are immutable, and thus inherently thread-safe.
Other subclasses may or may not be immutable, at implementation choice. But implementors are encouraged
to make sure that all subclasses remain immutable for more predictable behavior.
All create
static methods may return a shared instance. Those methods are preferred
to the constructors when the range is expected to have a long lifetime, typically as instance
given to parameter descriptor.
Other methods do not check for shared instances, since the created object is often temporary.
RangeFormat
,
RangeSet
,
Wikipedia: Interval,
Serialized FormDefined in the sis-utility
module
Constructor and Description |
---|
NumberRange(Class<E> type,
E minValue,
boolean isMinIncluded,
E maxValue,
boolean isMaxIncluded)
Constructs a range of
Number objects. |
NumberRange(Class<E> type,
ValueRange range)
Constructs a range of the given type with values from the given annotation.
|
NumberRange(Range<E> range)
Constructs a range with the same type and the same values than the specified range.
|
Modifier and Type | Method and Description |
---|---|
static <N extends Number & Comparable<? super N>> |
castOrCopy(Range<N> range)
Returns the specified
Range as a NumberRange object. |
<N extends Number & Comparable<? super N>> |
castTo(Class<N> type)
Casts this range to the specified type.
|
boolean |
containsAny(Number value)
Returns
true if this range contains the given value. |
boolean |
containsAny(NumberRange<?> range)
Returns
true if the supplied range is fully contained within this range. |
static NumberRange<Byte> |
create(byte minValue,
boolean isMinIncluded,
byte maxValue,
boolean isMaxIncluded)
Constructs a range of
byte values. |
static NumberRange<Double> |
create(double minValue,
boolean isMinIncluded,
double maxValue,
boolean isMaxIncluded)
Constructs a range of
double values. |
static NumberRange<Float> |
create(float minValue,
boolean isMinIncluded,
float maxValue,
boolean isMaxIncluded)
Constructs a range of
float values. |
static NumberRange<Integer> |
create(int minValue,
boolean isMinIncluded,
int maxValue,
boolean isMaxIncluded)
Constructs a range of
int values. |
static NumberRange<Long> |
create(long minValue,
boolean isMinIncluded,
long maxValue,
boolean isMaxIncluded)
Constructs a range of
long values. |
static NumberRange<Short> |
create(short minValue,
boolean isMinIncluded,
short maxValue,
boolean isMaxIncluded)
Constructs a range of
short values. |
static NumberRange<?> |
createBestFit(Number minValue,
boolean isMinIncluded,
Number maxValue,
boolean isMaxIncluded)
Constructs a range using the smallest type of
Number that can hold the given values. |
static NumberRange<Integer> |
createLeftBounded(int minValue,
boolean isMinIncluded)
Constructs a range of
int values without upper bound. |
double |
getMaxDouble()
Returns the maximum value as a
double . |
double |
getMaxDouble(boolean inclusive)
Returns the maximum value with the specified inclusive or exclusive state.
|
double |
getMinDouble()
Returns the minimum value as a
double . |
double |
getMinDouble(boolean inclusive)
Returns the minimum value with the specified inclusive or exclusive state.
|
NumberRange<?> |
intersectAny(NumberRange<?> range)
Returns the union of this range with the given range.
|
boolean |
intersectsAny(NumberRange<?> range)
Returns
true if the supplied range is fully contained within this range. |
NumberRange<?>[] |
subtractAny(NumberRange<?> range)
Returns the range of values that are in this range but not in the given range.
|
NumberRange<?> |
unionAny(NumberRange<?> range)
Returns the union of this range with the given range.
|
contains, contains, equals, formatTo, getElementType, getMaxValue, getMinValue, hashCode, intersect, intersects, isEmpty, isMaxIncluded, isMinIncluded, subtract, toString, union
public NumberRange(Range<E> range)
range
- the range to copy. The elements must be Number
instances.public NumberRange(Class<E> type, ValueRange range) throws IllegalArgumentException
public NumberRange(Class<E> type, E minValue, boolean isMinIncluded, E maxValue, boolean isMaxIncluded)
Number
objects.type
- the element type, usually one of Byte
, Short
,
Integer
, Long
, Float
or Double
.minValue
- the minimal value, or null
if none.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value, or null
if none.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.public static NumberRange<Byte> create(byte minValue, boolean isMinIncluded, byte maxValue, boolean isMaxIncluded)
byte
values.
This method may return a shared instance, at implementation choice.minValue
- the minimal value.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.public static NumberRange<Short> create(short minValue, boolean isMinIncluded, short maxValue, boolean isMaxIncluded)
short
values.
This method may return a shared instance, at implementation choice.minValue
- the minimal value.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.public static NumberRange<Integer> create(int minValue, boolean isMinIncluded, int maxValue, boolean isMaxIncluded)
int
values.
This method may return a shared instance, at implementation choice.minValue
- the minimal value.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.createLeftBounded(int, boolean)
public static NumberRange<Long> create(long minValue, boolean isMinIncluded, long maxValue, boolean isMaxIncluded)
long
values.
This method may return a shared instance, at implementation choice.minValue
- the minimal value.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.public static NumberRange<Float> create(float minValue, boolean isMinIncluded, float maxValue, boolean isMaxIncluded)
float
values.
The values can not be Float.NaN
.
This method may return a shared instance, at implementation choice.minValue
- the minimal value, or Float.NEGATIVE_INFINITY
if none.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value, or Float.POSITIVE_INFINITY
if none.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.public static NumberRange<Double> create(double minValue, boolean isMinIncluded, double maxValue, boolean isMaxIncluded)
double
values.
The values can not be Double.NaN
.
This method may return a shared instance, at implementation choice.minValue
- the minimal value, or Double.NEGATIVE_INFINITY
if none.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value, or Double.POSITIVE_INFINITY
if none.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.public static NumberRange<Integer> createLeftBounded(int minValue, boolean isMinIncluded)
int
values without upper bound.
This method may return a shared instance, at implementation choice.
create(…)
methods with a POSITIVE_INFINITY
constant.
We do not provide variants for other integer types because this method is typically invoked for
defining the cardinality of an attribute.minValue
- the minimal value.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.minValue
to positive infinity.create(int, boolean, int, boolean)
public static NumberRange<?> createBestFit(Number minValue, boolean isMinIncluded, Number maxValue, boolean isMaxIncluded)
Number
that can hold the given values.
The given numbers don't need to be of the same type since they will
be casted as needed.
More specifically this method returns:
NumberRange<Byte>
if the given values are integers between
-128 and 127 inclusive.NumberRange<Short>
if the given values are integers between
-32768 and 32767 inclusive.NumberRange<Integer>
if the given values are integers between
-2147483648 and 2147483647 inclusive.NumberRange<Long>
if the given values are integers in the range of long
values.NumberRange<Float>
if the given values can be casted to float
values without data lost.NumberRange<Double>
If none of the above types is suitable.minValue
- the minimal value, or null
if none.isMinIncluded
- true
if the minimal value is inclusive, or false
if exclusive.maxValue
- the maximal value, or null
if none.isMaxIncluded
- true
if the maximal value is inclusive, or false
if exclusive.null
if both minValue
and maxValue
are null
.public static <N extends Number & Comparable<? super N>> NumberRange<N> castOrCopy(Range<N> range)
Range
as a NumberRange
object.
If the specified range is already an instance of NumberRange
, then it is returned unchanged.
Otherwise a new number range is created using the copy constructor.N
- the type of elements in the given range.range
- the range to cast or copy.range
as a NumberRange
object.public <N extends Number & Comparable<? super N>> NumberRange<N> castTo(Class<N> type) throws IllegalArgumentException
N
- the class to cast to.type
- the class to cast to. Must be one of Byte
, Short
,
Integer
, Long
, Float
or Double
.this
if this range already uses the specified type.IllegalArgumentException
- if the given type is not one of the primitive wrappers for numeric types.public double getMinDouble()
double
.
If this range is unbounded, then Double.NEGATIVE_INFINITY
is returned.public double getMinDouble(boolean inclusive)
Double.NEGATIVE_INFINITY
is returned.inclusive
- true
for the minimum value inclusive, or
false
for the minimum value exclusive.public double getMaxDouble()
double
.
If this range is unbounded, then Double.POSITIVE_INFINITY
is returned.public double getMaxDouble(boolean inclusive)
Double.POSITIVE_INFINITY
is returned.inclusive
- true
for the maximum value inclusive, or
false
for the maximum value exclusive.public boolean containsAny(Number value) throws IllegalArgumentException
true
if this range contains the given value.
This method converts this
or the given argument to the widest numeric type,
then performs the same work than Range.contains(Comparable)
.value
- the value to check for inclusion in this range.true
if the given value is included in this range.IllegalArgumentException
- if the given range can not be converted to a valid type
through widening conversion.public boolean containsAny(NumberRange<?> range) throws IllegalArgumentException
true
if the supplied range is fully contained within this range.
This method converts this
or the given argument to the widest numeric type,
then delegates to Range.contains(Range)
.range
- the range to check for inclusion in this range.true
if the given range is included in this range.IllegalArgumentException
- if the given range can not be converted to a valid type
through widening conversion, or if the units of measurement are not convertible.public boolean intersectsAny(NumberRange<?> range) throws IllegalArgumentException
true
if the supplied range is fully contained within this range.
This method converts this
or the given argument to the widest numeric type,
then delegates to Range.intersects(Range)
.range
- the range to check for inclusion in this range.true
if the given range is included in this range.IllegalArgumentException
- if the given range can not be converted to a valid type
through widening conversion, or if the units of measurement are not convertible.public NumberRange<?> intersectAny(NumberRange<?> range) throws IllegalArgumentException
this
or the given argument to the widest numeric type,
then delegates to Range.intersect(Range)
.range
- the range to add to this range.IllegalArgumentException
- if the given range can not be converted to a valid type
through widening conversion, or if the units of measurement are not convertible.public NumberRange<?> unionAny(NumberRange<?> range) throws IllegalArgumentException
this
or the given argument to the widest numeric type,
then delegates to Range.union(Range)
.range
- the range to add to this range.IllegalArgumentException
- if the given range can not be converted to a valid type
through widening conversion, or if the units of measurement are not convertible.public NumberRange<?>[] subtractAny(NumberRange<?> range) throws IllegalArgumentException
this
or the given argument to the widest numeric type,
then delegates to Range.subtract(Range)
.range
- the range to subtract.IllegalArgumentException
- if the given range can not be converted to a valid type
through widening conversion, or if the units of measurement are not convertible.Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.