Class IntervalSet
- All Implemented Interfaces:
IntervalLike,Comparable<IntervalLike>,Iterable<Interval>
Interval class.
The intervals are always sorted by their natural order described in
IntervalLike interface documentation.
Note that any IntervalSet instance is always normalized, i.e. it
always contains a set of pairwise disjoint intervals even at creation time
the intervals used to create an IntervalSet instance were not
disjoint.
The simple example of using an IntervalSet to represent a working day
consisting of two 4-hour shifts:
var morningShift = Interval.of(8, 12);
var afternoonShift = Interval.of(13, 17);
IntervalSet workingDay = morningShift.union(afternoonShift);
// Output: {[8.0, 12.0], [13.0, 17.0]}
System.out.println("Working day: " + workingDay);
- Author:
- Andrey Malykhanov
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final IntervalSetEmpty interval set, i.e.static final IntervalSetUniverse interval set, i.e. -
Constructor Summary
ConstructorsConstructorDescriptionIntervalSet(Interval interval) Creates a new instance ofIntervalSetcontaining a single specified interval.IntervalSet(List<? extends Interval> intervals) Creates a new instance ofIntervalSetcontaining intervals in the specified list. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(IntervalLike other) Checks whether thisIntervalLikeinstance contains (i.e., fully covers) the specified other one.booleanbooleanequalsWithinEpsilon(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.Returns anOptionalcontaining the first (leftmost) interval of this set, orOptional.empty()if this set is empty.inthashCode()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.Returns an unmodifiable list of intervals contained in this interval set.booleanisEmpty()Checks whether this instance is an empty interval or interval set, i.e.booleanChecks whether this instance is a universe interval or interval set, i.e.booleanChecks whether currentIntervalLikeinstance has zero length, i.e.iterator()Returns anOptionalcontaining the last (rightmost) interval of this set, orOptional.empty()if this set is empty.leftSubset(double maxLength) Returns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsIntervalLike.min()boundary, with the cumulative length not exceeding the specified maximum length.doublelength()Returns the length of thisIntervalLikeinstance, more specifically:IntervalLike.max()-IntervalLike.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.static IntervalSetof(double min, double max) Creates and returns a new instance ofIntervalSetcontaining a single interval with the specified min and max bounds.static IntervalSetCreates and returns a new instance ofIntervalSetthat contains the specified intervals.static IntervalSetof(IntervalLike intervalLike) Converts the specified instance ofIntervalLiketo an instance ofIntervalSetaccording to the following rules: if the specified instance ofIntervalLikeis an instance ofIntervalSet, just returns the reference to this instance, without creating any new instances 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 instancestatic IntervalSetstatic IntervalSetrightSubset(double maxLength) Returns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsIntervalLike.max()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.toString()union(IntervalLike other) Returns a new instance ofIntervalSetthat represents a union of this instance and the specified other one.withIntervalsMatching(Predicate<Interval> predicate) Returns a newIntervalSetinstance containing only those intervals that match the specified predicate condition.withoutIntervalsMatching(Predicate<Interval> predicate) Returns a newIntervalSetinstance excluding the intervals that match the specified predicate condition.Returns a new instance ofIntervalSetthat is computed from this instance by removing all zero-length intervals.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.amalgamasimulation.core.intervals.IntervalLike
asIntervalSet, compareTo, complement, contains, isFiniteMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
EMPTY
Empty interval set, i.e. one containing no intervals and representing, for example, a result of intersection of disjoint intervals -
UNIVERSE
Universe interval set, i.e. interval set that contains exactly oneInterval.UNIVERSEinterval.
-
-
Constructor Details
-
IntervalSet
Creates a new instance ofIntervalSetcontaining a single specified interval.- Parameters:
interval- single specified interval
-
IntervalSet
Creates a new instance ofIntervalSetcontaining intervals in the specified list. Performs normalization if necessary.- Parameters:
intervals- specified list of intervals
-
-
Method Details
-
of
Creates and returns a new instance ofIntervalSetthat contains the specified intervals. Performs normalization if necessary.- Parameters:
intervals- specified intervals of the newly created interval set- Returns:
- a new instance of
IntervalSet
-
of
Creates and returns a new instance ofIntervalSetcontaining a single interval with the specified min and max bounds.- Parameters:
min- minimal bound of the only interval of the newly created interval setmax- maximal bound of the only interval of the newly created interval set- Returns:
- a new instance of
IntervalSet
-
of
Converts the specified instance ofIntervalLiketo an instance ofIntervalSetaccording to the following rules:- if the specified instance of
IntervalLikeis an instance ofIntervalSet, just returns the reference to this instance, without creating any new instances - 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
- Parameters:
intervalLike- specified instance ofIntervalLike- Returns:
- either existing or new instance of
IntervalSet
- if the specified instance of
-
ofSlots
-
ofGaps
-
union
Description copied from interface:IntervalLikeReturns a new instance ofIntervalSetthat represents a union of this instance and the specified other one.- Specified by:
unionin interfaceIntervalLike- Parameters:
other- specified otherIntervalLikeinstance- Returns:
- new instance of
IntervalSetthat represents a union of this instance and the specified other one.
-
exclusion
Description copied from interface:IntervalLikeReturns a new instance ofIntervalSetthat represents an exclusion of the specified instance from this one.- Specified by:
exclusionin interfaceIntervalLike- 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
-
hasNonZeroIntersectionWith
Description copied from interface:IntervalLikeChecks whether thisIntervalLikeinstance has a non-zero length intersection with the specified other one.Calling this method instead of
IntervalLike.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.- Specified by:
hasNonZeroIntersectionWithin interfaceIntervalLike- Parameters:
other- specified otherIntervalLikeinstance- Returns:
trueif thisIntervalLikehas a non-zero length intersection with the other one,falseotherwise
-
intersection
Description copied from interface:IntervalLikeReturns a new instance ofIntervalSetthat represents an intersection of this instance and the specified other one.- Specified by:
intersectionin interfaceIntervalLike- Parameters:
other- specified otherIntervalLikeinstance- Returns:
- new instance of
IntervalSetthat represents an intersection of this instance and the specified other one
-
contains
Description copied from interface:IntervalLikeChecks whether thisIntervalLikeinstance contains (i.e., fully covers) the specified other one.- Specified by:
containsin interfaceIntervalLike- Parameters:
other- specified otherIntervalLikeinstance- Returns:
trueif thisIntervalLikeinstance contains (i.e., fully covers) the specified other one,falseotherwise
-
withoutZeroIntervals
Returns a new instance ofIntervalSetthat is computed from this instance by removing all zero-length intervals.- Returns:
- new instance of
IntervalSetthat is computed from this instance by removing all zero-length intervals
-
withIntervalsMatching
Returns a newIntervalSetinstance containing only those intervals that match the specified predicate condition. This method does not modify the current instance.- Parameters:
predicate- APredicate<Interval>that defines the condition each interval must satisfy to be included in the resultantIntervalSet- Returns:
- A new
IntervalSetinstance containing only the intervals that match the predicate condition
-
withoutIntervalsMatching
Returns a newIntervalSetinstance excluding the intervals that match the specified predicate condition. This method does not modify the current instance.- Parameters:
predicate- APredicate<Interval>that defines the condition each interval must satisfy to be excluded from the resultantIntervalSet- Returns:
- A new
IntervalSetinstance containing only the intervals that do not satisfy the predicate condition
-
firstInterval
Returns anOptionalcontaining the first (leftmost) interval of this set, orOptional.empty()if this set is empty.- Returns:
Optionalcontaining the first (leftmost) interval if it exists
-
lastInterval
Returns anOptionalcontaining the last (rightmost) interval of this set, orOptional.empty()if this set is empty.- Returns:
Optionalcontaining the last (rightmost) interval if it exists
-
intervals
Returns an unmodifiable list of intervals contained in this interval set.- Returns:
- unmodifiable list of intervals
-
min
public double min()Description copied from interface:IntervalLikeReturns the minimal (left) bound of interval or interval set, orDouble.NaNif this instance is empty.- Specified by:
minin interfaceIntervalLike- Returns:
- left bound of interval or interval set, can be infinite or
Double.NaN
-
max
public double max()Description copied from interface:IntervalLikeReturns the maximal (right) bound of interval or interval set, orDouble.NaNif this instance is empty.- Specified by:
maxin interfaceIntervalLike- Returns:
- right bound of interval or interval set, can be infinite or
Double.NaN
-
length
public double length()Description copied from interface:IntervalLikeReturns the length of thisIntervalLikeinstance, more specifically:IntervalLike.max()-IntervalLike.min()for anInterval,- Sum of all intervals lengths for an
IntervalSet, - Zero for empty intervals and interval sets.
- Specified by:
lengthin interfaceIntervalLike- Returns:
- length of this
IntervalLikeinstance, zero or a positive number, or aDobule.POSITIVE_INFINITY
-
size
public int size()Description copied from interface:IntervalLikeReturns the number of intervals for interval set, or 1 for interval. If this instance is empty (IntervalLike.isEmpty()== true), returns 0.- Specified by:
sizein interfaceIntervalLike- Returns:
- number of intervals for interval set, or 1 for interval, or 0
-
isEmpty
public boolean isEmpty()Description copied from interface:IntervalLikeChecks 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());- Specified by:
isEmptyin interfaceIntervalLike- Returns:
trueif this instance is an empty interval or interval set,falseotherwise- See Also:
-
isUniverse
public boolean isUniverse()Description copied from interface:IntervalLikeChecks 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.- Specified by:
isUniversein interfaceIntervalLike- Returns:
trueif this instance is a universe interval or interval set,falseotherwise
-
toString
-
hashCode
public int hashCode() -
equals
-
iterator
-
toCodeString
Description copied from interface:IntervalLikeReturns a Java code that creates anIntervalLikeequal to this one. Can be used, for example, for unit testing and debugging.- Specified by:
toCodeStringin interfaceIntervalLike- Returns:
- string representing a Java code that creates an
IntervalLikeequal to this one
-
leftSubset
Description copied from interface:IntervalLikeReturns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsIntervalLike.min()boundary, with the cumulative length not exceeding the specified maximum length. IfIntervalLike.min()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.- Specified by:
leftSubsetin interfaceIntervalLike- 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
Description copied from interface:IntervalLikeReturns a new instance ofIntervalSetthat is a subset of thisIntervalLikeinstance taken from itsIntervalLike.max()boundary, with the cumulative length not exceeding the specified maximum length. IfIntervalLike.max()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.- Specified by:
rightSubsetin interfaceIntervalLike- 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
-
isZeroLength
public boolean isZeroLength()Description copied from interface:IntervalLikeChecks 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 ofIntervalLike.isEmpty()method for details on the difference between empty and zero-length intervals and interval sets.- Specified by:
isZeroLengthin interfaceIntervalLike- Returns:
trueif thisIntervalLikeinstance has zero length,falseotherwise- See Also:
-
equalsWithinEpsilon
Description copied from interface:IntervalLikeChecks whether thisIntervalLikeinstance is equal to the specified other one with respect to the default epsilon. The default epsilon can be obtained byCompare.getEpsilon()method.- Specified by:
equalsWithinEpsilonin interfaceIntervalLike- Parameters:
other- specified otherIntervalLikeinstance- Returns:
trueif thisIntervalLikeinstance is equal to the specified other one with respect to the default epsilon,falseotherwise
-
equalsWithinEpsilon
Description copied from interface:IntervalLikeChecks whether thisIntervalLikeinstance is equal to the specified other one with respect to the epsilon specified in theEpsilonComparatorinstance.- Specified by:
equalsWithinEpsilonin interfaceIntervalLike- 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
-