Writing your own interval type
You can add custom interval types of either of the two basic types of intervals:
- Iterable
intervals, which are intervals that you can iterate across, such
as in
forloop declarations - Non-iterable intervals
For typical code, iterable intervals are more useful because they can simplify common coding patterns in loops.
In some circumstances, you need to create a non-iterable interval. For example, suppose you want to encapsulate an inclusive range of real numbers between two endpoints. Such a set includes a theoretically infinite set of numbers between the values 1 and 1.001. Iterating across the set is meaningless.
The basic properties of an interval are as follows:
- The type of items in the interval
must implement the Java interface
java.lang.Comparable. - The interval has left and right endpoints (the starting and ending values of the interval)
- Each endpoint can be closed (included) or open (excluded)
The main difference for iterable intervals is that
they also implement the java.lang.Iterable
interface.
