Top Description Inners Fields Constructors Methods
com.google.common.collect

public final Class Range<C extends java.lang.Comparable<T>>

extends RangeGwtSerializationDependencies
implements Predicate<C extends Comparable<T>>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, com.google.common.base.Predicate, java.util.function.Predicate
Annotations
@GwtCompatible
@SuppressWarnings:rawtypes
@Immutable
containerOf:C
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull

A range (or "interval") defines the boundaries around a contiguous span of values of some Comparable type; for example, "integers from 1 to 100 inclusive." Note that it is not possible to iterate over these contained values. To do so, pass this range instance and an appropriate DiscreteDomain to ContiguousSet#create.

Types of ranges

Each end of the range may be bounded or unbounded. If bounded, there is an associated endpoint value, and the range is considered to be either open (does not include the endpoint) or closed (includes the endpoint) on that side. With three possibilities on each side, this yields nine basic types of ranges, enumerated below. (Notation: a square bracket ([ ]) indicates that the range is closed on that side; a parenthesis (( )) means it is either open or unbounded. The construct {x | statement} is read "the set of all x such that statement.")

Range Types
Notation Definition Factory method
(a..b) {x | a < x < b} open
[a..b] {x | a <= x <= b}closed
(a..b] {x | a < x <= b} openClosed
[a..b) {x | a <= x < b} closedOpen
(a..+∞) {x | x > a} greaterThan
[a..+∞) {x | x >= a} atLeast
(-∞..b) {x | x < b} lessThan
(-∞..b] {x | x <= b} atMost
(-∞..+∞){x} all

When both endpoints exist, the upper endpoint may not be less than the lower. The endpoints may be equal only if at least one of the bounds is closed:

Warnings

Other notes

Further reading

See the Guava User Guide article on Range.

Authors
Kevin Bourrillion, Gregory Kick
Since
10.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
private static class
Range.RangeLexOrdering

Needed to serialize sorted collections of Ranges.

pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final Range<Comparable<T>>
pack-priv final Cut<C>
private static final long
pack-priv final Cut<C>

Constructor Summary

AccessConstructor and Description
private
Range(Cut<C> lowerBound, Cut<C> upperBound)

Method Summary

Modifier and TypeMethod and Description
public static <C extends Comparable<?>> Range<C>
all()

Returns a range that contains every value of type C.

public boolean
apply(C input)

Implements com.google.common.base.Predicate.apply.

Deprecated Provided only to satisfy the Predicate interface; use contains instead.
Returns the result of applying this predicate to input (Java 8 users, see notes in the class documentation above).
public static <C extends Comparable<?>> Range<C>
atLeast(C endpoint)

Returns a range that contains all values greater than or equal to endpoint.

public static <C extends Comparable<?>> Range<C>
atMost(C endpoint)

Returns a range that contains all values less than or equal to endpoint.

public Range<C>
canonical(DiscreteDomain<C> domain)

Returns the canonical form of this range in the given domain.

public static <C extends Comparable<?>> Range<C>
closed(C lower, C upper)

Returns a range that contains all values greater than or equal to lower and less than or equal to upper.

public static <C extends Comparable<?>> Range<C>
closedOpen(C lower, C upper)

Returns a range that contains all values greater than or equal to lower and strictly less than upper.

pack-priv static int
public boolean
contains(C value)

Returns true if value is within the bounds of this range.

public boolean
containsAll(Iterable<? extends C> values)

Returns true if every element in values is contained in this range.

pack-priv static <C extends Comparable<?>> Range<C>
create(Cut<C> lowerBound, Cut<C> upperBound)

public static <C extends Comparable<?>> Range<C>
downTo(C endpoint, BoundType boundType)

Returns a range from the given endpoint, which may be either inclusive (closed) or exclusive (open), with no upper bound.

public static <C extends Comparable<?>> Range<C>
encloseAll(Iterable<C> values)

Returns the minimal range that contains all of the given values.

public boolean
encloses(Range<C> other)

Returns true if the bounds of other do not extend outside the bounds of this range.

