Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 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;
 
 import java.util.Set;
 
 import  javax.annotation.Nullable;

A set of values of type C made up of zero or more disjoint ranges.

It is guaranteed that connected ranges will be coalesced together, and that empty ranges will never be held in a RangeSet.

For a Set whose contents are specified by a Range, see ContiguousSet.

Author(s):
Kevin Bourrillion
Louis Wasserman
  abstract class RangeSet<C extends Comparable> {
   RangeSet() {}

  
Determines whether any of this range set's member ranges contains value.
 
   public boolean contains(C value) {
     return rangeContaining(value) != null;
   }

  
Returns the unique range from this range set that contains value, or null if this range set does not contain value.
 
   public Range<C> rangeContaining(C value) {
     checkNotNull(value);
     for (Range<C> range : asRanges()) {
       if (range.contains(value)) {
         return range;
       }
     }
     return null;
   }

  
Returns a view of the disconnected ranges that make up this range set. The returned set may be empty. The iterators returned by its Iterable.iterator method return the ranges in increasing order of lower bound (equivalently, of upper bound).
 
   public abstract Set<Range<C>> asRanges();

  
Returns true if this range set contains no ranges.
 
   public boolean isEmpty() {
     return asRanges().isEmpty();
   }

  
Returns a view of the complement of this RangeSet.

The returned view supports the add operation if this RangeSet supports remove, and vice versa.

 
   public abstract RangeSet<C> complement();

  
A basic, simple implementation of complement. This is not efficient on all methods; for example, rangeContaining and encloses are linear-time.
 
   static class StandardComplement<C extends Comparableextends RangeSet<C> {
     final RangeSet<C> positive;
 
     public StandardComplement(RangeSet<C> positive) {
       this. = positive;
     }
 
     @Override
     public boolean contains(C value) {
       return !.contains(value);
     }
 
    @Override
    public void add(Range<C> range) {
      .remove(range);
    }
    @Override
    public void remove(Range<C> range) {
      .add(range);
    }
    private transient Set<Range<C>> asRanges;
    @Override
    public final Set<Range<C>> asRanges() {
      Set<Range<C>> result = ;
      return (result == null) ?  = createAsRanges() : result;
    }
    Set<Range<C>> createAsRanges() {
      return new AbstractSet<Range<C>>() {
        @Override
        public Iterator<Range<C>> iterator() {
          final Iterator<Range<C>> positiveIterator = .asRanges().iterator();
          return new AbstractIterator<Range<C>>() {
            Cut<C> prevCut = Cut.belowAll();
            @Override
            protected Range<C> computeNext() {
              while (positiveIterator.hasNext()) {
                Cut<C> oldCut = ;
                Range<C> positiveRange = positiveIterator.next();
                 = positiveRange.upperBound;
                if (oldCut.compareTo(positiveRange.lowerBound) < 0) {
                  return new Range<C>(oldCutpositiveRange.lowerBound);
                }
              }
              Cut<C> posInfinity = Cut.aboveAll();
              if (.compareTo(posInfinity) < 0) {
                Range<C> result = new Range<C>(posInfinity);
                 = posInfinity;
                return result;
              }
              return endOfData();
            }
          };
        }
        @Override
        public int size() {
          return Iterators.size(iterator());
        }
      };
    }
    @Override
    public RangeSet<C> complement() {
      return ;
    }
  }

  
Adds the specified range to this RangeSet (optional operation). That is, for equal range sets a and b, the result of a.add(range) is that a will be the minimal range set for which both a.enclosesAll(b) and a.encloses(range).

Note that range will be coalesced with any ranges in the range set that are connected with it. Moreover, if range is empty, this is a no-op.

Throws:
UnsupportedOperationException if this range set does not support the add operation
  public void add(Range<C> range) {
    throw new UnsupportedOperationException();
  }

  
Removes the specified range from this RangeSet (optional operation). After this operation, if range.contains(c), this.contains(c) will return false.

If range is empty, this is a no-op.

Throws:
UnsupportedOperationException if this range set does not support the remove operation
  public void remove(Range<C> range) {
    throw new UnsupportedOperationException();
  }

  
Returns true if there exists a member range in this range set which encloses the specified range.
  public boolean encloses(Range<C> otherRange) {
    for (Range<C> range : asRanges()) {
      if (range.encloses(otherRange)) {
        return true;
      }
    }
    return false;
  }

  
Returns true if for each member range in other there exists a member range in this range set which encloses it. It follows that this.contains(value) whenever other.contains(value). Returns true if other is empty.

This is equivalent to checking if this range set encloses each of the ranges in other.

  public boolean enclosesAll(RangeSet<C> other) {
    for (Range<C> range : other.asRanges()) {
      if (!encloses(range)) {
        return false;
      }
    }
    return true;
  }

  
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 and other.

This is equivalent to calling add on each of the ranges in other in turn.

Throws:
UnsupportedOperationException if this range set does not support the addAll operation
  public void addAll(RangeSet<C> other) {
    for (Range<C> range : other.asRanges()) {
      this.add(range);
    }
  }

  
Removes all of the ranges from the specified range set from this range set (optional operation). After this operation, if other.contains(c), this.contains(c) will return false.

This is equivalent to calling remove on each of the ranges in other in turn.

Throws:
UnsupportedOperationException if this range set does not support the removeAll operation
  public void removeAll(RangeSet<C> other) {
    for (Range<C> range : other.asRanges()) {
      this.remove(range);
    }
  }

  
Returns true if obj is another RangeSet that contains the same ranges according to Range.equals(Object).
  public boolean equals(@Nullable Object obj) {
    if (obj instanceof RangeSet) {
      RangeSet<?> other = (RangeSet<?>) obj;
      return this.asRanges().equals(other.asRanges());
    }
    return false;
  }
  public final int hashCode() {
    return asRanges().hashCode();
  }

  
Returns a readable string representation of this range set. For example, if this RangeSet consisted of Ranges.closed(1, 3) and Ranges.greaterThan(4), this might return " [1‥3](4‥+∞)"}.
  public final String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append('{');
    for (Range<C> range : asRanges()) {
      builder.append(range);
    }
    builder.append('}');
    return builder.toString();
  }
New to GrepCode? Check out our FAQ X