javadoc.com.google.common.collect.Range.html Maven / Gradle / Ivy
Range (Guava: Google Core Libraries for Java 11.0.1 API)
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
com.google.common.collect
Class Range<C extends Comparable>
java.lang.Object
com.google.common.collect.Range<C>
- All Implemented Interfaces:
- Predicate<C>, Serializable
@GwtCompatible
@Beta
public final class Range<C extends Comparable>
- extends Object
- implements Predicate<C>, Serializable
A range, sometimes known as an interval, is a convex
(informally, "contiguous" or "unbroken") portion of a particular domain.
Formally, convexity means that for any a <= b <= c
,
range.contains(a) && range.contains(c)
implies that range.contains(b)
.
A range is characterized by its lower and upper bounds (extremes), each of which can open (exclusive of its endpoint), closed (inclusive of its endpoint), or unbounded. This yields nine basic types of ranges:
(a..b) = {x | a < x < b}
[a..b] = {x | a <= x <= b}
[a..b) = {x | a <= x < b}
(a..b] = {x | a < x <= b}
(a..+∞) = {x | x > a}
[a..+∞) = {x | x >= a}
(-∞..b) = {x | x < b}
(-∞..b] = {x | x <= b}
(-∞..+∞) = all values
{x | statement}
is read "the set of all x such
that statement.")
Notice that we use a square bracket ([ ]
) to denote that an range
is closed on that end, and a parenthesis (( )
) when it is open or
unbounded.
The values a
and b
used above are called endpoints.
The upper endpoint may not be less than the lower endpoint. The endpoints may
be equal only if at least one of the bounds is closed:
[a..a]
: singleton range[a..a); (a..a]
: empty, but valid(a..a)
: invalid
Instances of this type can be obtained using the static factory methods in
the Ranges
class.
Instances of Range
are immutable. It is strongly encouraged to
use this class only with immutable data types. When creating a range over a
mutable type, take great care not to allow the value objects to mutate after
the range is created.
In this and other range-related specifications, concepts like "equal",
"same", "unique" and so on are based on Comparable.compareTo(T)
returning zero, not on Object.equals(java.lang.Object)
returning true
. Of
course, when these methods are kept consistent (as defined in Comparable
), this is not an issue.
A range a
is said to be the maximal range having property
P if, for all ranges b
also having property P, a.encloses(b)
. Likewise, a
is minimal when b.encloses(a)
for all b
having property P. See, for example,
the definition of intersection(com.google.common.collect.Range
.
This class can be used with any type which implements Comparable
;
it does not require Comparable<? super C>
because this would be
incompatible with pre-Java 5 types. If this class is used with a perverse
Comparable
type (Foo implements Comparable<Bar>
where Bar
is not a supertype of Foo
), any of its methods may throw ClassCastException
. (There is no good reason for such a type to exist.)
When evaluated as a Predicate
, a range yields the same result as
invoking contains(C)
.
- Since:
- 10.0
- Author:
- Kevin Bourrillion, Gregory Kick
- See Also:
- Serialized Form
Method Summary | |
---|---|
boolean |
apply(C input)
Equivalent to contains(C) ; provided only to satisfy the Predicate interface. |
ContiguousSet<C> |
asSet(DiscreteDomain<C> domain)
Returns an ImmutableSortedSet containing the same values in the
given domain contained by this range. |
Range<C> |
canonical(DiscreteDomain<C> domain)
Returns the canonical form of this range in the given domain. |
boolean |
contains(C value)
Returns true if value is within the bounds of this
range. |
boolean |
containsAll(Iterable<? extends C> values)
Returns true if every element in values is contained in this range. |
boolean |
encloses(Range<C> other)
Returns true if the bounds of other do not extend outside
the bounds of this range. |
boolean |
equals(Object object)
Returns true if object is a range having the same
endpoints and bound types as this range. |
int |
hashCode()
Returns a hash code for this range. |
boolean |
hasLowerBound()
Returns true if this range has a lower endpoint. |
boolean |
hasUpperBound()
Returns true if this range has an upper endpoint. |
Range<C> |
intersection(Range<C> other)
Returns the maximal range enclosed by both this range and other , if such a range exists. |
boolean |
isConnected(Range<C> other)
Returns true if there exists a (possibly empty) range which is
enclosed by both this range and other . |
boolean |
isEmpty()
Returns true if this range is of the form [v..v) or (v..v] . |
BoundType |
lowerBoundType()
Returns the type of this range's lower bound: BoundType.CLOSED if
the range includes its lower endpoint, BoundType.OPEN if it does
not. |
C |
lowerEndpoint()
Returns the lower endpoint of this range. |
Range<C> |
span(Range<C> other)
Returns the minimal range that encloses both this range and other . |
String |
toString()
Returns a string representation of this range, such as "[3..5)"
(other examples are listed in the class documentation). |
BoundType |
upperBoundType()
Returns the type of this range's upper bound: BoundType.CLOSED if
the range includes its upper endpoint, BoundType.OPEN if it does
not. |
C |
upperEndpoint()
Returns the upper endpoint of this range. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
hasLowerBound
public boolean hasLowerBound()
- Returns
true
if this range has a lower endpoint.
lowerEndpoint
public C lowerEndpoint()
- Returns the lower endpoint of this range.
- Throws:
IllegalStateException
- if this range is unbounded below (that is,hasLowerBound()
returnsfalse
)
lowerBoundType
public BoundType lowerBoundType()
- Returns the type of this range's lower bound:
BoundType.CLOSED
if the range includes its lower endpoint,BoundType.OPEN
if it does not.- Throws:
IllegalStateException
- if this range is unbounded below (that is,hasLowerBound()
returnsfalse
)
hasUpperBound
public boolean hasUpperBound()
- Returns
true
if this range has an upper endpoint.
upperEndpoint
public C upperEndpoint()
- Returns the upper endpoint of this range.
- Throws:
IllegalStateException
- if this range is unbounded above (that is,hasUpperBound()
returnsfalse
)
upperBoundType
public BoundType upperBoundType()
- Returns the type of this range's upper bound:
BoundType.CLOSED
if the range includes its upper endpoint,BoundType.OPEN
if it does not.- Throws:
IllegalStateException
- if this range is unbounded above (that is,hasUpperBound()
returnsfalse
)
isEmpty
public boolean isEmpty()
- Returns
true
if this range is of the form[v..v)
or(v..v]
. (This does not encompass ranges of the form(v..v)
, because such ranges are invalid and can't be constructed at all.)Note that certain discrete ranges such as the integer range
(3..4)
are not considered empty, even though they contain no actual values.
contains
public boolean contains(C value)
- Returns
true
ifvalue
is within the bounds of this range. For example, on the range[0..2)
,contains(1)
returnstrue
, whilecontains(2)
returnsfalse
.
apply
public boolean apply(C input)
- Equivalent to
contains(C)
; provided only to satisfy thePredicate
interface. When using a reference of typeRange
, always invokecontains(C)
directly instead.- Specified by:
apply
in interfacePredicate<C extends Comparable>
containsAll
public boolean containsAll(Iterable<? extends C> values)
- Returns
true
if every element invalues
is contained in this range.
encloses
public boolean encloses(Range<C> other)
- Returns
true
if the bounds ofother
do not extend outside the bounds of this range. Examples:[3..6]
encloses[4..5]
(3..6)
encloses(3..6)
[3..6]
encloses[4..4)
(even though the latter is empty)(3..6]
does not enclose[3..6]
[4..5]
does not enclose(3..6)
(even though it contains every value contained by the latter range)[3..6]
does not enclose(1..1]
(even though it contains every value contained by the latter range)
a.encloses(b)
, thenb.contains(v)
impliesa.contains(v)
, but as the last two examples illustrate, the converse is not always true.The encloses relation has the following properties:
- reflexive:
a.encloses(a)
is always true - antisymmetric:
a.encloses(b) && b.encloses(a)
impliesa.equals(b)
- transitive:
a.encloses(b) && b.encloses(c)
impliesa.encloses(c)
- not a total ordering:
!a.encloses(b)
does not implyb.encloses(a)
- there exists a maximal range, for which
encloses
is always true - there also exist minimal ranges, for
which
encloses(b)
is always false when!equals(b)
- if
a.encloses(b)
, thena.isConnected(b)
istrue
.
intersection
public Range<C> intersection(Range<C> other)
- Returns the maximal range enclosed by both this
range and
other
, if such a range exists.For example, the intersection of
[1..5]
and(3..7)
is(3..5]
. The resulting range may be empty; for example,[1..5)
intersected with[5..7)
yields the empty range[5..5)
.Generally, the intersection exists if and only if this range and
other
are connected.The intersection operation has the following properties:
- commutative:
a.intersection(b)
produces the same result asb.intersection(a)
- associative:
a.intersection(b).intersection(c)
produces the same result asa.intersection(b.intersection(c))
- idempotent:
a.intersection(a)
equalsa
- identity (
Ranges.all()
):a.intersection(Ranges.all())
equalsa
- Throws:
IllegalArgumentException
- if no range exists that is enclosed by both these ranges
- commutative:
isConnected
public boolean isConnected(Range<C> other)
- Returns
true
if there exists a (possibly empty) range which is enclosed by both this range andother
.For example,
[2, 4)
and[5, 7)
are not connected[2, 4)
and[3, 5)
are connected, because both enclose[3, 4)
[2, 4)
and[4, 6)
are connected, because both enclose the empty range[4, 4)
Note that this range and
other
have a well-defined union and intersection (as a single, possibly-empty range) if and only if this method returnstrue
.The connectedness relation has the following properties:
- symmetric:
a.isConnected(b)
produces the same result asb.isConnected(a)
- reflexive:
a.isConnected(a)
returnstrue
span
public Range<C> span(Range<C> other)
- Returns the minimal range that encloses both this
range and
other
. For example, the span of[1..3]
and(5..7)
is[1..7)
. Note that the span may contain values that are not contained by either original range.The span operation has the following properties:
- closed: the range
a.span(b)
exists for all rangesa
andb
- commutative:
a.span(b)
equalsb.span(a)
- associative:
a.span(b).span(c)
equalsa.span(b.span(c))
- idempotent:
a.span(a)
equalsa
Note that the returned range is also called the union of this range and
other
if and only if the ranges are connected. - closed: the range
asSet
@GwtCompatible(serializable=false) public ContiguousSet<C> asSet(DiscreteDomain<C> domain)
- Returns an
ImmutableSortedSet
containing the same values in the given domain contained by this range.Note:
a.asSet().equals(b.asSet())
does not implya.equals(b)
! For example,a
andb
could be[2..4]
and(1..5)
, or the empty ranges[3..3)
and[4..4)
.Warning: Be extremely careful what you do with the
asSet
view of a large range (such asRanges.greaterThan(0)
). Certain operations on such a set can be performed efficiently, but others (such asSet.hashCode()
orCollections.frequency(java.util.Collection>, java.lang.Object)
) can cause major performance problems.The returned set's
Object.toString()
method returns a short-hand form of set's contents such as"[1..100]
"}.- Throws:
IllegalArgumentException
- if neither this range nor the domain has a lower bound, or if neither has an upper bound
canonical
public Range<C> canonical(DiscreteDomain<C> domain)
- Returns the canonical form of this range in the given domain. The canonical
form has the following properties:
- equivalence:
a.canonical().contains(v) == a.contains(v)
for allv
(in other words,a.canonical(domain).asSet(domain).equals(a.asSet(domain))
- uniqueness: unless
a.isEmpty()
,a.asSet(domain).equals(b.asSet(domain))
impliesa.canonical(domain).equals(b.canonical(domain))
- idempotence:
a.canonical(domain).canonical(domain).equals(a.canonical(domain))
- [start..end)
- [start..+∞)
- (-∞..end) (only if type
C
is unbounded below) - (-∞..+∞) (only if type
C
is unbounded below)
- equivalence:
equals
public boolean equals(@Nullable Object object)
- Returns
true
ifobject
is a range having the same endpoints and bound types as this range. Note that discrete ranges such as(1..4)
and[2..3]
are not equal to one another, despite the fact that they each contain precisely the same set of values. Similarly, empty ranges are not equal unless they have exactly the same representation, so[3..3)
,(3..3]
,(4..4]
are all unequal.
hashCode
public int hashCode()
toString
public String toString()
- Returns a string representation of this range, such as
"[3..5)"
(other examples are listed in the class documentation).
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2010-2012. All Rights Reserved.