public boolean
equals(Object
the reference object with which to compare.
object
)

Overrides java.lang.Object.equals.

Implements com.google.common.base.Predicate.equals.

Returns true if object is a range having the same endpoints and bound types as this range.
public Range<C>
gap(Range<C> otherRange)

Returns the maximal range lying between this range and otherRange, if such a range exists.

public static <C extends Comparable<?>> Range<C>
greaterThan(C endpoint)

Returns a range that contains all values strictly greater than endpoint.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code for this range.
public boolean
hasLowerBound()

Returns true if this range has a lower endpoint.

public boolean
hasUpperBound()

Returns true if this range has an upper endpoint.

public Range<C>
intersection(Range<C> connectedRange)

Returns the maximal range enclosed by both this range and connectedRange, if such a range exists.

public boolean
isConnected(Range<C> other)

Returns true if there exists a (possibly empty) range which is enclosed by both this range and other.

public boolean
isEmpty()

Returns true if this range is of the form [v..v) or (v..v].

public static <C extends Comparable<?>> Range<C>
lessThan(C endpoint)

Returns a range that contains all values strictly less than endpoint.

pack-priv static <C extends Comparable<?>> Function<Range<C>, Cut<C>>
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.

public C
lowerEndpoint()

Returns the lower endpoint of this range.

public static <C extends Comparable<?>> Range<C>
open(C lower, C upper)

Returns a range that contains all values strictly greater than lower and strictly less than upper.

public static <C extends Comparable<?>> Range<C>
openClosed(C lower, C upper)

Returns a range that contains all values strictly greater than lower and less than or equal to upper.

public static <C extends Comparable<?>> Range<C>
range(C lower, BoundType lowerType, C upper, BoundType upperType)

Returns a range that contains any value from lower to upper, where each endpoint may be either inclusive (closed) or exclusive (open).

pack-priv static <C extends Comparable<?>> Ordering<Range<C>>
pack-priv Object
public static <C extends Comparable<?>> Range<C>
singleton(C value)

Returns a range that contains only the given value.

public Range<C>
span(Range<C> other)

Returns the minimal range that encloses both this range and other.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of this range, such as "[3..5)" (other examples are listed in the class documentation).
private static String
toString(Cut<?> lowerBound, Cut<?> upperBound)

pack-priv static <C extends Comparable<?>> Function<Range<C>, Cut<C>>
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.

public C
upperEndpoint()

Returns the upper endpoint of this range.

public static <C extends Comparable<?>> Range<C>
upTo(C endpoint, BoundType boundType)

Returns a range with no lower bound up to the given endpoint, which may be either inclusive (closed) or exclusive (open).

Field Detail

ALLback to summary
private static final Range<Comparable<T>> ALL
lowerBoundback to summary
pack-priv final Cut<C> lowerBound
serialVersionUIDback to summary
private static final long serialVersionUID
upperBoundback to summary
pack-priv final Cut<C> upperBound

Constructor Detail

Rangeback to summary
private Range(Cut<C> lowerBound, Cut<C> upperBound)

Method Detail

allback to summary
public static <C extends Comparable<?>> Range<C> all()

Returns a range that contains every value of type C.

Annotations
@SuppressWarnings:unchecked
Since
14.0
applyback to summary
public boolean apply(C input)

Implements com.google.common.base.Predicate.apply.

Doc from com.google.common.base.Predicate.apply.

Deprecated

Provided only to satisfy the Predicate interface; use contains instead.

Returns the result of applying this predicate to input (Java 8 users, see notes in the class documentation above). This method is generally expected, but not absolutely required, to have the following properties:

  • Its execution does not cause any observable side effects.
  • The computation is consistent with equals; that is, Objects.equal(a, b) implies that predicate.apply(a) == predicate.apply(b)).
Annotations
@Deprecated
@Override
atLeastback to summary
public static <C extends Comparable<?>> Range<C> atLeast(C endpoint)

Returns a range that contains all values greater than or equal to endpoint.

