Class Bounds<T>
- Type Parameters:
T
- Java type of range bounds
AbstractNavigableSet
and AbstractNavigableMap
to define
the (optional) upper and lower bounds of a restricted range.
Instances are immutable.
-
Constructor Summary
ConstructorDescriptionBounds()
Convenience constructor to create an unbounded instance, i.e., having neither upper nor lower bounds.Create two-sided bounds.Create a one-sided bound.Primary constructor.Create two-sided bounds with inclusive lower bound and exclusive upper bound. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Bounds<T>
eq
(T value) Create an instance with bounds that allow only the given value.boolean
static <T> Bounds<T>
ge
(T lowerBound) Create an instance with an inclusive lower bound.Get the Java value corresponding to the lower bound restriction, if any.Get the type of the lower bound that corresponds togetLowerBound()
.Get the Java value corresponding to the upper bound restriction, if any.Get the type of the upper bound that corresponds togetUpperBound()
.static <T> Bounds<T>
gt
(T lowerBound) Create an instance with an exclusive lower bound.int
hashCode()
boolean
Determine whether this instance has a lower bound.boolean
Determine whether this instance has an upper bound.boolean
isEmpty
(Comparator<? super T> comparator) Determine if this instance is provably empty, i.e., no values can possibly be within the bounds.boolean
isInverted
(Comparator<? super T> comparator) Determine if this instance has bounds that are "inverted" with respect to the givenComparator
.boolean
Determine whether this instance is unbounded, i.e., it has neither a lower nor an upper bound.boolean
isWithinBounds
(Comparator<? super T> comparator, Bounds<? extends T> other) Determine whether the givenBounds
are within of the bounds of this instance.boolean
isWithinBounds
(Comparator<? super T> comparator, T value) Check whether the given value is within the bounds of this instance.boolean
isWithinLowerBound
(Comparator<? super T> comparator, T value) Check whether the given value is within the lower bound of this instance.boolean
isWithinUpperBound
(Comparator<? super T> comparator, T value) Check whether the given value is within the upper bound of this instance.static <T> Bounds<T>
le
(T upperBound) Create an instance with an inclusive upper bound.static <T> Bounds<T>
lt
(T upperBound) Create an instance with an exclusive upper bound.reverse()
Create an instance like this instance but with the upper and lower bounds reversed.toString()
union
(Comparator<? super T> comparator, Bounds<T> other) Consolidate the ranges of values implied by this instance and the given instance, if possible.withLowerBound
(T newLowerBound, BoundType newLowerBoundType) Create an instance like this instance but with a different lower bound.Create an instance like this instance but with the lower bound removed.Create an instance like this instance but with the upper bound removed.withUpperBound
(T newUpperBound, BoundType newUpperBoundType) Create an instance like this instance but with a different upper bound.
-
Constructor Details
-
Bounds
public Bounds()Convenience constructor to create an unbounded instance, i.e., having neither upper nor lower bounds. -
Bounds
Create a one-sided bound.- Parameters:
bound
- bound restriction value; ignored ifboundType
isBoundType.NONE
boundType
- type of bound forbound
, orBoundType.NONE
if there is no lower boundupper
- true to create an upper bound, false to create a lower bound- Throws:
IllegalArgumentException
- ifboundType
is null
-
Bounds
Create two-sided bounds with inclusive lower bound and exclusive upper bound.- Parameters:
lowerBound
- lower bound restriction value (inclusive)upperBound
- upper bound restriction value (exclusive)
-
Bounds
Create two-sided bounds.- Parameters:
lowerBound
- lower bound restriction value (inclusive)lowerInclusive
- true iflowerBound
is inclusive, false iflowerBound
is exclusiveupperBound
- upper bound restriction value (exclusive)upperInclusive
- true ifupperBound
is inclusive, false ifupperBound
is exclusive
-
Bounds
Primary constructor.- Parameters:
lowerBound
- lower bound restriction value; ignored iflowerBoundType
isBoundType.NONE
lowerBoundType
- type of bound forlowerBound
, orBoundType.NONE
if there is no lower boundupperBound
- upper bound restriction value; ignored ifupperBoundType
isBoundType.NONE
upperBoundType
- type of bound forupperBound
, orBoundType.NONE
if there is no upper bound- Throws:
IllegalArgumentException
- iflowerBoundType
orupperBoundType
is null
-
-
Method Details
-
hasLowerBound
public boolean hasLowerBound()Determine whether this instance has a lower bound.- Returns:
- false if this instance's lower bound type is
BoundType.NONE
, otherwise true
-
hasUpperBound
public boolean hasUpperBound()Determine whether this instance has an upper bound.- Returns:
- false if this instance's upper bound type is
BoundType.NONE
, otherwise true
-
isUnbounded
public boolean isUnbounded()Determine whether this instance is unbounded, i.e., it has neither a lower nor an upper bound.- Returns:
- true if this instance has neither bounds, otherwise false
-
getLowerBound
Get the Java value corresponding to the lower bound restriction, if any. Returns null whengetLowerBoundType()
returnsBoundType.NONE
.- Returns:
- lower bound Java value, or null if there is no lower bound
-
getUpperBound
Get the Java value corresponding to the upper bound restriction, if any. Returns null whengetUpperBoundType()
returnsBoundType.NONE
.- Returns:
- upper bound Java value, or null if there is no upper bound
-
getLowerBoundType
Get the type of the lower bound that corresponds togetLowerBound()
.- Returns:
- lower bound restriction type, never null
-
getUpperBoundType
Get the type of the upper bound that corresponds togetUpperBound()
.- Returns:
- upper bound restriction type, never null
-
reverse
Create an instance like this instance but with the upper and lower bounds reversed. Obviously, the result is only sensible when a reversedComparator
is also used.- Returns:
- reversal of this instance
-
withLowerBound
Create an instance like this instance but with a different lower bound.- Parameters:
newLowerBound
- new lower bound restriction value; ignored ifnewLowerBoundType
isBoundType.NONE
newLowerBoundType
- type of bound fornewLowerBound
, orBoundType.NONE
if there is no lower bound- Returns:
- instance with new lower bound
- Throws:
IllegalArgumentException
- ifnewLowerBoundType
is null
-
withUpperBound
Create an instance like this instance but with a different upper bound.- Parameters:
newUpperBound
- new upper bound restriction value; ignored ifnewUpperBoundType
isBoundType.NONE
newUpperBoundType
- type of bound fornewUpperBound
, orBoundType.NONE
if there is no upper bound- Returns:
- instance with new upper bound
- Throws:
IllegalArgumentException
- ifnewUpperBoundType
is null
-
withoutLowerBound
Create an instance like this instance but with the lower bound removed.- Returns:
- new instance
-
withoutUpperBound
Create an instance like this instance but with the upper bound removed.- Returns:
- new instance
-
isWithinLowerBound
Check whether the given value is within the lower bound of this instance.- Parameters:
comparator
- comparator used to compare values, or null for natural orderingvalue
- value to check- Returns:
- true if
value
is within bounds, false otherwise
-
isWithinUpperBound
Check whether the given value is within the upper bound of this instance.- Parameters:
comparator
- comparator used to compare values, or null for natural orderingvalue
- value to check- Returns:
- true if
value
is within bounds, false otherwise
-
isWithinBounds
Check whether the given value is within the bounds of this instance.Equivalent to:
isWithinLowerBound(comparator, value) && isWithinUpperBound(comparator, value)
- Parameters:
comparator
- comparator used to compare values, or null for natural orderingvalue
- value to check- Returns:
- true if
value
is within bounds, false otherwise
-
isWithinBounds
Determine whether the givenBounds
are within of the bounds of this instance.- Parameters:
comparator
- comparator used to compare values, or null for natural orderingother
- other bounds- Returns:
- true if
other
is in range, false otherwise - Throws:
IllegalArgumentException
- ifother
is null
-
union
Consolidate the ranges of values implied by this instance and the given instance, if possible.If the value ranges implied by the two bounds are not overlapping or adjacent, then null is returned. Otherwise an instance is returned that corresponds to the union of the two implied value ranges.
Some cases not handled, e.g., when one bound has an inclusive upper bound with value x and the other has an inclusive lower bound with value x + 1, because this method can't detect adjacent values.
- Parameters:
comparator
- comparator used to compare values, or null for natural orderingother
- instance to attempt consolidation with this one- Returns:
- consolidated bounds, or null if none is possible
- Throws:
IllegalArgumentException
- ifother
is null
-
isEmpty
Determine if this instance is provably empty, i.e., no values can possibly be within the bounds.- Parameters:
comparator
- comparator used to compare values, or null for natural ordering- Returns:
- true if this instance is backwards
-
isInverted
Determine if this instance has bounds that are "inverted" with respect to the givenComparator
.This instance is "inverted" if it has both lower and upper bounds and the lower bound's value is strictly greater than the upper bound's value. The bounds' types (i.e., whether
BoundType.INCLUSIVE
orBoundType.EXCLUSIVE
) is not considered.An inverted instance is always empty, but the reverse is not necessarily true.
- Parameters:
comparator
- comparator used to compare values, or null for natural ordering- Returns:
- true if this instance is backwards
-
eq
Create an instance with bounds that allow only the given value.- Type Parameters:
T
- Java type of range bounds- Parameters:
value
- unique bounded value- Returns:
- bounds containing exactly
value
-
ge
Create an instance with an inclusive lower bound.- Type Parameters:
T
- Java type of range bounds- Parameters:
lowerBound
- inclusive lower bound- Returns:
- bounds containing all values greater than or equal to
value
-
gt
Create an instance with an exclusive lower bound.- Type Parameters:
T
- Java type of range bounds- Parameters:
lowerBound
- exclusive lower bound- Returns:
- bounds containing all values greater than
value
-
le
Create an instance with an inclusive upper bound.- Type Parameters:
T
- Java type of range bounds- Parameters:
upperBound
- inclusive upper bound- Returns:
- bounds containing all values less than or equal to
value
-
lt
Create an instance with an exclusive upper bound.- Type Parameters:
T
- Java type of range bounds- Parameters:
upperBound
- exclusive upper bound- Returns:
- bounds containing all values less than
value
-
toString
-
equals
-
hashCode
public int hashCode()
-