Class Interval
- All Implemented Interfaces:
IntervalLike,Comparable<IntervalLike>
- Direct Known Subclasses:
TimeInterval
min(), max()].
One or both of these values may be infinite although not with the same sign.
Intervals can have zero length, such intervals represent a single
point. IntervalLike.isZeroLength() method checks if the interval has zero
length
Empty interval represents a non-existent interval, such as a result of
intersection of disjoint intervals. isEmpty() method can be
used to check is an interval is empty.
- Author:
- Andrey Malykhanov
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConverts 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 instancebooleancontains(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.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.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.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.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 Intervalof(double value) Creates a new instance ofIntervalwhich is zero-length and contains only the specified point.static Intervalof(double min, double max) Creates a new instance ofIntervalwith the specified minimal and maximal bounds.rightSubset(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.shift(double shift) Creates a new instance ofIntervalwhich equals to this interval shifted by the specified value.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.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.amalgamasimulation.core.intervals.IntervalLike
compareTo, complement, contains, isFinite, isZeroLength, length
-
Field Details
-
min
protected double min -
max
protected double max -
UNIVERSE
Universe number interval, i.e. [-∞, +∞]. -
EMPTY
Empty number interval that represents a non-existent interval.
-
-
Constructor Details
-
Interval
public Interval(double min, double max) Creates a new instance ofIntervalwith the specified minimal and maximal bounds.If at least one of the bound is
Double.NaN, then an empty interval is created. Also, if both bounds either equal toDouble.POSITIVE_INFINITY, orDouble.NEGATIVE_INFINITY, then an empty interval is created.If the difference between the specified bounds is less that the default epsilon, i.e.,
Compare.equalTo(min, max) == true, then a zero-length interval is created with max = minIf maximal bound is less than minimal bound, an
IllegalArgumentExceptionis thrown.- Parameters:
min- minimal (left) bound of the interval being createdmax- maximal (right) bound of the interval being created
-
-
Method Details
-
of
Creates a new instance ofIntervalwith the specified minimal and maximal bounds.- Parameters:
min- minimal (left) bound of the interval being createdmax- maximal (right) bound of the interval being created- Returns:
- a new instance of
Interval
-
of
Creates a new instance ofIntervalwhich is zero-length and contains only the specified point.- Parameters:
value- value of the zero-length interval- Returns:
- a new instance of
Interval
-
shift
Creates a new instance ofIntervalwhich equals to this interval shifted by the specified value.- Parameters:
shift- shift, can be zero, positive or negative, cannot beDouble.NaNor infinite- Returns:
- new instance of
Intervalwhich equals to this interval shifted by the specified value
-
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
-
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:
-
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
-
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
-
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
-
asIntervalSet
Description copied from interface:IntervalLikeConverts 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
- Specified by:
asIntervalSetin interfaceIntervalLike- Returns:
- either existing or new instance of of
IntervalSet
- if the specified instance of
-
hashCode
public int hashCode() -
equals
-
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
-
toString
-
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
-