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 com.google.common.annotations.GwtIncompatible;
020import com.google.errorprone.annotations.DoNotMock;
021import java.util.Collection;
022import java.util.Map;
023import java.util.Map.Entry;
024import java.util.NoSuchElementException;
025import org.jspecify.annotations.Nullable;
026
027/**
028 * A mapping from disjoint nonempty ranges to non-null values. Queries look up the value associated
029 * with the range (if any) that contains a specified key.
030 *
031 * <p>In contrast to {@link RangeSet}, no "coalescing" is done of {@linkplain
032 * Range#isConnected(Range) connected} ranges, even if they are mapped to the same value.
033 *
034 * @author Louis Wasserman
035 * @since 14.0
036 */
037@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989
038@DoNotMock("Use ImmutableRangeMap or TreeRangeMap")
039@GwtIncompatible
040public interface RangeMap<K extends Comparable, V> {
041  /*
042   * TODO(cpovirk): These docs sometimes say "map" and sometimes say "range map." Pick one, or at
043   * least decide on a policy for when to use which.
044   */
045
046  /**
047   * Returns the value associated with the specified key, or {@code null} if there is no such value.
048   *
049   * <p>Specifically, if any range in this range map contains the specified key, the value
050   * associated with that range is returned.
051   */
052  @Nullable V get(K key);
053
054  /**
055   * Returns the range containing this key and its associated value, if such a range is present in
056   * the range map, or {@code null} otherwise.
057   */
058  @Nullable Entry<Range<K>, V> getEntry(K key);
059
060  /**
061   * Returns the minimal range {@linkplain Range#encloses(Range) enclosing} the ranges in this
062   * {@code RangeMap}.
063   *
064   * @throws NoSuchElementException if this range map is empty
065   */
066  Range<K> span();
067
068  /**
069   * Maps a range to a specified value (optional operation).
070   *
071   * <p>Specifically, after a call to {@code put(range, value)}, if {@link
072   * Range#contains(Comparable) range.contains(k)}, then {@link #get(Comparable) get(k)} will return
073   * {@code value}.
074   *
075   * <p>If {@code range} {@linkplain Range#isEmpty() is empty}, then this is a no-op.
076   */
077  void put(Range<K> range, V value);
078
079  /**
080   * Maps a range to a specified value, coalescing this range with any existing ranges with the same
081   * value that are {@linkplain Range#isConnected connected} to this range.
082   *
083   * <p>The behavior of {@link #get(Comparable) get(k)} after calling this method is identical to
084   * the behavior described in {@link #put(Range, Object) put(range, value)}, however the ranges
085   * returned from {@link #asMapOfRanges} will be different if there were existing entries which
086   * connect to the given range and value.
087   *
088   * <p>Even if the input range is empty, if it is connected on both sides by ranges mapped to the
089   * same value those two ranges will be coalesced.
090   *
091   * <p><b>Note:</b> coalescing requires calling {@code .equals()} on any connected values, which
092   * may be expensive depending on the value type. Using this method on range maps with large values
093   * such as {@link Collection} types is discouraged.
094   *
095   * @since 22.0
096   */
097  void putCoalescing(Range<K> range, V value);
098
099  /** Puts all the associations from {@code rangeMap} into this range map (optional operation). */
100  void putAll(RangeMap<K, ? extends V> rangeMap);
101
102  /** Removes all associations from this range map (optional operation). */
103  void clear();
104
105  /**
106   * Removes all associations from this range map in the specified range (optional operation).
107   *
108   * <p>If {@code !range.contains(k)}, {@link #get(Comparable) get(k)} will return the same result
109   * before and after a call to {@code remove(range)}. If {@code range.contains(k)}, then after a
110   * call to {@code remove(range)}, {@code get(k)} will return {@code null}.
111   */
112  void remove(Range<K> range);
113
114  /**
115   * Returns a view of this range map as an unmodifiable {@code Map<Range<K>, V>}. Modifications to
116   * this range map are guaranteed to read through to the returned {@code Map}.
117   *
118   * <p>The returned {@code Map} iterates over entries in ascending order of the bounds of the
119   * {@code Range} entries.
120   *
121   * <p>It is guaranteed that no empty ranges will be in the returned {@code Map}.
122   */
123  Map<Range<K>, V> asMapOfRanges();
124
125  /**
126   * Returns a view of this range map as an unmodifiable {@code Map<Range<K>, V>}. Modifications to
127   * this range map are guaranteed to read through to the returned {@code Map}.
128   *
129   * <p>The returned {@code Map} iterates over entries in descending order of the bounds of the
130   * {@code Range} entries.
131   *
132   * <p>It is guaranteed that no empty ranges will be in the returned {@code Map}.
133   *
134   * @since 19.0
135   */
136  Map<Range<K>, V> asDescendingMapOfRanges();
137
138  /**
139   * Returns a view of the part of this range map that intersects with {@code range}.
140   *
141   * <p>For example, if {@code rangeMap} had the entries {@code [1, 5] => "foo", (6, 8) => "bar",
142   * (10, ∞) => "baz"} then {@code rangeMap.subRangeMap(Range.open(3, 12))} would return a range map
143   * with the entries {@code (3, 5] => "foo", (6, 8) => "bar", (10, 12) => "baz"}.
144   *
145   * <p>The returned range map supports all optional operations that this range map supports, except
146   * for {@code asMapOfRanges().iterator().remove()}.
147   *
148   * <p>The returned range map will throw an {@link IllegalArgumentException} on an attempt to
149   * insert a range not {@linkplain Range#encloses(Range) enclosed} by {@code range}.
150   */
151  // TODO(cpovirk): Consider documenting that IAE on the various methods that can throw it.
152  RangeMap<K, V> subRangeMap(Range<K> range);
153
154  /**
155   * Returns {@code true} if {@code obj} is another {@code RangeMap} that has an equivalent {@link
156   * #asMapOfRanges()}.
157   */
158  @Override
159  boolean equals(@Nullable Object o);
160
161  /** Returns {@code asMapOfRanges().hashCode()}. */
162  @Override
163  int hashCode();
164
165  /** Returns a readable string representation of this range map. */
166  @Override
167  String toString();
168}