Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 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.checkArgument;
 import static com.google.common.base.Preconditions.checkElementIndex;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.Map;
 
An immutable implementation of RangeMap, supporting all query operations efficiently.

Like all RangeMap implementations, this supports neither null keys nor null values.

Author(s):
Louis Wasserman
Since:
14.0
 
 @GwtIncompatible("NavigableMap")
 public class ImmutableRangeMap<K extends Comparable<?>, V> implements RangeMap<K, V> {
 
   @SuppressWarnings("unchecked")
   private static final ImmutableRangeMap EMPTY =
       new ImmutableRangeMap(ImmutableList.of(), ImmutableList.of());

  
Returns an empty immutable range map.
 
   @SuppressWarnings("unchecked")
   public static <K extends Comparable<?>, V> ImmutableRangeMap<K, V> of() {
     return ;
   }

  
Returns an immutable range map mapping a single range to a single value.
 
   public static <K extends Comparable<?>, V> ImmutableRangeMap<K, V> of(
       Range<K> range, V value) {
     return new ImmutableRangeMap<K, V>(ImmutableList.of(range), ImmutableList.of(value));
   }
 
   @SuppressWarnings("unchecked")
   public static <K extends Comparable<?>, V> ImmutableRangeMap<K, V> copyOf(
       RangeMap<K, ? extends V> rangeMap) {
     if (rangeMap instanceof ImmutableRangeMap) {
       return (ImmutableRangeMap<K, V>) rangeMap;
     }
     Map<Range<K>, ? extends V> map = rangeMap.asMapOfRanges();
     ImmutableList.Builder<Range<K>> rangesBuilder = new ImmutableList.Builder<Range<K>>(map.size());
     ImmutableList.Builder<V> valuesBuilder = new ImmutableList.Builder<V>(map.size());
     for (Entry<Range<K>, ? extends V> entry : map.entrySet()) {
       rangesBuilder.add(entry.getKey());
       valuesBuilder.add(entry.getValue());
     }
     return new ImmutableRangeMap<K, V>(rangesBuilder.build(), valuesBuilder.build());
   }

  
Returns a new builder for an immutable range map.
 
   public static <K extends Comparable<?>, V> Builder<K, V> builder() {
     return new Builder<K, V>();
   }

  
A builder for immutable range maps. Overlapping ranges are prohibited.
 
   public static final class Builder<K extends Comparable<?>, V> {
     private final RangeSet<K> keyRanges;
     private final RangeMap<K, V> rangeMap;
 
     public Builder() {
       this. = TreeRangeSet.create();
       this. = TreeRangeMap.create();
     }

    
Associates the specified range with the specified value.

Throws:
java.lang.IllegalArgumentException if range overlaps with any other ranges inserted into this builder, or if range is empty
    public Builder<K, V> put(Range<K> range, V value) {
      checkNotNull(range);
      checkNotNull(value);
      checkArgument(!range.isEmpty(), "Range must not be empty, but was %s"range);
      if (!.complement().encloses(range)) {
        // it's an error case; we can afford an expensive lookup
        for (Entry<Range<K>, V> entry : .asMapOfRanges().entrySet()) {
          Range<K> key = entry.getKey();
          if (key.isConnected(range) && !key.intersection(range).isEmpty()) {
            throw new IllegalArgumentException(
                "Overlapping ranges: range " + range + " overlaps with entry " + entry);
          }
        }
      }
      .add(range);
      .put(rangevalue);
      return this;
    }

    
Copies all associations from the specified range map into this builder.

Throws:
java.lang.IllegalArgumentException if any of the ranges in rangeMap overlap with ranges already in this builder
    public Builder<K, V> putAll(RangeMap<K, ? extends V> rangeMap) {
      for (Entry<Range<K>, ? extends V> entry : rangeMap.asMapOfRanges().entrySet()) {
        put(entry.getKey(), entry.getValue());
      }
      return this;
    }

    
Returns an ImmutableRangeMap containing the associations previously added to this builder.
    public ImmutableRangeMap<K, V> build() {
      Map<Range<K>, V> map = .asMapOfRanges();
      ImmutableList.Builder<Range<K>> rangesBuilder =
          new ImmutableList.Builder<Range<K>>(map.size());
      ImmutableList.Builder<V> valuesBuilder = new ImmutableList.Builder<V>(map.size());
      for (Entry<Range<K>, V> entry : map.entrySet()) {
        rangesBuilder.add(entry.getKey());
        valuesBuilder.add(entry.getValue());
      }
      return new ImmutableRangeMap<K, V>(rangesBuilder.build(), valuesBuilder.build());
    }
  }
  private final ImmutableList<Range<K>> ranges;
  private final ImmutableList<V> values;
  ImmutableRangeMap(ImmutableList<Range<K>> rangesImmutableList<V> values) {
    this. = ranges;
    this. = values;
  }
  public V get(K key) {
    int index = SortedLists.binarySearch(, Range.<K>lowerBoundFn(),
    if (index == -1) {
      return null;
    } else {
      Range<K> range = .get(index);
      return range.contains(key) ? .get(index) : null;
    }
  }
  public Map.Entry<Range<K>, V> getEntry(K key) {
    int index = SortedLists.binarySearch(, Range.<K>lowerBoundFn(),
    if (index == -1) {
      return null;
    } else {
      Range<K> range = .get(index);
      return range.contains(key) ? Maps.immutableEntry(range.get(index)) : null;
    }
  }
  public Range<K> span() {
    if (.isEmpty()) {
      throw new NoSuchElementException();
    }
    Range<K> firstRange = .get(0);
    Range<K> lastRange = .get(.size() - 1);
    return Range.create(firstRange.lowerBoundlastRange.upperBound);
  }
  public void put(Range<K> range, V value) {
    throw new UnsupportedOperationException();
  }
  public void putAll(RangeMap<K, V> rangeMap) {
    throw new UnsupportedOperationException();
  }
  public void clear() {
    throw new UnsupportedOperationException();
  }
  public void remove(Range<K> range) {
    throw new UnsupportedOperationException();
  }
  public ImmutableMap<Range<K>, V> asMapOfRanges() {
    if (.isEmpty()) {
      return ImmutableMap.of();
    }
    RegularImmutableSortedSet<Range<K>> rangeSet =
    return new RegularImmutableSortedMap<Range<K>, V>(rangeSet);
  }
  
  public ImmutableRangeMap<K, V> subRangeMap(final Range<K> range) {
    if (checkNotNull(range).isEmpty()) {
      return ImmutableRangeMap.of();
    } else if (.isEmpty() || range.encloses(span())) {
      return this;
    }
    int lowerIndex = SortedLists.binarySearch(
        , Range.<K>upperBoundFn(), range.lowerBound,
    int upperIndex = SortedLists.binarySearch(
        Range.<K>lowerBoundFn(), range.upperBound,
    if (lowerIndex >= upperIndex) {
      return ImmutableRangeMap.of();
    }
    final int off = lowerIndex;
    final int len = upperIndex - lowerIndex;
    ImmutableList<Range<K>> subRanges = new ImmutableList<Range<K>>() {
      @Override
      public int size() {
        return len;
      }
      @Override
      public Range<K> get(int index) {
        checkElementIndex(indexlen);
        if (index == 0 || index == len - 1) {
          return .get(index + off).intersection(range);
        } else {
          return .get(index + off);
        }
      }
      @Override
      boolean isPartialView() {
        return true;
      }
    };
    final ImmutableRangeMap<K, V> outer = this;
    return new ImmutableRangeMap<K, V>(
        subRanges.subList(lowerIndexupperIndex)) {
          @Override
          public ImmutableRangeMap<K, V> subRangeMap(Range<K> subRange) {
            if (range.isConnected(subRange)) {
              return outer.subRangeMap(subRange.intersection(range));
            } else {
              return ImmutableRangeMap.of();
            }
          }
    };
  }
  public int hashCode() {
    return asMapOfRanges().hashCode();
  }
  public boolean equals(@Nullable Object o) {
    if (o instanceof RangeMap) {
      RangeMap<?, ?> rangeMap = (RangeMap<?, ?>) o;
      return asMapOfRanges().equals(rangeMap.asMapOfRanges());
    }
    return false;
  }
  public String toString() {
    return asMapOfRanges().toString();
  }
New to GrepCode? Check out our FAQ X