T - The type that represents a single point from the domain of definition of the
interval.public abstract class Interval<T extends Comparable<? super T>> extends Object
Dates
or basically any type, the elements of which can be compared to one another. Since the
class requires its generic variable to implement the Comparable interface, all
comparisons in the internals of the Interval class are done via the interface method.
When subclassing the Interval class, note that the start and end points of the
interval can be null. A null start point represents
the negative infinity and a null end point represents positive infinity. This
fact needs to be kept in mind in particular when overwriting methods with default
implementations in the Interval class, such as contains(Comparable),
isLeftOf(Comparable), and in particular equals(Object) and
hashCode().| Modifier and Type | Class and Description |
|---|---|
static class |
Interval.Bounded
An enum representing all possible types of bounded intervals.
|
class |
Interval.Builder
Used to create new intervals in an intuitive fashion by using the builder pattern.
|
static class |
Interval.Unbounded |
| Modifier and Type | Field and Description |
|---|---|
static Comparator<Interval> |
sweepLeftToRight
A comparator that can be used as a parameter for sorting functions.
|
static Comparator<Interval> |
sweepRightToLeft
A comparator that can be used as a parameter for sorting functions.
|
| Constructor and Description |
|---|
Interval()
Instantiates a new interval representing all points in the domain of definition,
i.e.
|
Interval(T value,
Interval.Unbounded type)
Instantiates a new unbounded interval - an interval that extends to positive or
negative infinity.
|
Interval(T start,
T end,
Interval.Bounded type)
Instantiates a new bounded interval.
|
| Modifier and Type | Method and Description |
|---|---|
Interval.Builder |
builder() |
protected int |
compareSpecialization(Interval<T> other)
A method that should be overwritten by subclasses of
Interval, if they have properties
that characterize the objects of the class and are used to identify them. |
boolean |
contains(Interval<T> another)
Checks if the current interval contains the entirety of another interval.
|
boolean |
contains(T query)
Determines if the current interval contains a query point.
|
protected abstract Interval<T> |
create()
Used to create new instances of a specific
Interval subclass. |
protected Interval<T> |
create(T start,
boolean isStartInclusive,
T end,
boolean isEndInclusive)
Creates a new instance of the particular
Interval subclass. |
boolean |
equals(Object obj) |
T |
getEnd()
Returns the end point of the interval.
|
Interval<T> |
getIntersection(Interval<T> other)
Returns an interval, representing the intersection of two intervals.
|
abstract T |
getMidpoint()
Returns the center of the current interval.
|
T |
getStart()
Returns the start point of the interval.
|
int |
hashCode() |
boolean |
intersects(Interval<T> query)
Checks if the current interval intersects another interval.
|
boolean |
isEmpty()
Checks if the current interval contains no points.
|
boolean |
isEndInclusive()
Returns
true, if the end point is a part of the interval, or false otherwise. |
boolean |
isLeftOf(Interval<T> other)
This method checks, if this current interval is entirely to the left of another interval
with no common points.
|
boolean |
isLeftOf(T point)
This method checks, if this current interval is entirely to the left of a point.
|
boolean |
isLeftOf(T point,
boolean inclusive)
This method checks, if this current interval is entirely to the left of a point.
|
boolean |
isPoint()
Determines if the current interval is a single point.
|
boolean |
isRightOf(Interval<T> other)
This method checks, if this current interval is entirely to the right of another interval
with no common points.
|
boolean |
isRightOf(T point)
This method checks, if this current interval is entirely to the right of a point.
|
boolean |
isRightOf(T point,
boolean inclusive)
This method checks, if this current interval is entirely to the right of a point.
|
boolean |
isStartInclusive()
Returns
true, if the start point is a part of the interval, or false otherwise. |
public static Comparator<Interval> sweepLeftToRight
Intervals with start point null (negative infinity) are considered smaller than all other intervals. If two intervals have the same start point, the closed start point is considered smaller than the open one. For example, [0, 2) is considered smaller than (0, 2).
To ensure that this comparator can also be used in sets it considers the end points of the intervals, if the start points are the same. Otherwise the set will not be able to handle two different intervals, sharing the same starting point, and omit one of the intervals.
Since this is a static method of a generic class, it involves unchecked calls to class methods. It is left to ths user to ensure that she compares intervals from the same class, otherwise an exception might be thrown.
public static Comparator<Interval> sweepRightToLeft
Intervals with end point null (positive infinity) are placed before all other intervals. If two intervals have the same end point, the closed end point is placed before the open one. For example, [0, 10) is placed after (0, 10].
To ensure that this comparator can also be used in sets it considers the start points of the intervals, if the end points are the same. Otherwise the set will not be able to handle two different intervals, sharing the same end point, and omit one of the intervals.
Since this is a static method of a generic class, it involves unchecked calls to class methods. It is left to ths user to ensure that she compares intervals from the same class, otherwise an exception might be thrown.
public Interval()
public Interval(T start, T end, Interval.Bounded type)
start - The start point of the intervalend - The end point of the interval.type - Description of whether the interval is open/closed at one or both
of its ends. See the documentation of the Bounded enum
for more information on the different possibilities.public Interval(T value, Interval.Unbounded type)
value - The bounding value for either the start or the end point.type - Describes, if the interval extends to positive or negative infinity,
as well as if it is open or closed at the bounding point. See the Unbounded enum for description of the different possibilities.public boolean isEmpty()
In particular, if the end point is less than the start point, then the interval is
considered to be empty. There are, however other instances, in which an interval is empty.
For example, in the class IntegerInterval, an open interval, whose start and end
points differ by one, for example the interval (4, 5), is empty, because it contains no integers
in it. The same interval, however, will not be considered empty in the
DoubleInterval class, because there are Double numbers within this interval.
true, if the current interval is empty or false otherwise.protected abstract Interval<T> create()
Interval subclass.
The Interval class aims to avoid reflexion. On several occasions, however, the class
needs to create new instances of the Interval class. To be able to guarantee that they
will have the desired runtime type, the create() method of a specific reference object
is called.
Generally, the only thing you need to do in your implementation of this abstract method is to call the default constructor of your subclass and return the new interval.
Interval class.public abstract T getMidpoint()
TreeNode inside a IntervalTree, which is why
it is not necessary to return exactly the center of the interval, but it will help the
IntervalTree perform slightly better.null otherwise. If the
center exists but can't be determined correctly, return any point inside the interval.protected Interval<T> create(T start, boolean isStartInclusive, T end, boolean isEndInclusive)
Interval subclass.start - The start point of the intervalisStartInclusive - true, if the start is inclusive or false otherwiseend - The end point of the intervalisEndInclusive - true, if the end is inclusive or false otherwisepublic T getStart()
public T getEnd()
public boolean isStartInclusive()
true, if the start point is a part of the interval, or false otherwise.public boolean isEndInclusive()
true, if the end point is a part of the interval, or false otherwise.public boolean isPoint()
true, if the current interval represents a single point.public boolean contains(T query)
query - The point.true, if the current interval contains the query point or false otherwise.public Interval<T> getIntersection(Interval<T> other)
x in the returned interval, x will belong in both the current interval
and the other interval.other - The other intervalother interval.public boolean contains(Interval<T> another)
true, if for every point x from the interval another
this point x also belongs to the current interval.another - Another interval.true, if the interval another is contained in the current interval in
its entirety, or false otherwise.public boolean intersects(Interval<T> query)
true if there is at least one point the current interval, that also
belongs to the query interval.query - The interval being checked for intersection with the current interval.true, if the two intervals intersect or false otherwise.public boolean isRightOf(T point, boolean inclusive)
true, if for every point x from the current interval the inequality
x > point holds. If the parameter inclusive is set to false, this
method will return true also if the start point of the interval is equal to the reference
point.point - The reference pointinclusive - false if the reference point is allowed to be the start point
of the current interval.true, if the current interval is entirely to the right of the other
interval, or false instead.public boolean isRightOf(T point)
x from the current interval the inequality
x > point holds. This formal definition implies in particular that if the start point
of the current interval is equal to the reference point and the end point is open, the method
will return true.point - The reference pointtrue, if the current interval is entirely to the right of the other
interval, or false instead.public boolean isRightOf(Interval<T> other)
x
from the current interval and for every point y from the other interval the
inequality x > y holds. This formal definition implies in particular that if the start point
of the current interval is equal to the end point of the other interval, the method
will return false only if both points are inclusive and true in all other cases.other - The reference intervaltrue, if the current interval is entirely to the right of the other
interval, or false instead.public boolean isLeftOf(T point, boolean inclusive)
true, if for every point x from the current interval the inequality
x < point holds. If the parameter inclusive is set to false, this
method will return true also if the end point of the interval is equal to the reference
point.point - The reference pointinclusive - false if the reference point is allowed to be the end point
of the current interval.true, if the current interval is entirely to the left of the other
interval, or false instead.public boolean isLeftOf(T point)
x from the current interval the inequality
x < point holds. This formal definition implies in particular that if the end point
of the current interval is equal to the reference point and the end point is open, the method
will return true.point - The reference pointtrue, if the current interval is entirely to the left of the other
interval, or false instead.public boolean isLeftOf(Interval<T> other)
x
from the current interval and for every point y from the other interval the
inequality x < y holds. This formal definition implies in particular that if the end point
of the current interval is equal to the start point of the other interval, the method
will return false only if both points are inclusive and true in all other cases.other - The reference intervaltrue, if the current interval is entirely to the left of the other
interval, or false instead.protected int compareSpecialization(Interval<T> other)
Interval, if they have properties
that characterize the objects of the class and are used to identify them. It is used to create
a total order between distinct objects, that would otherwise be considered equal, if only
the start and end points were considered. If you don't have any such special properties, you
may leave the default implementation of this method.
This method functions as a traditional Comparator, bit can not and should not be used
on its own, nor should it be implemented as a full standalone comparator. Instead, it is always
used in conjunction with one of the two base Comparators in the Interval class -
sweepLeftToRight and sweepRightToLeft. This method will only be executed if
the main comparator returns 0, i.e. if it considers the intervals to be equal. At that moment,
the start and end points would already have been compared to one another, which is why this method
should disregard the start and end points completely and only consider the
special properties defined in the particular subclass.
It is vital to overwrite this method, if you have any properties in your subclass, that identify
the interval, such as for example user IDs, student IDs or room numbers. The two base comparators
are used within the underlying TreeSets, which may discard two distinct interval
objects, that have the same start and end points.
other - The object that is being compared to this intervalother,other.public Interval.Builder builder()
Copyright © 2016. All rights reserved.