public abstract class DiscreteDomain<C extends java.lang.Comparable>
extends java.lang.Object
Comparable
domain such as all Integer
instances. A discrete domain is one that supports the three basic operations: next(C)
,
previous(C)
and distance(C, C)
, according to their specifications. The methods minValue()
and maxValue()
should also be overridden for bounded types.
A discrete domain always represents the entire set of values of its type; it cannot represent partial domains such as "prime integers" or "strings of length 5."
See the Guava User Guide section on DiscreteDomain
.
Modifier and Type | Class and Description |
---|---|
private static class |
DiscreteDomain.BigIntegerDomain |
private static class |
DiscreteDomain.IntegerDomain |
private static class |
DiscreteDomain.LongDomain |
Modifier and Type | Field and Description |
---|---|
(package private) boolean |
supportsFastOffset |
Modifier | Constructor and Description |
---|---|
protected |
DiscreteDomain()
Constructor for use by subclasses.
|
private |
DiscreteDomain(boolean supportsFastOffset)
Private constructor for built-in DiscreteDomains supporting fast offset.
|
Modifier and Type | Method and Description |
---|---|
static DiscreteDomain<java.math.BigInteger> |
bigIntegers()
Returns the discrete domain for values of type
BigInteger . |
abstract long |
distance(C start,
C end)
Returns a signed value indicating how many nested invocations of
next(C) (if positive) or
previous(C) (if negative) are needed to reach end starting from start . |
static DiscreteDomain<java.lang.Integer> |
integers()
Returns the discrete domain for values of type
Integer . |
static DiscreteDomain<java.lang.Long> |
longs()
Returns the discrete domain for values of type
Long . |
C |
maxValue()
Returns the maximum value of type
C , if it has one. |
C |
minValue()
Returns the minimum value of type
C , if it has one. |
abstract C |
next(C value)
Returns the unique least value of type
C that is greater than value , or null if none exists. |
(package private) C |
offset(C origin,
long distance)
Returns, conceptually, "origin + distance", or equivalently, the result of calling
next(C) on origin distance times. |
abstract C |
previous(C value)
Returns the unique greatest value of type
C that is less than value , or null if none exists. |
protected DiscreteDomain()
private DiscreteDomain(boolean supportsFastOffset)
public static DiscreteDomain<java.lang.Integer> integers()
Integer
.DiscreteDomains.integers()
)public static DiscreteDomain<java.lang.Long> longs()
Long
.DiscreteDomains.longs()
)public static DiscreteDomain<java.math.BigInteger> bigIntegers()
BigInteger
.C offset(C origin, long distance)
next(C)
on origin
distance
times.public abstract C next(C value)
C
that is greater than value
, or null
if none exists. Inverse operation to previous(C)
.value
- any value of type C
value
, or null
if value
is maxValue()
public abstract C previous(C value)
C
that is less than value
, or null
if none exists. Inverse operation to next(C)
.value
- any value of type C
value
, or null
if value
is minValue()
public abstract long distance(C start, C end)
next(C)
(if positive) or
previous(C)
(if negative) are needed to reach end
starting from start
.
For example, if end = next(next(next(start)))
, then distance(start, end) == 3
and distance(end, start) == -3
. As well, distance(a, a)
is always zero.
Note that this function is necessarily well-defined for any discrete type.
Long.MIN_VALUE
or Long.MAX_VALUE
if
the distance is too small or too large, respectively.public C minValue()
C
, if it has one. The minimum value is the unique
value for which Comparable.compareTo(Object)
never returns a positive value for any
input of type C
.
The default implementation throws NoSuchElementException
.
C
; never nulljava.util.NoSuchElementException
- if the type has no (practical) minimum value; for example,
BigInteger
public C maxValue()
C
, if it has one. The maximum value is the unique
value for which Comparable.compareTo(Object)
never returns a negative value for any
input of type C
.
The default implementation throws NoSuchElementException
.
C
; never nulljava.util.NoSuchElementException
- if the type has no (practical) maximum value; for example,
BigInteger