001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021import static com.google.common.base.Predicates.compose;
022import static com.google.common.base.Predicates.in;
023import static com.google.common.base.Predicates.not;
024import static java.util.Objects.requireNonNull;
025
026import com.google.common.annotations.Beta;
027import com.google.common.annotations.GwtIncompatible;
028import com.google.common.base.MoreObjects;
029import com.google.common.base.Predicate;
030import com.google.common.collect.Maps.IteratorBasedAbstractMap;
031import java.util.AbstractMap;
032import java.util.Collection;
033import java.util.Collections;
034import java.util.Iterator;
035import java.util.List;
036import java.util.Map;
037import java.util.Map.Entry;
038import java.util.NavigableMap;
039import java.util.NoSuchElementException;
040import java.util.Set;
041import java.util.function.BiFunction;
042import javax.annotation.CheckForNull;
043import org.checkerframework.checker.nullness.qual.Nullable;
044
045/**
046 * An implementation of {@code RangeMap} based on a {@code TreeMap}, supporting all optional
047 * operations.
048 *
049 * <p>Like all {@code RangeMap} implementations, this supports neither null keys nor null values.
050 *
051 * @author Louis Wasserman
052 * @since 14.0
053 */
054@Beta
055@GwtIncompatible // NavigableMap
056@ElementTypesAreNonnullByDefault
057public final class TreeRangeMap<K extends Comparable, V> implements RangeMap<K, V> {
058
059  private final NavigableMap<Cut<K>, RangeMapEntry<K, V>> entriesByLowerBound;
060
061  public static <K extends Comparable, V> TreeRangeMap<K, V> create() {
062    return new TreeRangeMap<>();
063  }
064
065  private TreeRangeMap() {
066    this.entriesByLowerBound = Maps.newTreeMap();
067  }
068
069  private static final class RangeMapEntry<K extends Comparable, V>
070      extends AbstractMapEntry<Range<K>, V> {
071    private final Range<K> range;
072    private final V value;
073
074    RangeMapEntry(Cut<K> lowerBound, Cut<K> upperBound, V value) {
075      this(Range.create(lowerBound, upperBound), value);
076    }
077
078    RangeMapEntry(Range<K> range, V value) {
079      this.range = range;
080      this.value = value;
081    }
082
083    @Override
084    public Range<K> getKey() {
085      return range;
086    }
087
088    @Override
089    public V getValue() {
090      return value;
091    }
092
093    public boolean contains(K value) {
094      return range.contains(value);
095    }
096
097    Cut<K> getLowerBound() {
098      return range.lowerBound;
099    }
100
101    Cut<K> getUpperBound() {
102      return range.upperBound;
103    }
104  }
105
106  @Override
107  @CheckForNull
108  public V get(K key) {
109    Entry<Range<K>, V> entry = getEntry(key);
110    return (entry == null) ? null : entry.getValue();
111  }
112
113  @Override
114  @CheckForNull
115  public Entry<Range<K>, V> getEntry(K key) {
116    Entry<Cut<K>, RangeMapEntry<K, V>> mapEntry =
117        entriesByLowerBound.floorEntry(Cut.belowValue(key));
118    if (mapEntry != null && mapEntry.getValue().contains(key)) {
119      return mapEntry.getValue();
120    } else {
121      return null;
122    }
123  }
124
125  @Override
126  public void put(Range<K> range, V value) {
127    if (!range.isEmpty()) {
128      checkNotNull(value);
129      remove(range);
130      entriesByLowerBound.put(range.lowerBound, new RangeMapEntry<K, V>(range, value));
131    }
132  }
133
134  @Override
135  public void putCoalescing(Range<K> range, V value) {
136    // don't short-circuit if the range is empty - it may be between two ranges we can coalesce.
137    if (entriesByLowerBound.isEmpty()) {
138      put(range, value);
139      return;
140    }
141
142    Range<K> coalescedRange = coalescedRange(range, checkNotNull(value));
143    put(coalescedRange, value);
144  }
145
146  /** Computes the coalesced range for the given range+value - does not mutate the map. */
147  private Range<K> coalescedRange(Range<K> range, V value) {
148    Range<K> coalescedRange = range;
149    Entry<Cut<K>, RangeMapEntry<K, V>> lowerEntry =
150        entriesByLowerBound.lowerEntry(range.lowerBound);
151    coalescedRange = coalesce(coalescedRange, value, lowerEntry);
152
153    Entry<Cut<K>, RangeMapEntry<K, V>> higherEntry =
154        entriesByLowerBound.floorEntry(range.upperBound);
155    coalescedRange = coalesce(coalescedRange, value, higherEntry);
156
157    return coalescedRange;
158  }
159
160  /** Returns the range that spans the given range and entry, if the entry can be coalesced. */
161  private static <K extends Comparable, V> Range<K> coalesce(
162      Range<K> range, V value, @CheckForNull Entry<Cut<K>, RangeMapEntry<K, V>> entry) {
163    if (entry != null
164        && entry.getValue().getKey().isConnected(range)
165        && entry.getValue().getValue().equals(value)) {
166      return range.span(entry.getValue().getKey());
167    }
168    return range;
169  }
170
171  @Override
172  public void putAll(RangeMap<K, V> rangeMap) {
173    for (Entry<Range<K>, V> entry : rangeMap.asMapOfRanges().entrySet()) {
174      put(entry.getKey(), entry.getValue());
175    }
176  }
177
178  @Override
179  public void clear() {
180    entriesByLowerBound.clear();
181  }
182
183  @Override
184  public Range<K> span() {
185    Entry<Cut<K>, RangeMapEntry<K, V>> firstEntry = entriesByLowerBound.firstEntry();
186    Entry<Cut<K>, RangeMapEntry<K, V>> lastEntry = entriesByLowerBound.lastEntry();
187    // Either both are null or neither is, but we check both to satisfy the nullness checker.
188    if (firstEntry == null || lastEntry == null) {
189      throw new NoSuchElementException();
190    }
191    return Range.create(
192        firstEntry.getValue().getKey().lowerBound, lastEntry.getValue().getKey().upperBound);
193  }
194
195  private void putRangeMapEntry(Cut<K> lowerBound, Cut<K> upperBound, V value) {
196    entriesByLowerBound.put(lowerBound, new RangeMapEntry<K, V>(lowerBound, upperBound, value));
197  }
198
199  @Override
200  public void remove(Range<K> rangeToRemove) {
201    if (rangeToRemove.isEmpty()) {
202      return;
203    }
204
205    /*
206     * The comments for this method will use [ ] to indicate the bounds of rangeToRemove and ( ) to
207     * indicate the bounds of ranges in the range map.
208     */
209    Entry<Cut<K>, RangeMapEntry<K, V>> mapEntryBelowToTruncate =
210        entriesByLowerBound.lowerEntry(rangeToRemove.lowerBound);
211    if (mapEntryBelowToTruncate != null) {
212      // we know ( [
213      RangeMapEntry<K, V> rangeMapEntry = mapEntryBelowToTruncate.getValue();
214      if (rangeMapEntry.getUpperBound().compareTo(rangeToRemove.lowerBound) > 0) {
215        // we know ( [ )
216        if (rangeMapEntry.getUpperBound().compareTo(rangeToRemove.upperBound) > 0) {
217          // we know ( [ ] ), so insert the range ] ) back into the map --
218          // it's being split apart
219          putRangeMapEntry(
220              rangeToRemove.upperBound,
221              rangeMapEntry.getUpperBound(),
222              mapEntryBelowToTruncate.getValue().getValue());
223        }
224        // overwrite mapEntryToTruncateBelow with a truncated range
225        putRangeMapEntry(
226            rangeMapEntry.getLowerBound(),
227            rangeToRemove.lowerBound,
228            mapEntryBelowToTruncate.getValue().getValue());
229      }
230    }
231
232    Entry<Cut<K>, RangeMapEntry<K, V>> mapEntryAboveToTruncate =
233        entriesByLowerBound.lowerEntry(rangeToRemove.upperBound);
234    if (mapEntryAboveToTruncate != null) {
235      // we know ( ]
236      RangeMapEntry<K, V> rangeMapEntry = mapEntryAboveToTruncate.getValue();
237      if (rangeMapEntry.getUpperBound().compareTo(rangeToRemove.upperBound) > 0) {
238        // we know ( ] ), and since we dealt with truncating below already,
239        // we know [ ( ] )
240        putRangeMapEntry(
241            rangeToRemove.upperBound,
242            rangeMapEntry.getUpperBound(),
243            mapEntryAboveToTruncate.getValue().getValue());
244      }
245    }
246    entriesByLowerBound.subMap(rangeToRemove.lowerBound, rangeToRemove.upperBound).clear();
247  }
248
249  private void split(Cut<K> cut) {
250    /*
251     * The comments for this method will use | to indicate the cut point and ( ) to indicate the
252     * bounds of ranges in the range map.
253     */
254    Entry<Cut<K>, RangeMapEntry<K, V>> mapEntryToSplit = entriesByLowerBound.lowerEntry(cut);
255    if (mapEntryToSplit == null) {
256      return;
257    }
258    // we know ( |
259    RangeMapEntry<K, V> rangeMapEntry = mapEntryToSplit.getValue();
260    if (rangeMapEntry.getUpperBound().compareTo(cut) <= 0) {
261      return;
262    }
263    // we know ( | )
264    putRangeMapEntry(rangeMapEntry.getLowerBound(), cut, rangeMapEntry.getValue());
265    putRangeMapEntry(cut, rangeMapEntry.getUpperBound(), rangeMapEntry.getValue());
266  }
267
268  @Override
269  public void merge(
270      Range<K> range,
271      @CheckForNull V value,
272      BiFunction<? super V, ? super @Nullable V, ? extends @Nullable V> remappingFunction) {
273    checkNotNull(range);
274    checkNotNull(remappingFunction);
275
276    if (range.isEmpty()) {
277      return;
278    }
279    split(range.lowerBound);
280    split(range.upperBound);
281
282    // Due to the splitting of any entries spanning the range bounds, we know that any entry with a
283    // lower bound in the merge range is entirely contained by the merge range.
284    Set<Entry<Cut<K>, RangeMapEntry<K, V>>> entriesInMergeRange =
285        entriesByLowerBound.subMap(range.lowerBound, range.upperBound).entrySet();
286
287    // Create entries mapping any unmapped ranges in the merge range to the specified value.
288    ImmutableMap.Builder<Cut<K>, RangeMapEntry<K, V>> gaps = ImmutableMap.builder();
289    if (value != null) {
290      final Iterator<Entry<Cut<K>, RangeMapEntry<K, V>>> backingItr =
291          entriesInMergeRange.iterator();
292      Cut<K> lowerBound = range.lowerBound;
293      while (backingItr.hasNext()) {
294        RangeMapEntry<K, V> entry = backingItr.next().getValue();
295        Cut<K> upperBound = entry.getLowerBound();
296        if (!lowerBound.equals(upperBound)) {
297          gaps.put(lowerBound, new RangeMapEntry<K, V>(lowerBound, upperBound, value));
298        }
299        lowerBound = entry.getUpperBound();
300      }
301      if (!lowerBound.equals(range.upperBound)) {
302        gaps.put(lowerBound, new RangeMapEntry<K, V>(lowerBound, range.upperBound, value));
303      }
304    }
305
306    // Remap all existing entries in the merge range.
307    final Iterator<Entry<Cut<K>, RangeMapEntry<K, V>>> backingItr = entriesInMergeRange.iterator();
308    while (backingItr.hasNext()) {
309      Entry<Cut<K>, RangeMapEntry<K, V>> entry = backingItr.next();
310      V newValue = remappingFunction.apply(entry.getValue().getValue(), value);
311      if (newValue == null) {
312        backingItr.remove();
313      } else {
314        entry.setValue(
315            new RangeMapEntry<K, V>(
316                entry.getValue().getLowerBound(), entry.getValue().getUpperBound(), newValue));
317      }
318    }
319
320    entriesByLowerBound.putAll(gaps.build());
321  }
322
323  @Override
324  public Map<Range<K>, V> asMapOfRanges() {
325    return new AsMapOfRanges(entriesByLowerBound.values());
326  }
327
328  @Override
329  public Map<Range<K>, V> asDescendingMapOfRanges() {
330    return new AsMapOfRanges(entriesByLowerBound.descendingMap().values());
331  }
332
333  private final class AsMapOfRanges extends IteratorBasedAbstractMap<Range<K>, V> {
334
335    final Iterable<Entry<Range<K>, V>> entryIterable;
336
337    @SuppressWarnings("unchecked") // it's safe to upcast iterables
338    AsMapOfRanges(Iterable<RangeMapEntry<K, V>> entryIterable) {
339      this.entryIterable = (Iterable) entryIterable;
340    }
341
342    @Override
343    public boolean containsKey(@CheckForNull Object key) {
344      return get(key) != null;
345    }
346
347    @Override
348    @CheckForNull
349    public V get(@CheckForNull Object key) {
350      if (key instanceof Range) {
351        Range<?> range = (Range<?>) key;
352        RangeMapEntry<K, V> rangeMapEntry = entriesByLowerBound.get(range.lowerBound);
353        if (rangeMapEntry != null && rangeMapEntry.getKey().equals(range)) {
354          return rangeMapEntry.getValue();
355        }
356      }
357      return null;
358    }
359
360    @Override
361    public int size() {
362      return entriesByLowerBound.size();
363    }
364
365    @Override
366    Iterator<Entry<Range<K>, V>> entryIterator() {
367      return entryIterable.iterator();
368    }
369  }
370
371  @Override
372  public RangeMap<K, V> subRangeMap(Range<K> subRange) {
373    if (subRange.equals(Range.all())) {
374      return this;
375    } else {
376      return new SubRangeMap(subRange);
377    }
378  }
379
380  @SuppressWarnings("unchecked")
381  private RangeMap<K, V> emptySubRangeMap() {
382    return (RangeMap<K, V>) (RangeMap<?, ?>) EMPTY_SUB_RANGE_MAP;
383  }
384
385  @SuppressWarnings("ConstantCaseForConstants") // This RangeMap is immutable.
386  private static final RangeMap<Comparable<?>, Object> EMPTY_SUB_RANGE_MAP =
387      new RangeMap<Comparable<?>, Object>() {
388        @Override
389        @CheckForNull
390        public Object get(Comparable<?> key) {
391          return null;
392        }
393
394        @Override
395        @CheckForNull
396        public Entry<Range<Comparable<?>>, Object> getEntry(Comparable<?> key) {
397          return null;
398        }
399
400        @Override
401        public Range<Comparable<?>> span() {
402          throw new NoSuchElementException();
403        }
404
405        @Override
406        public void put(Range<Comparable<?>> range, Object value) {
407          checkNotNull(range);
408          throw new IllegalArgumentException(
409              "Cannot insert range " + range + " into an empty subRangeMap");
410        }
411
412        @Override
413        public void putCoalescing(Range<Comparable<?>> range, Object value) {
414          checkNotNull(range);
415          throw new IllegalArgumentException(
416              "Cannot insert range " + range + " into an empty subRangeMap");
417        }
418
419        @Override
420        public void putAll(RangeMap<Comparable<?>, Object> rangeMap) {
421          if (!rangeMap.asMapOfRanges().isEmpty()) {
422            throw new IllegalArgumentException(
423                "Cannot putAll(nonEmptyRangeMap) into an empty subRangeMap");
424          }
425        }
426
427        @Override
428        public void clear() {}
429
430        @Override
431        public void remove(Range<Comparable<?>> range) {
432          checkNotNull(range);
433        }
434
435        @Override
436        public void merge(
437            Range<Comparable<?>> range,
438            @CheckForNull Object value,
439            BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
440          checkNotNull(range);
441          throw new IllegalArgumentException(
442              "Cannot merge range " + range + " into an empty subRangeMap");
443        }
444
445        @Override
446        public Map<Range<Comparable<?>>, Object> asMapOfRanges() {
447          return Collections.emptyMap();
448        }
449
450        @Override
451        public Map<Range<Comparable<?>>, Object> asDescendingMapOfRanges() {
452          return Collections.emptyMap();
453        }
454
455        @Override
456        public RangeMap<Comparable<?>, Object> subRangeMap(Range<Comparable<?>> range) {
457          checkNotNull(range);
458          return this;
459        }
460      };
461
462  private class SubRangeMap implements RangeMap<K, V> {
463
464    private final Range<K> subRange;
465
466    SubRangeMap(Range<K> subRange) {
467      this.subRange = subRange;
468    }
469
470    @Override
471    @CheckForNull
472    public V get(K key) {
473      return subRange.contains(key) ? TreeRangeMap.this.get(key) : null;
474    }
475
476    @Override
477    @CheckForNull
478    public Entry<Range<K>, V> getEntry(K key) {
479      if (subRange.contains(key)) {
480        Entry<Range<K>, V> entry = TreeRangeMap.this.getEntry(key);
481        if (entry != null) {
482          return Maps.immutableEntry(entry.getKey().intersection(subRange), entry.getValue());
483        }
484      }
485      return null;
486    }
487
488    @Override
489    public Range<K> span() {
490      Cut<K> lowerBound;
491      Entry<Cut<K>, RangeMapEntry<K, V>> lowerEntry =
492          entriesByLowerBound.floorEntry(subRange.lowerBound);
493      if (lowerEntry != null
494          && lowerEntry.getValue().getUpperBound().compareTo(subRange.lowerBound) > 0) {
495        lowerBound = subRange.lowerBound;
496      } else {
497        lowerBound = entriesByLowerBound.ceilingKey(subRange.lowerBound);
498        if (lowerBound == null || lowerBound.compareTo(subRange.upperBound) >= 0) {
499          throw new NoSuchElementException();
500        }
501      }
502
503      Cut<K> upperBound;
504      Entry<Cut<K>, RangeMapEntry<K, V>> upperEntry =
505          entriesByLowerBound.lowerEntry(subRange.upperBound);
506      if (upperEntry == null) {
507        throw new NoSuchElementException();
508      } else if (upperEntry.getValue().getUpperBound().compareTo(subRange.upperBound) >= 0) {
509        upperBound = subRange.upperBound;
510      } else {
511        upperBound = upperEntry.getValue().getUpperBound();
512      }
513      return Range.create(lowerBound, upperBound);
514    }
515
516    @Override
517    public void put(Range<K> range, V value) {
518      checkArgument(
519          subRange.encloses(range), "Cannot put range %s into a subRangeMap(%s)", range, subRange);
520      TreeRangeMap.this.put(range, value);
521    }
522
523    @Override
524    public void putCoalescing(Range<K> range, V value) {
525      if (entriesByLowerBound.isEmpty() || !subRange.encloses(range)) {
526        put(range, value);
527        return;
528      }
529
530      Range<K> coalescedRange = coalescedRange(range, checkNotNull(value));
531      // only coalesce ranges within the subRange
532      put(coalescedRange.intersection(subRange), value);
533    }
534
535    @Override
536    public void putAll(RangeMap<K, V> rangeMap) {
537      if (rangeMap.asMapOfRanges().isEmpty()) {
538        return;
539      }
540      Range<K> span = rangeMap.span();
541      checkArgument(
542          subRange.encloses(span),
543          "Cannot putAll rangeMap with span %s into a subRangeMap(%s)",
544          span,
545          subRange);
546      TreeRangeMap.this.putAll(rangeMap);
547    }
548
549    @Override
550    public void clear() {
551      TreeRangeMap.this.remove(subRange);
552    }
553
554    @Override
555    public void remove(Range<K> range) {
556      if (range.isConnected(subRange)) {
557        TreeRangeMap.this.remove(range.intersection(subRange));
558      }
559    }
560
561    @Override
562    public void merge(
563        Range<K> range,
564        @CheckForNull V value,
565        BiFunction<? super V, ? super @Nullable V, ? extends @Nullable V> remappingFunction) {
566      checkArgument(
567          subRange.encloses(range),
568          "Cannot merge range %s into a subRangeMap(%s)",
569          range,
570          subRange);
571      TreeRangeMap.this.merge(range, value, remappingFunction);
572    }
573
574    @Override
575    public RangeMap<K, V> subRangeMap(Range<K> range) {
576      if (!range.isConnected(subRange)) {
577        return emptySubRangeMap();
578      } else {
579        return TreeRangeMap.this.subRangeMap(range.intersection(subRange));
580      }
581    }
582
583    @Override
584    public Map<Range<K>, V> asMapOfRanges() {
585      return new SubRangeMapAsMap();
586    }
587
588    @Override
589    public Map<Range<K>, V> asDescendingMapOfRanges() {
590      return new SubRangeMapAsMap() {
591
592        @Override
593        Iterator<Entry<Range<K>, V>> entryIterator() {
594          if (subRange.isEmpty()) {
595            return Iterators.emptyIterator();
596          }
597          final Iterator<RangeMapEntry<K, V>> backingItr =
598              entriesByLowerBound
599                  .headMap(subRange.upperBound, false)
600                  .descendingMap()
601                  .values()
602                  .iterator();
603          return new AbstractIterator<Entry<Range<K>, V>>() {
604
605            @Override
606            @CheckForNull
607            protected Entry<Range<K>, V> computeNext() {
608              if (backingItr.hasNext()) {
609                RangeMapEntry<K, V> entry = backingItr.next();
610                if (entry.getUpperBound().compareTo(subRange.lowerBound) <= 0) {
611                  return endOfData();
612                }
613                return Maps.immutableEntry(entry.getKey().intersection(subRange), entry.getValue());
614              }
615              return endOfData();
616            }
617          };
618        }
619      };
620    }
621
622    @Override
623    public boolean equals(@CheckForNull Object o) {
624      if (o instanceof RangeMap) {
625        RangeMap<?, ?> rangeMap = (RangeMap<?, ?>) o;
626        return asMapOfRanges().equals(rangeMap.asMapOfRanges());
627      }
628      return false;
629    }
630
631    @Override
632    public int hashCode() {
633      return asMapOfRanges().hashCode();
634    }
635
636    @Override
637    public String toString() {
638      return asMapOfRanges().toString();
639    }
640
641    class SubRangeMapAsMap extends AbstractMap<Range<K>, V> {
642
643      @Override
644      public boolean containsKey(@CheckForNull Object key) {
645        return get(key) != null;
646      }
647
648      @Override
649      @CheckForNull
650      public V get(@CheckForNull Object key) {
651        try {
652          if (key instanceof Range) {
653            @SuppressWarnings("unchecked") // we catch ClassCastExceptions
654            Range<K> r = (Range<K>) key;
655            if (!subRange.encloses(r) || r.isEmpty()) {
656              return null;
657            }
658            RangeMapEntry<K, V> candidate = null;
659            if (r.lowerBound.compareTo(subRange.lowerBound) == 0) {
660              // r could be truncated on the left
661              Entry<Cut<K>, RangeMapEntry<K, V>> entry =
662                  entriesByLowerBound.floorEntry(r.lowerBound);
663              if (entry != null) {
664                candidate = entry.getValue();
665              }
666            } else {
667              candidate = entriesByLowerBound.get(r.lowerBound);
668            }
669
670            if (candidate != null
671                && candidate.getKey().isConnected(subRange)
672                && candidate.getKey().intersection(subRange).equals(r)) {
673              return candidate.getValue();
674            }
675          }
676        } catch (ClassCastException e) {
677          return null;
678        }
679        return null;
680      }
681
682      @Override
683      @CheckForNull
684      public V remove(@CheckForNull Object key) {
685        V value = get(key);
686        if (value != null) {
687          // it's definitely in the map, so the cast and requireNonNull are safe
688          @SuppressWarnings("unchecked")
689          Range<K> range = (Range<K>) requireNonNull(key);
690          TreeRangeMap.this.remove(range);
691          return value;
692        }
693        return null;
694      }
695
696      @Override
697      public void clear() {
698        SubRangeMap.this.clear();
699      }
700
701      private boolean removeEntryIf(Predicate<? super Entry<Range<K>, V>> predicate) {
702        List<Range<K>> toRemove = Lists.newArrayList();
703        for (Entry<Range<K>, V> entry : entrySet()) {
704          if (predicate.apply(entry)) {
705            toRemove.add(entry.getKey());
706          }
707        }
708        for (Range<K> range : toRemove) {
709          TreeRangeMap.this.remove(range);
710        }
711        return !toRemove.isEmpty();
712      }
713
714      @Override
715      public Set<Range<K>> keySet() {
716        return new Maps.KeySet<Range<K>, V>(SubRangeMapAsMap.this) {
717          @Override
718          public boolean remove(@CheckForNull Object o) {
719            return SubRangeMapAsMap.this.remove(o) != null;
720          }
721
722          @Override
723          public boolean retainAll(Collection<?> c) {
724            return removeEntryIf(compose(not(in(c)), Maps.<Range<K>>keyFunction()));
725          }
726        };
727      }
728
729      @Override
730      public Set<Entry<Range<K>, V>> entrySet() {
731        return new Maps.EntrySet<Range<K>, V>() {
732          @Override
733          Map<Range<K>, V> map() {
734            return SubRangeMapAsMap.this;
735          }
736
737          @Override
738          public Iterator<Entry<Range<K>, V>> iterator() {
739            return entryIterator();
740          }
741
742          @Override
743          public boolean retainAll(Collection<?> c) {
744            return removeEntryIf(not(in(c)));
745          }
746
747          @Override
748          public int size() {
749            return Iterators.size(iterator());
750          }
751
752          @Override
753          public boolean isEmpty() {
754            return !iterator().hasNext();
755          }
756        };
757      }
758
759      Iterator<Entry<Range<K>, V>> entryIterator() {
760        if (subRange.isEmpty()) {
761          return Iterators.emptyIterator();
762        }
763        Cut<K> cutToStart =
764            MoreObjects.firstNonNull(
765                entriesByLowerBound.floorKey(subRange.lowerBound), subRange.lowerBound);
766        final Iterator<RangeMapEntry<K, V>> backingItr =
767            entriesByLowerBound.tailMap(cutToStart, true).values().iterator();
768        return new AbstractIterator<Entry<Range<K>, V>>() {
769
770          @Override
771          @CheckForNull
772          protected Entry<Range<K>, V> computeNext() {
773            while (backingItr.hasNext()) {
774              RangeMapEntry<K, V> entry = backingItr.next();
775              if (entry.getLowerBound().compareTo(subRange.upperBound) >= 0) {
776                return endOfData();
777              } else if (entry.getUpperBound().compareTo(subRange.lowerBound) > 0) {
778                // this might not be true e.g. at the start of the iteration
779                return Maps.immutableEntry(entry.getKey().intersection(subRange), entry.getValue());
780              }
781            }
782            return endOfData();
783          }
784        };
785      }
786
787      @Override
788      public Collection<V> values() {
789        return new Maps.Values<Range<K>, V>(this) {
790          @Override
791          public boolean removeAll(Collection<?> c) {
792            return removeEntryIf(compose(in(c), Maps.<V>valueFunction()));
793          }
794
795          @Override
796          public boolean retainAll(Collection<?> c) {
797            return removeEntryIf(compose(not(in(c)), Maps.<V>valueFunction()));
798          }
799        };
800      }
801    }
802  }
803
804  @Override
805  public boolean equals(@CheckForNull Object o) {
806    if (o instanceof RangeMap) {
807      RangeMap<?, ?> rangeMap = (RangeMap<?, ?>) o;
808      return asMapOfRanges().equals(rangeMap.asMapOfRanges());
809    }
810    return false;
811  }
812
813  @Override
814  public int hashCode() {
815    return asMapOfRanges().hashCode();
816  }
817
818  @Override
819  public String toString() {
820    return entriesByLowerBound.values().toString();
821  }
822}