Since
14.0
atMostback to summary
public static <C extends Comparable<?>> Range<C> atMost(C endpoint)

Returns a range that contains all values less than or equal to endpoint.

Since
14.0
canonicalback to summary
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 all v (in other words, ContiguousSet.create(a.canonical(domain), domain).equals( ContiguousSet.create(a, domain))
  • uniqueness: unless a.isEmpty(), ContiguousSet.create(a, domain).equals(ContiguousSet.create(b, domain)) implies a.canonical(domain).equals(b.canonical(domain))
  • idempotence: a.canonical(domain).canonical(domain).equals(a.canonical(domain))

Furthermore, this method guarantees that the range returned will be one of the following canonical forms:

  • [start..end)
  • [start..+∞)
  • (-∞..end) (only if type C is unbounded below)
  • (-∞..+∞) (only if type C is unbounded below)
closedback to summary
public static <C extends Comparable<?>> Range<C> closed(C lower, C upper)

Returns a range that contains all values greater than or equal to lower and less than or equal to upper.

Exceptions
IllegalArgumentException:
if lower is greater than upper
ClassCastException:
if lower and upper are not mutually comparable
Since
14.0
closedOpenback to summary
public static <C extends Comparable<?>> Range<C> closedOpen(C lower, C upper)

Returns a range that contains all values greater than or equal to lower and strictly less than upper.

Exceptions
IllegalArgumentException:
if lower is greater than upper
ClassCastException:
if lower and upper are not mutually comparable
Since
14.0
compareOrThrowback to summary
pack-priv static int compareOrThrow(Comparable<T> left, Comparable<T> right)
Annotations
@SuppressWarnings:unchecked
containsback to summary
public boolean contains(C value)

Returns true if value is within the bounds of this range. For example, on the range [0..2), contains(1) returns true, while contains(2) returns false.

containsAllback to summary
public boolean containsAll(Iterable<? extends C> values)

Returns true if every element in values is contained in this range.

createback to summary
pack-priv static <C extends Comparable<?>> Range<C> create(Cut<C> lowerBound, Cut<C> upperBound)
downToback to summary
public static <C extends Comparable<?>> Range<C> downTo(C endpoint, BoundType boundType)

Returns a range from the given endpoint, which may be either inclusive (closed) or exclusive (open), with no upper bound.

Since
14.0
encloseAllback to summary
public static <C extends Comparable<?>> Range<C> encloseAll(Iterable<C> values)

Returns the minimal range that contains all of the given values. The returned range is closed on both ends.

Exceptions
ClassCastException:
if the values are not mutually comparable
NoSuchElementException:
if values is empty
NullPointerException:
if any of values is null
Since
14.0
enclosesback to summary
public boolean encloses(Range<C> other)

Returns true if the bounds of other 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)

Note that if a.encloses(b), then b.contains(v) implies a.contains(v), but as the last two examples illustrate, the converse is not always true.

Being reflexive, antisymmetric and transitive, the encloses relation defines a partial order over ranges. There exists a unique maximal range according to this relation, and also numerous minimal ranges. Enclosure also implies connectedness.

equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Implements com.google.common.base.Predicate.equals.

Returns true if object 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.

Parameters
object:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
gapback to summary
public Range<C> gap(Range<C> otherRange)

Returns the maximal range lying between this range and otherRange, if such a range exists. The resulting range may be empty if the two ranges are adjacent but non-overlapping.

For example, the gap of [1..5] and (7..10) is (5..7]. The resulting range may be empty; for example, the gap between [1..5) [5..7) yields the empty range [5..5).

The gap exists if and only if the two ranges are either disconnected or immediately adjacent (any intersection must be an empty range).

The gap operation is commutative.

Exceptions
IllegalArgumentException:
if this range and otherRange have a nonempty intersection
Since
27.0
greaterThanback to summary
public static <C extends Comparable<?>> Range<C> greaterThan(C endpoint)

Returns a range that contains all values strictly greater than endpoint.

Since
14.0
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code for this range.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
hasLowerBoundback to summary
public boolean hasLowerBound()

