Interface IntervalLike
- All Superinterfaces:
Comparable<IntervalLike>
- All Known Implementing Classes:
BookingSlot,Gap,Interval,IntervalSet,Slot,TimeInterval
Interval and IntervalSet classes.
Any instances of this type have common properties of a number interval or a
set of number intervals, such as:
- minimal bound, which can be retrieved by
min()method, - maximal bound, which can be retrieved by
max()method, - length, i.e. the length of an interval, or the sum of all interval
lengths in the case of an interval set. Can be retrieved by
length()method
isEmpty() method checks whether the instance
is empty.
An interval can represent a single point, i.e. its
min()==max(). Such interval is called a zero-length
interval, isZeroLength() method checks whether current
IntervalLike instance is of such type. Zero-length interval is not
the same as empty interval, however the length of an empty interval is also
zero by definition. Zero-length interval set contains one or more zero-length
intervals.
Another specific case is the universe interval, which contains all
possible number intervals. There is only one such intervals, namely [-∞, +∞].
isUniverse() method checks whether this IntervalLike
instance is a universe interval or interval set.
The natural ordering for IntervalLike instances is defined in
the following way:
- Author:
- Andrey Malykhanov
-
Method Summary
Modifier and TypeMethodDescriptiondefault IntervalSetConverts this instance ofIntervalLiketo an instance ofIntervalSetaccording to the following rules: if the specified instance ofIntervalLikeis already an instance ofIntervalSet, just returns the reference to this instance, without creating any new objects otherwise, if the specified instance ofIntervalLikeis an instance ofInterval, creates a new instance ofIntervalSetwith this single interval, and returns the reference to this newly created instancedefault intcompareTo(IntervalLike other) default IntervalSetReturns a new instance of anIntervalSetthat is a complement to this one.default booleancontains(double value) Checks whether thisIntervalLikeinstance contains (i.e., touches or covers) the specified value.booleancontains(IntervalLike other) Checks whether thisIntervalLikeinstance contains (i.e., fully covers) the specified other one.booleanequalsWithinEpsilon(IntervalLike other) Checks whether thisIntervalLikeinstance is equal to the specified other one with respect to the default epsilon.booleanequalsWithinEpsilon(IntervalLike other, EpsilonComparator comparator) Checks whether thisIntervalLikeinstance is equal to the specified other one with respect to the epsilon specified in theEpsilonComparatorinstance.exclusion(IntervalLike other) Returns a new instance ofIntervalSetthat represents an exclusion of the specified instance from this one.booleanChecks whether thisIntervalLikeinstance has a non-zero length intersection with the specified other one.intersection(IntervalLike other) Returns a new instance ofIntervalSetthat represents an intersection of this instance and the specified other one.booleanisEmpty()Checks whether this instance is an empty interval or interval set, i.e.default booleanisFinite()Checks whether currentIntervalLikeinstance is finite, i.e.booleanChecks whether this instance is a universe interval or interval set, i.e.default booleanChecks whether currentIntervalLikeinstance has zero length, i.e.leftSubset(double maxLength) Returns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsmin()boundary, with the cumulative length not exceeding the specified maximum length.default doublelength()Returns the length of thisIntervalLikeinstance, more specifically:max()-min()for anInterval, Sum of all intervals lengths for anIntervalSet, Zero for empty intervals and interval sets.doublemax()Returns the maximal (right) bound of interval or interval set, orDouble.NaNif this instance is empty.doublemin()Returns the minimal (left) bound of interval or interval set, orDouble.NaNif this instance is empty.rightSubset(double maxLength) Returns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsmax()boundary, with the cumulative length not exceeding the specified maximum length.intsize()Returns the number of intervals for interval set, or 1 for interval.Returns a Java code that creates anIntervalLikeequal to this one.union(IntervalLike other) Returns a new instance ofIntervalSetthat represents a union of this instance and the specified other one.
-
Method Details
-
min
double min()Returns the minimal (left) bound of interval or interval set, orDouble.NaNif this instance is empty.- Returns:
- left bound of interval or interval set, can be infinite or
Double.NaN
-
max
double max()Returns the maximal (right) bound of interval or interval set, orDouble.NaNif this instance is empty.- Returns:
- right bound of interval or interval set, can be infinite or
Double.NaN
-
isEmpty
boolean isEmpty()Checks whether this instance is an empty interval or interval set, i.e. represents a non-existent set, such as a result of intersection of disjoint intervals.Empty intervals and interval sets should not be confused with zero-length ones. Zero-length intervals are still intervals with same min and max boundaries.
For illustration of the difference between zero-length and empty instances, see the code below:
var morningShift = Interval.of(8, 12); var lunchtime = Interval.of(12, 13); var afternoonShift = Interval.of(13, 17); // The following expressions are true: // morningShift and lunchtime intervals touch at point of 12, // so their intersection has zero length but is not empty: assert(morningShift.intersection(lunchtime).equals(IntervalSet.of(12, 12))); assert(morningShift.intersection(lunchtime).isZeroLength()); assert(!morningShift.intersection(lunchtime).isEmpty()); // morningShift and afternoonShift intervals do not intersect, // so their intersection is an empty interval. By definition, // empty intervals have zero length: assert(morningShift.intersection(afternoonShift).equals(IntervalSet.EMPTY)); assert(morningShift.intersection(afternoonShift).isZeroLength()); assert(morningShift.intersection(afternoonShift).isEmpty());- Returns:
trueif this instance is an empty interval or interval set,falseotherwise- See Also:
-
isUniverse
boolean isUniverse()Checks whether this instance is a universe interval or interval set, i.e. such that includes all possible intervals. A universe interval is the one of [-∞, +∞], a universe interval set is the one containing only one universe interval.- Returns:
trueif this instance is a universe interval or interval set,falseotherwise
-
size
int size()Returns the number of intervals for interval set, or 1 for interval. If this instance is empty (isEmpty()== true), returns 0.- Returns:
- number of intervals for interval set, or 1 for interval, or 0
-
union
Returns a new instance ofIntervalSetthat represents a union of this instance and the specified other one.- Parameters:
other- specified otherIntervalLikeinstance- Returns:
- new instance of
IntervalSetthat represents a union of this instance and the specified other one.
-
exclusion
Returns a new instance ofIntervalSetthat represents an exclusion of the specified instance from this one.- Parameters:
other- specified otherIntervalLikeinstance that is being excluded from this one- Returns:
- new instance of
IntervalSetthat represents an exclusion of the specified instance from this one
-
intersection
Returns a new instance ofIntervalSetthat represents an intersection of this instance and the specified other one.- Parameters:
other- specified otherIntervalLikeinstance- Returns:
- new instance of
IntervalSetthat represents an intersection of this instance and the specified other one
-
hasNonZeroIntersectionWith
Checks whether thisIntervalLikeinstance has a non-zero length intersection with the specified other one.Calling this method instead of
intersection(IntervalLike)makes sense when it is only needed to check for the presence of non-zero intersection of twoIntervalLikeinstances, as this is done faster than computing the intersection itself.- Parameters:
other- specified otherIntervalLikeinstance- Returns:
trueif thisIntervalLikehas a non-zero length intersection with the other one,falseotherwise
-
contains
Checks whether thisIntervalLikeinstance contains (i.e., fully covers) the specified other one.- Parameters:
other- specified otherIntervalLikeinstance- Returns:
trueif thisIntervalLikeinstance contains (i.e., fully covers) the specified other one,falseotherwise
-
leftSubset
Returns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsmin()boundary, with the cumulative length not exceeding the specified maximum length. Ifmin()boundary of thisIntervalLikeisDouble.NEGATIVE_INFINITY, indicating an unbounded left boundary, anIllegalStateExceptionis thrownIf the specified length is >= the length of this
IntervalLike, a copy of this instance is returned.- Parameters:
maxLength- the maximum cumulative length of intervals to include in the subset. Must be non-negative.- Returns:
- An
IntervalSetrepresenting the leftmost subset of thisIntervalLikeinstance with length not exceeding the specified limit
-
rightSubset
Returns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsmax()boundary, with the cumulative length not exceeding the specified maximum length. Ifmax()boundary of thisIntervalLikeisDouble.POSITIVE_INFINITY, indicating an unbounded right boundary, anIllegalStateExceptionis thrownIf the specified length is >= the length of this
IntervalLike, a copy of this instance is returned.- Parameters:
maxLength- the maximum cumulative length of intervals to include in the subset. Must be non-negative.- Returns:
- An
IntervalSetrepresenting the rightmost subset of thisIntervalLikeinstance with length not exceeding the specified limit
-
contains
default boolean contains(double value) Checks whether thisIntervalLikeinstance contains (i.e., touches or covers) the specified value. The result of calling this method is equivalent to the result of the following code:IntervalLike x = ... x.contains(Interval.of(value))
- Parameters:
value- specified value- Returns:
trueif thisIntervalLikeinstance contains contains (i.e., touches or covers) the specified value,falseotherwise
-
toCodeString
String toCodeString()Returns a Java code that creates anIntervalLikeequal to this one. Can be used, for example, for unit testing and debugging.- Returns:
- string representing a Java code that creates an
IntervalLikeequal to this one
-
length
default double length()Returns the length of thisIntervalLikeinstance, more specifically:max()-min()for anInterval,- Sum of all intervals lengths for an
IntervalSet, - Zero for empty intervals and interval sets.
- Returns:
- length of this
IntervalLikeinstance, zero or a positive number, or aDobule.POSITIVE_INFINITY
-
isFinite
default boolean isFinite()Checks whether currentIntervalLikeinstance is finite, i.e. has a finite length.An interval such that at least one of its boundaries is infinite, is infinite. An interval set containing at least one infinite interval, is infinite
- Returns:
trueif thisIntervalLikeinstance is finite,falseotherwise
-
isZeroLength
default boolean isZeroLength()Checks whether currentIntervalLikeinstance has zero length, i.e. consists only of zero-length intervals. Empty intervals and interval sets also have zero lengths. See the comment ofisEmpty()method for details on the difference between empty and zero-length intervals and interval sets.- Returns:
trueif thisIntervalLikeinstance has zero length,falseotherwise- See Also:
-
complement
Returns a new instance of anIntervalSetthat is a complement to this one. The result of calling this method is equivalent to the result of the following code:IntervalSet.UNIVERSE.exclusion(this)
- Returns:
- new instance of an
IntervalSetthat is a complement to this one
-
asIntervalSet
Converts this instance ofIntervalLiketo an instance ofIntervalSetaccording to the following rules:- if the specified instance of
IntervalLikeis already an instance ofIntervalSet, just returns the reference to this instance, without creating any new objects - otherwise, if the specified instance of
IntervalLikeis an instance ofInterval, creates a new instance ofIntervalSetwith this single interval, and returns the reference to this newly created instance
- Returns:
- either existing or new instance of of
IntervalSet
- if the specified instance of
-
equalsWithinEpsilon
Checks whether thisIntervalLikeinstance is equal to the specified other one with respect to the default epsilon. The default epsilon can be obtained byCompare.getEpsilon()method.- Parameters:
other- specified otherIntervalLikeinstance- Returns:
trueif thisIntervalLikeinstance is equal to the specified other one with respect to the default epsilon,falseotherwise
-
equalsWithinEpsilon
Checks whether thisIntervalLikeinstance is equal to the specified other one with respect to the epsilon specified in theEpsilonComparatorinstance.- Parameters:
other- specified otherIntervalLikeinstancecomparator- anEpsilonComparatorinstance specifying the tolerance of comparison- Returns:
trueif thisIntervalLikeinstance is equal to the specified other one with respect to the specified epsilon,falseotherwise
-
compareTo
- Specified by:
compareToin interfaceComparable<IntervalLike>
-