Interface RangeSet<C extends Comparable>
- All Known Implementing Classes:
ImmutableRangeSet
,TreeRangeSet
C
.
Implementations that choose to support the add(Range)
operation are required to
ignore empty ranges and coalesce connected ranges. For example:
RangeSet<Integer> rangeSet = TreeRangeSet.create();
rangeSet.add(Range.closed(1, 10)); // {[1, 10]}
rangeSet.add(Range.closedOpen(11, 15)); // disconnected range; {[1, 10], [11, 15)}
rangeSet.add(Range.closedOpen(15, 20)); // connected range; {[1, 10], [11, 20)}
rangeSet.add(Range.openClosed(0, 0)); // empty range; {[1, 10], [11, 20)}
rangeSet.remove(Range.open(5, 10)); // splits [1, 10]; {[1, 5], [10, 10], [11, 20)}
Note that the behavior of Range.isEmpty()
and Range.isConnected(Range)
may not
be as expected on discrete ranges. See the Javadoc of those methods for details.
For a Set
whose contents are specified by a Range
, see ContiguousSet
.
See the Guava User Guide article on RangeSets.
- Since:
- 14.0
- Author:
- Kevin Bourrillion, Louis Wasserman
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the specified range to thisRangeSet
(optional operation).void
Adds all of the ranges from the specified range set to this range set (optional operation).default void
Adds all of the specified ranges to this range set (optional operation).Returns a descending view of the disconnected ranges that make up this range set.asRanges()
Returns a view of the disconnected ranges that make up this range set.void
clear()
Removes all ranges from thisRangeSet
(optional operation).Returns a view of the complement of thisRangeSet
.boolean
Determines whether any of this range set's member ranges containsvalue
.boolean
Returnstrue
if there exists a member range in this range set which encloses the specified range.boolean
enclosesAll
(RangeSet<C> other) Returnstrue
if for each member range inother
there exists a member range in this range set which encloses it.default boolean
enclosesAll
(Iterable<Range<C>> other) Returnstrue
if for each range inother
there exists a member range in this range set which encloses it.boolean
Returnstrue
ifobj
is anotherRangeSet
that contains the same ranges according toRange.equals(Object)
.int
hashCode()
ReturnsasRanges().hashCode()
.boolean
intersects
(Range<C> otherRange) Returnstrue
if there exists a non-empty range enclosed by both a member range in this range set and the specified range.boolean
isEmpty()
Returnstrue
if this range set contains no ranges.rangeContaining
(C value) Returns the unique range from this range set that containsvalue
, ornull
if this range set does not containvalue
.void
Removes the specified range from thisRangeSet
(optional operation).void
Removes all of the ranges from the specified range set from this range set (optional operation).default void
Removes all of the specified ranges from this range set (optional operation).span()
Returns the minimal range which encloses all ranges in this range set.subRangeSet
(Range<C> view) Returns a view of the intersection of thisRangeSet
with the specified range.toString()
Returns a readable string representation of this range set.
-
Method Details
-
contains
-
rangeContaining
-
intersects
Returnstrue
if there exists a non-empty range enclosed by both a member range in this range set and the specified range. This is equivalent to callingsubRangeSet(otherRange)
and testing whether the resulting range set is non-empty.- Since:
- 20.0
-
encloses
-
enclosesAll
Returnstrue
if for each member range inother
there exists a member range in this range set which encloses it. It follows thatthis.contains(value)
wheneverother.contains(value)
. Returnstrue
ifother
is empty.This is equivalent to checking if this range set
encloses(com.google.common.collect.Range<C>)
each of the ranges inother
. -
enclosesAll
Returnstrue
if for each range inother
there exists a member range in this range set which encloses it. Returnstrue
ifother
is empty.This is equivalent to checking if this range set
encloses(com.google.common.collect.Range<C>)
each range inother
.- Since:
- 21.0
-
isEmpty
boolean isEmpty()Returnstrue
if this range set contains no ranges. -
span
-
asRanges
Returns a view of the disconnected ranges that make up this range set. The returned set may be empty. The iterators returned by itsIterable.iterator()
method return the ranges in increasing order of lower bound (equivalently, of upper bound). -
asDescendingSetOfRanges
Returns a descending view of the disconnected ranges that make up this range set. The returned set may be empty. The iterators returned by itsIterable.iterator()
method return the ranges in decreasing order of lower bound (equivalently, of upper bound).- Since:
- 19.0
-
complement
RangeSet<C> complement()Returns a view of the complement of thisRangeSet
.The returned view supports the
add(com.google.common.collect.Range<C>)
operation if thisRangeSet
supportsremove(com.google.common.collect.Range<C>)
, and vice versa. -
subRangeSet
Returns a view of the intersection of thisRangeSet
with the specified range.The returned view supports all optional operations supported by this
RangeSet
, with the caveat that anIllegalArgumentException
is thrown on an attempt to add any range not enclosed byview
. -
add
Adds the specified range to thisRangeSet
(optional operation). That is, for equal range sets a and b, the result ofa.add(range)
is thata
will be the minimal range set for which botha.enclosesAll(b)
anda.encloses(range)
.Note that
range
will be coalesced with any ranges in the range set that are connected with it. Moreover, ifrange
is empty, this is a no-op.- Throws:
UnsupportedOperationException
- if this range set does not support theadd
operation
-
remove
Removes the specified range from thisRangeSet
(optional operation). After this operation, ifrange.contains(c)
,this.contains(c)
will returnfalse
.If
range
is empty, this is a no-op.- Throws:
UnsupportedOperationException
- if this range set does not support theremove
operation
-
clear
void clear()Removes all ranges from thisRangeSet
(optional operation). After this operation,this.contains(c)
will return false for allc
.This is equivalent to
remove(Range.all())
.- Throws:
UnsupportedOperationException
- if this range set does not support theclear
operation
-
addAll
Adds all of the ranges from the specified range set to this range set (optional operation). After this operation, this range set is the minimal range set that encloses both the original range set andother
.This is equivalent to calling
add(com.google.common.collect.Range<C>)
on each of the ranges inother
in turn.- Throws:
UnsupportedOperationException
- if this range set does not support theaddAll
operation
-
addAll
Adds all of the specified ranges to this range set (optional operation). After this operation, this range set is the minimal range set that encloses both the original range set and each range inother
.This is equivalent to calling
add(com.google.common.collect.Range<C>)
on each of the ranges inother
in turn.- Throws:
UnsupportedOperationException
- if this range set does not support theaddAll
operation- Since:
- 21.0
-
removeAll
Removes all of the ranges from the specified range set from this range set (optional operation). After this operation, ifother.contains(c)
,this.contains(c)
will returnfalse
.This is equivalent to calling
remove(com.google.common.collect.Range<C>)
on each of the ranges inother
in turn.- Throws:
UnsupportedOperationException
- if this range set does not support theremoveAll
operation
-
removeAll
Removes all of the specified ranges from this range set (optional operation).This is equivalent to calling
remove(com.google.common.collect.Range<C>)
on each of the ranges inother
in turn.- Throws:
UnsupportedOperationException
- if this range set does not support theremoveAll
operation- Since:
- 21.0
-
equals
-
hashCode
-
toString
-