Returns true if this range has a lower endpoint.

hasUpperBoundback to summary
public boolean hasUpperBound()

Returns true if this range has an upper endpoint.

intersectionback to summary
public Range<C> intersection(Range<C> connectedRange)

Returns the maximal range enclosed by both this range and connectedRange, 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).

The intersection exists if and only if the two ranges are connected.

The intersection operation is commutative, associative and idempotent, and its identity element is Range#all).

Exceptions
IllegalArgumentException:
if isConnected(connectedRange) is false
isConnectedback to summary
public boolean isConnected(Range<C> other)

Returns true if there exists a (possibly empty) range which is enclosed by both this range and other.

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 returns true.

The connectedness relation is both reflexive and symmetric, but does not form an equivalence relation as it is not transitive.

Note that certain discrete ranges are not considered connected, even though there are no elements "between them." For example, [3, 5] is not considered connected to [6, 10]. In these cases, it may be desirable for both input ranges to be preprocessed with canonical(DiscreteDomain) before testing for connectedness.

isEmptyback to summary
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. In these cases, it may be helpful to preprocess ranges with canonical(DiscreteDomain).

lessThanback to summary
public static <C extends Comparable<?>> Range<C> lessThan(C endpoint)

Returns a range that contains all values strictly less than endpoint.

Since
14.0
lowerBoundFnback to summary
pack-priv static <C extends Comparable<?>> Function<Range<C>, Cut<C>> lowerBoundFn()
Annotations
@SuppressWarnings:unchecked
lowerBoundTypeback to summary
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.

Exceptions
IllegalStateException:
if this range is unbounded below (that is, hasLowerBound() returns false)
lowerEndpointback to summary
public C lowerEndpoint()

Returns the lower endpoint of this range.

Exceptions
IllegalStateException:
if this range is unbounded below (that is, hasLowerBound() returns false)
openback to summary
public static <C extends Comparable<?>> Range<C> open(C lower, C upper)

Returns a range that contains all values strictly greater than lower and strictly less than upper.

Exceptions
IllegalArgumentException:
if lower is greater than or equal to upper
ClassCastException:
if lower and upper are not mutually comparable
Since
14.0
openClosedback to summary
public static <C extends Comparable<?>> Range<C> openClosed(C lower, C upper)

Returns a range that contains all values strictly greater than lower and less than or equal to upper.

Exceptions
IllegalArgumentException:
if lower is greater than upper
ClassCastException:
if lower and upper are not mutually comparable
Since
14.0
rangeback to summary
public static <C extends Comparable<?>> Range<C> range(C lower, BoundType lowerType, C upper, BoundType upperType)

Returns a range that contains any value from lower to upper, where each endpoint may be either inclusive (closed) or exclusive (open).

Exceptions
IllegalArgumentException:
if lower is greater than upper
ClassCastException:
if lower and upper are not mutually comparable
Since
14.0
rangeLexOrderingback to summary
pack-priv static <C extends Comparable<?>> Ordering<Range<C>> rangeLexOrdering()
readResolveback to summary
pack-priv Object readResolve()
singletonback to summary
public static <C extends Comparable<?>> Range<C> singleton(C value)

Returns a range that contains only the given value. The returned range is closed on both ends.

Since
14.0
spanback to summary
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).

If the input ranges are connected, the returned range can also be called their union. If they are not, note that the span might contain values that are not contained in either input range.

Like intersection, this operation is commutative, associative and idempotent. Unlike it, it is always well-defined for any two input ranges.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this range, such as "[3..5)" (other examples are listed in the class documentation).

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
toStringback to summary
private static String toString(Cut<?> lowerBound, Cut<?> upperBound)
upperBoundFnback to summary
pack-priv static <C extends Comparable<?>> Function<Range<C>, Cut<C>> upperBoundFn()
Annotations
@SuppressWarnings:unchecked
upperBoundTypeback to summary
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.

Exceptions
IllegalStateException:
if this range is unbounded above (that is, hasUpperBound() returns false)
upperEndpointback to summary
public C upperEndpoint()

