com.google.common.collect
Class Ranges

java.lang.Object
  extended by com.google.common.collect.Ranges

@GwtCompatible
@Beta
public final class Ranges
extends Object

Static methods pertaining to Range instances. Each of the nine types of ranges can be constructed with a corresponding factory method:

(a..b)
open(C, C)
[a..b]
closed(C, C)
[a..b)
closedOpen(C, C)
(a..b]
openClosed(C, C)
(a..+∞)
greaterThan(C)
[a..+∞)
atLeast(C)
(-∞..b)
lessThan(C)
(-∞..b]
atMost(C)
(-∞..+∞)
all()

Additionally, Range instances can be constructed by passing the bound types explicitly.

Bounded on both ends
range(C, com.google.common.collect.BoundType, C, com.google.common.collect.BoundType)
Unbounded on top ((a..+∞) or (a..+∞))
downTo(C, com.google.common.collect.BoundType)
Unbounded on bottom ((-∞..b) or (-∞..b])
upTo(C, com.google.common.collect.BoundType)

Since:
10.0
Author:
Kevin Bourrillion, Gregory Kick

Method Summary
static
<C extends Comparable<?>>
Range<C>
all()
          Returns a range that contains every value of type C.
static
<C extends Comparable<?>>
Range<C>
atLeast(C endpoint)
          Returns a range that contains all values greater than or equal to endpoint.
static
<C extends Comparable<?>>
Range<C>
atMost(C endpoint)
          Returns a range that contains all values less than or equal to endpoint.
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.
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.
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.
static
<C extends Comparable<?>>
Range<C>
encloseAll(Iterable<C> values)
          Returns the minimal range that contains all of the given values.
static
<C extends Comparable<?>>
Range<C>
greaterThan(C endpoint)
          Returns a range that contains all values strictly greater than endpoint.
static
<C extends Comparable<?>>
Range<C>
lessThan(C endpoint)
          Returns a range that contains all values strictly less than endpoint.
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.
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.
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).
static
<C extends Comparable<?>>
Range<C>
singleton(C value)
          Returns a range that contains only the given value.
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).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

open

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.

Throws:
IllegalArgumentException - if lower is greater than or equal to upper

closed

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.

Throws:
IllegalArgumentException - if lower is greater than upper

closedOpen

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.

Throws:
IllegalArgumentException - if lower is greater than upper

openClosed

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.

Throws:
IllegalArgumentException - if lower is greater than upper

range

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).

Throws:
IllegalArgumentException - if lower is greater than upper

lessThan

public static <C extends Comparable<?>> Range<C> lessThan(C endpoint)
Returns a range that contains all values strictly less than endpoint.


atMost

public static <C extends Comparable<?>> Range<C> atMost(C endpoint)
Returns a range that contains all values less than or equal to endpoint.


upTo

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).


greaterThan

public static <C extends Comparable<?>> Range<C> greaterThan(C endpoint)
Returns a range that contains all values strictly greater than endpoint.


atLeast

public static <C extends Comparable<?>> Range<C> atLeast(C endpoint)
Returns a range that contains all values greater than or equal to endpoint.


downTo

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.


all

public static <C extends Comparable<?>> Range<C> all()
Returns a range that contains every value of type C.


singleton

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.


encloseAll

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.

Throws:
ClassCastException - if the parameters are not mutually comparable
NoSuchElementException - if values is empty
NullPointerException - if any of values is null


Copyright © 2010-2012. All Rights Reserved.