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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionConverts this instance ofIntervalLike
to an instance ofIntervalSet
according to the following rules: if the specified instance ofIntervalLike
is already an instance ofIntervalSet
, just returns the reference to this instance, without creating any new objects otherwise, if the specified instance ofIntervalLike
is an instance ofInterval
, creates a new instance ofIntervalSet
with this single interval, and returns the reference to this newly created instanceboolean
contains
(IntervalLike other) Checks whether thisIntervalLike
instance contains (i.e., fully covers) the specified other one.boolean
boolean
equalsWithinEpsilon
(IntervalLike other) Checks whether thisIntervalLike
instance is equal to the specified other one with respect to the default epsilon.boolean
equalsWithinEpsilon
(IntervalLike other, EpsilonComparator comparator) Checks whether thisIntervalLike
instance is equal to the specified other one with respect to the epsilon specified in theEpsilonComparator
instance.exclusion
(IntervalLike other) Returns a new instance ofIntervalSet
that represents an exclusion of the specified instance from this one.int
hashCode()
intersection
(IntervalLike other) Returns a new instance ofIntervalSet
that represents an intersection of this instance and the specified other one.boolean
isEmpty()
Checks whether this instance is an empty interval or interval set, i.e.boolean
Checks whether this instance is a universe interval or interval set, i.e.leftSubset
(double maxLength) Returns a new instance ofIntervalSet
that is a subset of thisIntervalLike
instance taken from itsIntervalLike.min()
boundary, with the cumulative length not exceeding the specified maximum length.double
max()
Returns the maximal (right) bound of interval or interval set, orDouble.NaN
if this instance is empty.double
min()
Returns the minimal (left) bound of interval or interval set, orDouble.NaN
if this instance is empty.static Interval
of
(double value) Creates a new instance ofInterval
which is zero-length and contains only the specified point.static Interval
of
(double min, double max) Creates a new instance ofInterval
with the specified minimal and maximal bounds.rightSubset
(double maxLength) Returns a new instance ofIntervalSet
that is a subset of thisIntervalLike
instance taken from itsIntervalLike.max()
boundary, with the cumulative length not exceeding the specified maximum length.shift
(double shift) Creates a new instance ofInterval
which equals to this interval shifted by the specified value.int
size()
Returns the number of intervals for interval set, or 1 for interval.Returns a Java code that creates anIntervalLike
equal to this one.toString()
union
(IntervalLike other) Returns a new instance ofIntervalSet
that 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, wait
Methods 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 ofInterval
with 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
IllegalArgumentException
is 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 ofInterval
with 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 ofInterval
which 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 ofInterval
which equals to this interval shifted by the specified value.- Parameters:
shift
- shift, can be zero, positive or negative, cannot beDouble.NaN
or infinite- Returns:
- new instance of
Interval
which equals to this interval shifted by the specified value
-
min
public double min()Description copied from interface:IntervalLike
Returns the minimal (left) bound of interval or interval set, orDouble.NaN
if this instance is empty.- Specified by:
min
in interfaceIntervalLike
- Returns:
- left bound of interval or interval set, can be infinite or
Double.NaN
-
max
public double max()Description copied from interface:IntervalLike
Returns the maximal (right) bound of interval or interval set, orDouble.NaN
if this instance is empty.- Specified by:
max
in interfaceIntervalLike
- Returns:
- right bound of interval or interval set, can be infinite or
Double.NaN
-
isEmpty
public boolean isEmpty()Description copied from interface:IntervalLike
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.- Specified by:
isEmpty
in interfaceIntervalLike
- Returns:
true
if this instance is an empty interval or interval set,false
otherwise
-
size
public int size()Description copied from interface:IntervalLike
Returns the number of intervals for interval set, or 1 for interval. If this instance is empty (IntervalLike.isEmpty()
== true), returns 0.- Specified by:
size
in interfaceIntervalLike
- Returns:
- number of intervals for interval set, or 1 for interval, or 0
-
isUniverse
public boolean isUniverse()Description copied from interface:IntervalLike
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.- Specified by:
isUniverse
in interfaceIntervalLike
- Returns:
true
if this instance is a universe interval or interval set,false
otherwise
-
union
Description copied from interface:IntervalLike
Returns a new instance ofIntervalSet
that represents a union of this instance and the specified other one.- Specified by:
union
in interfaceIntervalLike
- Parameters:
other
- specified otherIntervalLike
instance- Returns:
- new instance of
IntervalSet
that represents a union of this instance and the specified other one.
-
exclusion
Description copied from interface:IntervalLike
Returns a new instance ofIntervalSet
that represents an exclusion of the specified instance from this one.- Specified by:
exclusion
in interfaceIntervalLike
- Parameters:
other
- specified otherIntervalLike
instance that is being excluded from this one- Returns:
- new instance of
IntervalSet
that represents an exclusion of the specified instance from this one
-
intersection
Description copied from interface:IntervalLike
Returns a new instance ofIntervalSet
that represents an intersection of this instance and the specified other one.- Specified by:
intersection
in interfaceIntervalLike
- Parameters:
other
- specified otherIntervalLike
instance- Returns:
- new instance of
IntervalSet
that represents an intersection of this instance and the specified other one
-
contains
Description copied from interface:IntervalLike
Checks whether thisIntervalLike
instance contains (i.e., fully covers) the specified other one.- Specified by:
contains
in interfaceIntervalLike
- Parameters:
other
- specified otherIntervalLike
instance- Returns:
true
if thisIntervalLike
instance contains (i.e., fully covers) the specified other one,false
otherwise
-
asIntervalSet
Description copied from interface:IntervalLike
Converts this instance ofIntervalLike
to an instance ofIntervalSet
according to the following rules:- if the specified instance of
IntervalLike
is already an instance ofIntervalSet
, just returns the reference to this instance, without creating any new objects - otherwise, if the specified instance of
IntervalLike
is an instance ofInterval
, creates a new instance ofIntervalSet
with this single interval, and returns the reference to this newly created instance
- Specified by:
asIntervalSet
in 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:IntervalLike
Checks whether thisIntervalLike
instance is equal to the specified other one with respect to the default epsilon. The default epsilon can be obtained byCompare.getEpsilon()
method.- Specified by:
equalsWithinEpsilon
in interfaceIntervalLike
- Parameters:
other
- specified otherIntervalLike
instance- Returns:
true
if thisIntervalLike
instance is equal to the specified other one with respect to the default epsilon,false
otherwise
-
equalsWithinEpsilon
Description copied from interface:IntervalLike
Checks whether thisIntervalLike
instance is equal to the specified other one with respect to the epsilon specified in theEpsilonComparator
instance.- Specified by:
equalsWithinEpsilon
in interfaceIntervalLike
- Parameters:
other
- specified otherIntervalLike
instancecomparator
- anEpsilonComparator
instance specifying the tolerance of comparison- Returns:
true
if thisIntervalLike
instance is equal to the specified other one with respect to the specified epsilon,false
otherwise
-
toString
-
toCodeString
Description copied from interface:IntervalLike
Returns a Java code that creates anIntervalLike
equal to this one. Can be used, for example, for unit testing and debugging.- Specified by:
toCodeString
in interfaceIntervalLike
- Returns:
- string representing a Java code that creates an
IntervalLike
equal to this one
-
leftSubset
Description copied from interface:IntervalLike
Returns a new instance ofIntervalSet
that is a subset of thisIntervalLike
instance taken from itsIntervalLike.min()
boundary, with the cumulative length not exceeding the specified maximum length. IfIntervalLike.min()
boundary of thisIntervalLike
isDouble.NEGATIVE_INFINITY
, indicating an unbounded left boundary, anIllegalStateException
is thrownIf the specified length is >= the length of this
IntervalLike
, a copy of this instance is returned.- Specified by:
leftSubset
in interfaceIntervalLike
- Parameters:
maxLength
- the maximum cumulative length of intervals to include in the subset. Must be non-negative.- Returns:
- An
IntervalSet
representing the leftmost subset of thisIntervalLike
instance with length not exceeding the specified limit
-
rightSubset
Description copied from interface:IntervalLike
Returns a new instance ofIntervalSet
that is a subset of thisIntervalLike
instance taken from itsIntervalLike.max()
boundary, with the cumulative length not exceeding the specified maximum length. IfIntervalLike.max()
boundary of thisIntervalLike
isDouble.POSITIVE_INFINITY
, indicating an unbounded right boundary, anIllegalStateException
is thrownIf the specified length is >= the length of this
IntervalLike
, a copy of this instance is returned.- Specified by:
rightSubset
in interfaceIntervalLike
- Parameters:
maxLength
- the maximum cumulative length of intervals to include in the subset. Must be non-negative.- Returns:
- An
IntervalSet
representing the rightmost subset of thisIntervalLike
instance with length not exceeding the specified limit
-