Represents an interval between values.
The Range
type behaves the same as the EdgeDB range
type.
Ranges can have a lower and upper boundary, which can be inclusive or
exclusive, or omitted completely (null
). By default, the lower boundary
is inclusive, and the upper boundary exclusive.
Depending on the type of the range, T
, the range is either discrete or
continuous.
Discrete range types: Range<int>
Continuous range types: Range<double>
, Range<DateTime>
Discrete ranges are normalised upon creation, such that the lower boundary
becomes inclusive, and the upper boundary becomes exclusive.
If a range can contain no values, it is considered ‘empty’. Empty ranges
are all considered equal to each other. An empty range can be created
either by the Range.empty()
constructor, creating a range where the lower
and upper boundaries are equal, and at least one boundary is exclusive, or
as the result of some operation on a range.
Range<T>()
Range<T>(
T? lower,
T? upper,
{bool? incLower,
bool? incUpper}
)
Creates a new Range.
If not given, incLower
and incUpper
default to true
and false
respectively.
.incLower
bool get incLower
Whether the lower boundary is inclusive. Is always false
for unspecified
boundaries and empty ranges.
.incUpper
bool get incUpper
Whether the upper boundary is inclusive. Is always false
for unspecified
boundaries and empty ranges.
.compareTo()
int compareTo(
Range<T> other
)
Compares this object to another object.
Returns a value like a Comparator when comparing this
to other
.
That is, it returns a negative integer if this
is ordered before other
,
a positive integer if this
is ordered after other
,
and zero if this
and other
are ordered together.
The other
argument must be a value that is comparable to this object.
.containsRange()
bool containsRange(
Range<T> range
)
Checks whether range
is entirely within this range.
.overlaps()
bool overlaps(
Range<T> other
)
Checks whether other
range overlaps this range.
.toString()
String toString()
String representation of the range.
Inclusive boundaries are denoted by []
brackets, and exclusive
boundaries by ()
. If the range is empty, returns the string 'empty'
.
.unpack()
Iterable<T> unpack(
{Object? step}
)
If the range is discrete and no step
is provided, returns an Iterable
of all values in the range. Otherwise returns an Iterable
of each
value starting at the lower bound, increasing by step
up to the
upper bound.
An error is thrown if the range is unbounded (ie. either lower
or
upper
are null
), or the step
parameter is not given for
non-discrete ranges.
+
Range<T> operator +(
Range<T> other
)
Returns the union of two ranges.
Throws an error if the result is not a single continuous range.
-
Range<T> operator -(
Range<T> other
)
Subtracts one range from another.
Throws an error if the result is not a single continuous range.
<
bool operator <(
Range<T> other
)
Returns whether this range is before the other
range.
A range is considered to be ordered before another range if its lower bound is lower than the other. If the lower bounds are equal, the upper bounds are checked. An empty range is considered lower than a non-empty range, and unspecified lower/upper bounds are considered lower/greater than specified lower/upper bounds respectively.
<=
bool operator <=(
Range<T> other
)
Returns whether this range is before or equal to the other
range.
A range is considered to be ordered before another range if its lower bound is lower than the other. If the lower bounds are equal, the upper bounds are checked. An empty range is considered lower than a non-empty range, and unspecified lower/upper bounds are considered lower/greater than specified lower/upper bounds respectively.
>
bool operator >(
Range<T> other
)
Returns whether this range is after the other
range.
A range is considered to be ordered after another range if its lower bound is greater than the other. If the lower bounds are equal, the upper bounds are checked. An empty range is considered lower than a non-empty range, and unspecified lower/upper bounds are considered lower/greater than specified lower/upper bounds respectively.
>=
bool operator >=(
Range<T> other
)
Returns whether this range is after or equal to the other
range.
A range is considered to be ordered after another range if its lower bound is greater than the other. If the lower bounds are equal, the upper bounds are checked. An empty range is considered lower than a non-empty range, and unspecified lower/upper bounds are considered lower/greater than specified lower/upper bounds respectively.
Represents an amount of memory in bytes.
Uses the base-2 KiB
notation (1024 bytes), instead of the more
ambiguous ‘kB’, which can mean 1000 or 1024 bytes.
.toString()
String toString()
A string representation of this object.
Some classes have a default textual representation,
often paired with a static parse
function (like int.parse).
These classes will provide the textual representation as
their string representation.
Other classes have no meaningful textual representation
that a program will care about.
Such classes will typically override toString
to provide
useful information when inspecting the object,
mainly for debugging or logging.