public final class Interval<T extends java.lang.Comparable<T>>
extends java.lang.Object
This class is generic and can be used on any data type that implements the Comparable
interface.
To demonstrate how to use this class, consider the following example:
Interval<Integer> interval = Interval.of(0, 10);
boolean contains = interval.contains(5);
You may optionally specify the kind of interval you want to use, given an instance of the enum Interval.Kind
. This enum contains four values, Kind.CLOSED_CLOSED
, Kind.CLOSED_OPEN
, Kind.OPEN_CLOSED
and Kind.OPEN_OPEN
.
They all alter the behavior of the methods in this class slightly.
This class has potential to be thread-safe, in which case it's suitable for concurrent use without external synchronization. The Interval
class is itself "immutable", but the values stored in it may or may not be.
Modifier and Type | Class and Description |
---|---|
static class |
Interval.Kind
An enum that tells the
Interval class how it should alter the behavior of its methods. |
Modifier and Type | Method and Description |
---|---|
boolean |
contains(T c)
Checks if
c is contained within this Interval instance. |
T |
getA()
Returns the lesser value of this
Interval instance. |
T |
getB()
Returns the greater value of this
Interval instance. |
Interval.Kind |
getKind()
Returns the
Interval.Kind used by this Interval instance. |
boolean |
isGreaterThan(T c)
Checks if
c is greater than the greater value of this Interval instance. |
boolean |
isLessThan(T c)
Checks if
c is less than the lesser value of this Interval instance. |
static <T extends java.lang.Comparable<T>> |
of(T a,
T b)
Returns an
Interval instance based on the lesser value of a and the greater value of b . |
static <T extends java.lang.Comparable<T>> |
of(T a,
T b,
Interval.Kind kind)
Returns an
Interval instance based on the lesser value of a , the greater value of b and a given Kind . |
java.lang.String |
toString()
Returns a
String representation of this Interval instance. |
public boolean contains(T c)
c
is contained within this Interval
instance.
Returns true
if, and only if, c
is contained within this Interval
instance, false
otherwise.
The behavior that checks for containment of c
differs slightly, depending on the Interval.Kind
used by this Interval
instance.
If c
is null
, a NullPointerException
will be thrown.
c
- the value to check for containmenttrue
if, and only if, c
is contained within this Interval
instance, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, c
is null
public boolean isGreaterThan(T c)
c
is greater than the greater value of this Interval
instance.
Returns true
if, and only if, c
is greater than the greater value of this Interval
instance, false
otherwise.
The behavior that governs this method differs slightly, depending on the Interval.Kind
used by this Interval
instance.
If c
is null
, a NullPointerException
will be thrown.
c
- the value to checktrue
if, and only if, c
is greater than the greater value of this Interval
instance, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, c
is null
public boolean isLessThan(T c)
c
is less than the lesser value of this Interval
instance.
Returns true
if, and only if, c
is less than the lesser value of this Interval
instance, false
otherwise.
The behavior that governs this method differs slightly, depending on the Interval.Kind
used by this Interval
instance.
If c
is null
, a NullPointerException
will be thrown.
c
- the value to checktrue
if, and only if, c
is less than the lesser value of this Interval
instance, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, c
is null
public Interval.Kind getKind()
Interval.Kind
used by this Interval
instance.Kind
used by this Interval
instancepublic java.lang.String toString()
String
representation of this Interval
instance.toString
in class java.lang.Object
String
representation of this Interval
instancepublic T getA()
Interval
instance.Interval
instancepublic T getB()
Interval
instance.Interval
instancepublic static <T extends java.lang.Comparable<T>> Interval<T> of(T a, T b)
Interval
instance based on the lesser value of a
and the greater value of b
.
Calling this method is equivalent to calling Interval.of(a, b, Kind.CLOSED_CLOSED)
.
If either a
or b
are null
, a NullPointerException
will be thrown.
If a
is greater than b
, an IllegalArgumentException
will be thrown.
T
- the generic type of a
and b
a
- the lesser valueb
- the greater valueInterval
instance based on the lesser value of a
and the greater value of b
java.lang.IllegalArgumentException
- thrown if, and only if, a
is greater than b
java.lang.NullPointerException
- thrown if, and only if, either a
or b
are null
public static <T extends java.lang.Comparable<T>> Interval<T> of(T a, T b, Interval.Kind kind)
Interval
instance based on the lesser value of a
, the greater value of b
and a given Kind
.
If either a
, b
or kind
are null
, a NullPointerException
will be thrown.
If a
is greater than b
, an IllegalArgumentException
will be thrown.
T
- the generic type of a
and b
a
- the lesser valueb
- the greater valuekind
- the Kind
to assignInterval
instance based on the lesser value of a
, the greater value of b
and a given Kind
java.lang.IllegalArgumentException
- thrown if, and only if, a
is greater than b
java.lang.NullPointerException
- thrown if, and only if, either a
, b
or kind
are null