Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2010 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
   * 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.
 import static;
 import static;
A sorted set of contiguous values in a given

Warning: Be extremely careful what you do with conceptually large instances (such as ContiguousSet.create(Range.greaterThan(0), DiscreteDomain.integers()). Certain operations on such a set can be performed efficiently, but others (such as Set or Collections) can cause major performance problems.

Gregory Kick
 @GwtCompatible(emulated = true)
 @SuppressWarnings("rawtypes"// allow ungenerified Comparable types
 public abstract class ContiguousSet<C extends Comparableextends ImmutableSortedSet<C> {
Returns a ContiguousSet containing the same values in the given domain contained by the range.

java.lang.IllegalArgumentException if neither range nor the domain has a lower bound, or if neither has an upper bound
   public static <C extends ComparableContiguousSet<C> create(
       Range<C> rangeDiscreteDomain<C> domain) {
     Range<C> effectiveRange = range;
     try {
       if (!range.hasLowerBound()) {
         effectiveRange = effectiveRange.intersection(Range.atLeast(domain.minValue()));
       if (!range.hasUpperBound()) {
         effectiveRange = effectiveRange.intersection(Range.atMost(domain.maxValue()));
     } catch (NoSuchElementException e) {
       throw new IllegalArgumentException(e);
     // Per class spec, we are allowed to throw CCE if necessary
     boolean empty = effectiveRange.isEmpty()
         || Range.compareOrThrow(
             range.upperBound.greatestValueBelow(domain)) > 0;
     return empty
         ? new EmptyContiguousSet<C>(domain)
         : new RegularContiguousSet<C>(effectiveRangedomain);
   final DiscreteDomain<C> domain;
   ContiguousSet(DiscreteDomain<C> domain) {
     this. = domain;
   @Override public ContiguousSet<C> headSet(C toElement) {
     return headSetImpl(checkNotNull(toElement), false);
   @Override public ContiguousSet<C> subSet(C fromElement, C toElement) {
     checkArgument(comparator().compare(fromElementtoElement) <= 0);
     return subSetImpl(fromElementtruetoElementfalse);
   @Override public ContiguousSet<C> tailSet(C fromElement) {
     return tailSetImpl(checkNotNull(fromElement), true);
    * These methods perform most headSet, subSet, and tailSet logic, besides parameter validation.
  /*@Override*/ abstract ContiguousSet<C> headSetImpl(C toElementboolean inclusive);
  /*@Override*/ abstract ContiguousSet<C> subSetImpl(C fromElementboolean fromInclusive,
      C toElementboolean toInclusive);
  /*@Override*/ abstract ContiguousSet<C> tailSetImpl(C fromElementboolean inclusive);

Returns the set of values that are contained in both this set and the other.

This method should always be used instead of Sets.intersection(java.util.Set,java.util.Set) for ContiguousSet instances.

  public abstract ContiguousSet<C> intersection(ContiguousSet<C> other);

Returns a range, closed on both ends, whose endpoints are the minimum and maximum values contained in this set. This is equivalent to range(CLOSED, CLOSED).

java.util.NoSuchElementException if this set is empty
  public abstract Range<C> range();

Returns the minimal range with the given boundary types for which all values in this set are contained within the range.

Note that this method will return ranges with unbounded endpoints if is requested for a domain minimum or maximum. For example, if set was created from the range [1..Integer.MAX_VALUE] then set.range(CLOSED, OPEN) must return [1..∞).

java.util.NoSuchElementException if this set is empty
  public abstract Range<C> range(BoundType lowerBoundTypeBoundType upperBoundType);

Returns a short-hand representation of the contents such as "[1..100]".
  @Override public String toString() {
    return range().toString();

Not supported. ContiguousSet instances are constructed with ImmutableSet.create(java.lang.Object[]). This method exists only to hide ImmutableSet.builder() from consumers of ContiguousSet.

  @Deprecated public static <E> ImmutableSortedSet.Builder<E> builder() {
    throw new UnsupportedOperationException();
New to GrepCode? Check out our FAQ X