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