Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
Static methods pertaining to Range instances. Each of the nine types of ranges can be constructed with a corresponding factory method:
(a..b)
open(java.lang.Comparable,java.lang.Comparable)
[a..b]
closed(java.lang.Comparable,java.lang.Comparable)
[a..b)
closedOpen(java.lang.Comparable,java.lang.Comparable)
(a..b]
openClosed(java.lang.Comparable,java.lang.Comparable)
(a..+∞)
greaterThan(java.lang.Comparable)
[a..+∞)
atLeast(java.lang.Comparable)
(-∞..b)
lessThan(java.lang.Comparable)
(-∞..b]
atMost(java.lang.Comparable)
(-∞..+∞)
all()

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

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

See the Guava User Guide article on Range.

Author(s):
Kevin Bourrillion
Gregory Kick
Since:
10.0
 
 public final class Ranges {
   private Ranges() {}
 
   static <C extends Comparable<?>> Range<C> create(
       Cut<C> lowerBoundCut<C> upperBound) {
     return new Range<C>(lowerBoundupperBound);
   }

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

Throws:
java.lang.IllegalArgumentException if lower is greater than or equal to upper
 
   public static <C extends Comparable<?>> Range<C> open(C lower, C upper) {
     return create(Cut.aboveValue(lower), Cut.belowValue(upper));
   }

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

Throws:
java.lang.IllegalArgumentException if lower is greater than upper
  public static <C extends Comparable<?>> Range<C> closed(C lower, C upper) {
    return create(Cut.belowValue(lower), Cut.aboveValue(upper));
  }

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

Throws:
java.lang.IllegalArgumentException if lower is greater than upper
  public static <C extends Comparable<?>> Range<C> closedOpen(
      C lower, C upper) {
    return create(Cut.belowValue(lower), Cut.belowValue(upper));
  }

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

Throws:
java.lang.IllegalArgumentException if lower is greater than upper
  public static <C extends Comparable<?>> Range<C> openClosed(
      C lower, C upper) {
    return create(Cut.aboveValue(lower), Cut.aboveValue(upper));
  }

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

Throws:
java.lang.IllegalArgumentException if lower is greater than upper
  public static <C extends Comparable<?>> Range<C> range(
      C lowerBoundType lowerType, C upperBoundType upperType) {
    checkNotNull(lowerType);
    checkNotNull(upperType);
    Cut<C> lowerBound = (lowerType == .)
        ? Cut.aboveValue(lower)
        : Cut.belowValue(lower);
    Cut<C> upperBound = (upperType == .)
        ? Cut.belowValue(upper)
        : Cut.aboveValue(upper);
    return create(lowerBoundupperBound);
  }

  
Returns a range that contains all values strictly less than endpoint.
  public static <C extends Comparable<?>> Range<C> lessThan(C endpoint) {
    return create(Cut.<C>belowAll(), Cut.belowValue(endpoint));
  }

  
Returns a range that contains all values less than or equal to endpoint.
  public static <C extends Comparable<?>> Range<C> atMost(C endpoint) {
    return create(Cut.<C>belowAll(), Cut.aboveValue(endpoint));
  }

  
Returns a range with no lower bound up to the given endpoint, which may be either inclusive (closed) or exclusive (open).
  public static <C extends Comparable<?>> Range<C> upTo(
      C endpointBoundType boundType) {
    switch (boundType) {
      case :
        return lessThan(endpoint);
      case :
        return atMost(endpoint);
      default:
        throw new AssertionError();
    }
  }

  
Returns a range that contains all values strictly greater than endpoint.
  public static <C extends Comparable<?>> Range<C> greaterThan(C endpoint) {
    return create(Cut.aboveValue(endpoint), Cut.<C>aboveAll());
  }

  
Returns a range that contains all values greater than or equal to endpoint.
  public static <C extends Comparable<?>> Range<C> atLeast(C endpoint) {
    return create(Cut.belowValue(endpoint), Cut.<C>aboveAll());
  }

  
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> downTo(
      C endpointBoundType boundType) {
    switch (boundType) {
      case :
        return greaterThan(endpoint);
      case :
        return atLeast(endpoint);
      default:
        throw new AssertionError();
    }
  }

  
Returns a range that contains every value of type C.
  public static <C extends Comparable<?>> Range<C> all() {
    return create(Cut.<C>belowAll(), Cut.<C>aboveAll());
  }

  
Returns a range that contains only the given value. The returned range is closed on both ends.
  public static <C extends Comparable<?>> Range<C> singleton(C value) {
    return closed(valuevalue);
  }

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

Throws:
java.lang.ClassCastException if the parameters are not mutually comparable
java.util.NoSuchElementException if values is empty
java.lang.NullPointerException if any of values is null
  public static <C extends Comparable<?>> Range<C> encloseAll(
      Iterable<C> values) {
    checkNotNull(values);
    if (values instanceof ContiguousSet) {
      return ((ContiguousSet<C>) values).range();
    }
    Iterator<C> valueIterator = values.iterator();
    C min = checkNotNull(valueIterator.next());
    C max = min;
    while (valueIterator.hasNext()) {
      C value = checkNotNull(valueIterator.next());
      min = Ordering.natural().min(minvalue);
      max = Ordering.natural().max(maxvalue);
    }
    return closed(minmax);
  }
New to GrepCode? Check out our FAQ X