Returns the upper endpoint of this range.

Exceptions
IllegalStateException:
if this range is unbounded above (that is, hasUpperBound() returns false)
upToback to summary
public static <C extends Comparable<?>> Range<C> upTo(C endpoint, BoundType boundType)

Returns a range with no lower bound up to the given endpoint, which may be either inclusive (closed) or exclusive (open).

Since
14.0
com.google.common.collect back to summary

pack-priv Class Range.LowerBoundFn

extends Object
implements Function<Range<C extends Comparable<T>>, Cut<C extends Comparable<T>>>
Class Inheritance
All Implemented Interfaces
com.google.common.base.Function, java.util.function.Function

Field Summary

Modifier and TypeField and Description
pack-priv static final Range.LowerBoundFn

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Cut<C extends Comparable<T>>
apply(Range<C extends Comparable<T>>
the function argument
range
)

Implements com.google.common.base.Function.apply.

Applies this function to the given argument.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
pack-priv static final Range.LowerBoundFn INSTANCE

Constructor Detail

LowerBoundFnback to summary
pack-priv LowerBoundFn()

Method Detail

applyback to summary
public Cut<C extends Comparable<T>> apply(Range<C extends Comparable<T>> range)

Implements com.google.common.base.Function.apply.

Doc from java.util.function.Function.apply.

Applies this function to the given argument.

Parameters
range:Range<C extends Comparable<T>>

the function argument

Returns:Cut<C extends Comparable<T>>

the function result

Annotations
@Override
com.google.common.collect back to summary

private Class Range.RangeLexOrdering

extends Ordering<Range<?>>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Needed to serialize sorted collections of Ranges.

Field Summary

Modifier and TypeField and Description
pack-priv static final Ordering<Range<?>>
private static final long
Inherited from com.google.common.collect.Ordering:
LEFT_IS_GREATERRIGHT_IS_GREATER

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
compare(Range<?>
the first object to be compared.
left
,
Range<?>
the second object to be compared.
right
)

Implements abstract com.google.common.collect.Ordering.compare.

Implements java.util.Comparator.compare.

Compares its two arguments for order.
Inherited from com.google.common.collect.Ordering:
allEqualarbitrarybinarySearchcompoundcompoundexplicitexplicitfromfromgreatestOfgreatestOfimmutableSortedCopyisOrderedisStrictlyOrderedleastOfleastOflexicographicalmaxmaxmaxmaxminminminminnaturalnullsFirstnullsLastonKeysonResultOfreversesortedCopyusingToString

Field Detail

INSTANCEback to summary
pack-priv static final Ordering<Range<?>> INSTANCE
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

RangeLexOrderingback to summary
private RangeLexOrdering()

Method Detail

compareback to summary
public int compare(Range<?> left, Range<?> right)

Implements abstract com.google.common.collect.Ordering.compare.

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
left:Range<?>

the first object to be compared.

right:Range<?>

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
com.google.common.collect back to summary

pack-priv Class Range.UpperBoundFn

extends Object
implements Function<Range<C extends Comparable<T>>, Cut<C extends Comparable<T>>>
Class Inheritance
All Implemented Interfaces
com.google.common.base.Function, java.util.function.Function

Field Summary

Modifier and TypeField and Description
pack-priv static final Range.UpperBoundFn

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Cut<C extends Comparable<T>>
apply(Range<C extends Comparable<T>>
the function argument
range
)

Implements com.google.common.base.Function.apply.

Applies this function to the given argument.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
pack-priv static final Range.UpperBoundFn INSTANCE

Constructor Detail

UpperBoundFnback to summary
pack-priv UpperBoundFn()

Method Detail

applyback to summary
public Cut<C extends Comparable<T>> apply(Range<C extends Comparable<T>> range)

Implements com.google.common.base.Function.apply.

Doc from java.util.function.Function.apply.

Applies this function to the given argument.

Parameters
range:Range<C extends Comparable<T>>

the function argument

Returns:Cut<C extends Comparable<T>>

the function result

Annotations
@Override