A Range represents the list of discrete items between some starting (or from
)
value and working up towards some ending (or to
) value.
For a reverse range, the list is obtained by starting at the to
value and
working down towards the from
value.
The concept of working up and working down is dependent on the range implementation.
In the general case, working up involves successive calls to the first item's next()
method while working down involves calling the previous()
method. Optimized
numerical ranges may apply numeric addition or subtraction of some numerical step size.
Particular range implementations may also support the notion of inclusivity
and exclusivity with respect to the ending value in the range.
E.g. 1..3 == [1, 2, 3]
; but 1..<3 == [1, 2]
.
In general, the second boundary may not be contained in the range,
and a..b
may produce a different set of elements than (b..a).reversed()
.
E.g. 1..2.5 == [1, 2]
; but 2.5..1 == [2.5, 1.5]
.
Implementations can be memory efficient by storing just the from
and to
boundary
values rather than eagerly creating all discrete items in the conceptual list. The actual discrete items
can be lazily calculated on an as needed basis (e.g. when calling methods from the java.util.List
interface or the additional step
methods in the Range
interface).
In addition to the methods related to a Range's "discrete items" abstraction, there is a method,
containsWithinBounds
which, for numerical ranges, allows checking within the continuous
interval between the Range's boundary values.