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.collect.Iterators.pollNext; 020import static com.google.common.collect.Maps.keyOrNull; 021 022import com.google.common.annotations.GwtIncompatible; 023import java.util.Iterator; 024import java.util.NavigableMap; 025import java.util.NavigableSet; 026import java.util.NoSuchElementException; 027import java.util.SortedMap; 028import java.util.function.BiFunction; 029import org.jspecify.annotations.Nullable; 030 031/** 032 * A navigable map which forwards all its method calls to another navigable map. Subclasses should 033 * override one or more methods to modify the behavior of the backing map as desired per the <a 034 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 035 * 036 * <p><b>Warning:</b> The methods of {@code ForwardingNavigableMap} forward <i>indiscriminately</i> 037 * to the methods of the delegate. For example, overriding {@link #put} alone <i>will not</i> change 038 * the behavior of {@link #putAll}, which can lead to unexpected behavior. In this case, you should 039 * override {@code putAll} as well, either providing your own implementation, or delegating to the 040 * provided {@code standardPutAll} method. 041 * 042 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 043 * default} methods. Instead, it inherits their default implementations. When those implementations 044 * invoke methods, they invoke methods on the {@code ForwardingNavigableMap}. 045 * 046 * <p>Each of the {@code standard} methods uses the map's comparator (or the natural ordering of the 047 * elements, if there is no comparator) to test element equality. As a result, if the comparator is 048 * not consistent with equals, some of the standard implementations may violate the {@code Map} 049 * contract. 050 * 051 * <p>The {@code standard} methods and the collection views they return are not guaranteed to be 052 * thread-safe, even when all of the methods that they depend on are thread-safe. 053 * 054 * @author Louis Wasserman 055 * @since 12.0 056 */ 057@GwtIncompatible 058public abstract class ForwardingNavigableMap<K extends @Nullable Object, V extends @Nullable Object> 059 extends ForwardingSortedMap<K, V> implements NavigableMap<K, V> { 060 061 /** Constructor for use by subclasses. */ 062 protected ForwardingNavigableMap() {} 063 064 @Override 065 protected abstract NavigableMap<K, V> delegate(); 066 067 @Override 068 public @Nullable Entry<K, V> lowerEntry(@ParametricNullness K key) { 069 return delegate().lowerEntry(key); 070 } 071 072 /** 073 * A sensible definition of {@link #lowerEntry} in terms of the {@code lastEntry()} of {@link 074 * #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override {@code 075 * lowerEntry} to forward to this implementation. 076 */ 077 protected @Nullable Entry<K, V> standardLowerEntry(@ParametricNullness K key) { 078 return headMap(key, false).lastEntry(); 079 } 080 081 @Override 082 public @Nullable K lowerKey(@ParametricNullness K key) { 083 return delegate().lowerKey(key); 084 } 085 086 /** 087 * A sensible definition of {@link #lowerKey} in terms of {@code lowerEntry}. If you override 088 * {@link #lowerEntry}, you may wish to override {@code lowerKey} to forward to this 089 * implementation. 090 */ 091 protected @Nullable K standardLowerKey(@ParametricNullness K key) { 092 return keyOrNull(lowerEntry(key)); 093 } 094 095 @Override 096 public @Nullable Entry<K, V> floorEntry(@ParametricNullness K key) { 097 return delegate().floorEntry(key); 098 } 099 100 /** 101 * A sensible definition of {@link #floorEntry} in terms of the {@code lastEntry()} of {@link 102 * #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override {@code 103 * floorEntry} to forward to this implementation. 104 */ 105 protected @Nullable Entry<K, V> standardFloorEntry(@ParametricNullness K key) { 106 return headMap(key, true).lastEntry(); 107 } 108 109 @Override 110 public @Nullable K floorKey(@ParametricNullness K key) { 111 return delegate().floorKey(key); 112 } 113 114 /** 115 * A sensible definition of {@link #floorKey} in terms of {@code floorEntry}. If you override 116 * {@code floorEntry}, you may wish to override {@code floorKey} to forward to this 117 * implementation. 118 */ 119 protected @Nullable K standardFloorKey(@ParametricNullness K key) { 120 return keyOrNull(floorEntry(key)); 121 } 122 123 @Override 124 public @Nullable Entry<K, V> ceilingEntry(@ParametricNullness K key) { 125 return delegate().ceilingEntry(key); 126 } 127 128 /** 129 * A sensible definition of {@link #ceilingEntry} in terms of the {@code firstEntry()} of {@link 130 * #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override {@code 131 * ceilingEntry} to forward to this implementation. 132 */ 133 protected @Nullable Entry<K, V> standardCeilingEntry(@ParametricNullness K key) { 134 return tailMap(key, true).firstEntry(); 135 } 136 137 @Override 138 public @Nullable K ceilingKey(@ParametricNullness K key) { 139 return delegate().ceilingKey(key); 140 } 141 142 /** 143 * A sensible definition of {@link #ceilingKey} in terms of {@code ceilingEntry}. If you override 144 * {@code ceilingEntry}, you may wish to override {@code ceilingKey} to forward to this 145 * implementation. 146 */ 147 protected @Nullable K standardCeilingKey(@ParametricNullness K key) { 148 return keyOrNull(ceilingEntry(key)); 149 } 150 151 @Override 152 public @Nullable Entry<K, V> higherEntry(@ParametricNullness K key) { 153 return delegate().higherEntry(key); 154 } 155 156 /** 157 * A sensible definition of {@link #higherEntry} in terms of the {@code firstEntry()} of {@link 158 * #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override {@code 159 * higherEntry} to forward to this implementation. 160 */ 161 protected @Nullable Entry<K, V> standardHigherEntry(@ParametricNullness K key) { 162 return tailMap(key, false).firstEntry(); 163 } 164 165 @Override 166 public @Nullable K higherKey(@ParametricNullness K key) { 167 return delegate().higherKey(key); 168 } 169 170 /** 171 * A sensible definition of {@link #higherKey} in terms of {@code higherEntry}. If you override 172 * {@code higherEntry}, you may wish to override {@code higherKey} to forward to this 173 * implementation. 174 */ 175 protected @Nullable K standardHigherKey(@ParametricNullness K key) { 176 return keyOrNull(higherEntry(key)); 177 } 178 179 @Override 180 public @Nullable Entry<K, V> firstEntry() { 181 return delegate().firstEntry(); 182 } 183 184 /** 185 * A sensible definition of {@link #firstEntry} in terms of the {@code iterator()} of {@link 186 * #entrySet}. If you override {@code entrySet}, you may wish to override {@code firstEntry} to 187 * forward to this implementation. 188 */ 189 protected @Nullable Entry<K, V> standardFirstEntry() { 190 return Iterables.<@Nullable Entry<K, V>>getFirst(entrySet(), null); 191 } 192 193 /** 194 * A sensible definition of {@link #firstKey} in terms of {@code firstEntry}. If you override 195 * {@code firstEntry}, you may wish to override {@code firstKey} to forward to this 196 * implementation. 197 */ 198 protected K standardFirstKey() { 199 Entry<K, V> entry = firstEntry(); 200 if (entry == null) { 201 throw new NoSuchElementException(); 202 } else { 203 return entry.getKey(); 204 } 205 } 206 207 @Override 208 public @Nullable Entry<K, V> lastEntry() { 209 return delegate().lastEntry(); 210 } 211 212 /** 213 * A sensible definition of {@link #lastEntry} in terms of the {@code iterator()} of the {@link 214 * #entrySet} of {@link #descendingMap}. If you override {@code descendingMap}, you may wish to 215 * override {@code lastEntry} to forward to this implementation. 216 */ 217 protected @Nullable Entry<K, V> standardLastEntry() { 218 return Iterables.<@Nullable Entry<K, V>>getFirst(descendingMap().entrySet(), null); 219 } 220 221 /** 222 * A sensible definition of {@link #lastKey} in terms of {@code lastEntry}. If you override {@code 223 * lastEntry}, you may wish to override {@code lastKey} to forward to this implementation. 224 */ 225 protected K standardLastKey() { 226 Entry<K, V> entry = lastEntry(); 227 if (entry == null) { 228 throw new NoSuchElementException(); 229 } else { 230 return entry.getKey(); 231 } 232 } 233 234 @Override 235 public @Nullable Entry<K, V> pollFirstEntry() { 236 return delegate().pollFirstEntry(); 237 } 238 239 /** 240 * A sensible definition of {@link #pollFirstEntry} in terms of the {@code iterator} of {@code 241 * entrySet}. If you override {@code entrySet}, you may wish to override {@code pollFirstEntry} to 242 * forward to this implementation. 243 */ 244 protected @Nullable Entry<K, V> standardPollFirstEntry() { 245 return pollNext(entrySet().iterator()); 246 } 247 248 @Override 249 public @Nullable Entry<K, V> pollLastEntry() { 250 return delegate().pollLastEntry(); 251 } 252 253 /** 254 * A sensible definition of {@link #pollFirstEntry} in terms of the {@code iterator} of the {@code 255 * entrySet} of {@code descendingMap}. If you override {@code descendingMap}, you may wish to 256 * override {@code pollFirstEntry} to forward to this implementation. 257 */ 258 protected @Nullable Entry<K, V> standardPollLastEntry() { 259 return pollNext(descendingMap().entrySet().iterator()); 260 } 261 262 @Override 263 public NavigableMap<K, V> descendingMap() { 264 return delegate().descendingMap(); 265 } 266 267 /** 268 * A sensible implementation of {@link NavigableMap#descendingMap} in terms of the methods of this 269 * {@code NavigableMap}. In many cases, you may wish to override {@link 270 * ForwardingNavigableMap#descendingMap} to forward to this implementation or a subclass thereof. 271 * 272 * <p>In particular, this map iterates over entries with repeated calls to {@link 273 * NavigableMap#lowerEntry}. If a more efficient means of iteration is available, you may wish to 274 * override the {@code entryIterator()} method of this class. 275 * 276 * @since 12.0 277 */ 278 protected class StandardDescendingMap extends Maps.DescendingMap<K, V> { 279 /** Constructor for use by subclasses. */ 280 public StandardDescendingMap() {} 281 282 @Override 283 final NavigableMap<K, V> forward() { 284 return ForwardingNavigableMap.this; 285 } 286 287 @Override 288 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { 289 forward().replaceAll(function); 290 } 291 292 @Override 293 protected Iterator<Entry<K, V>> entryIterator() { 294 return new Iterator<Entry<K, V>>() { 295 private @Nullable Entry<K, V> toRemove = null; 296 private @Nullable Entry<K, V> nextOrNull = forward().lastEntry(); 297 298 @Override 299 public boolean hasNext() { 300 return nextOrNull != null; 301 } 302 303 @Override 304 public Entry<K, V> next() { 305 if (nextOrNull == null) { 306 throw new NoSuchElementException(); 307 } 308 try { 309 return nextOrNull; 310 } finally { 311 toRemove = nextOrNull; 312 nextOrNull = forward().lowerEntry(nextOrNull.getKey()); 313 } 314 } 315 316 @Override 317 public void remove() { 318 if (toRemove == null) { 319 throw new IllegalStateException("no calls to next() since the last call to remove()"); 320 } 321 forward().remove(toRemove.getKey()); 322 toRemove = null; 323 } 324 }; 325 } 326 } 327 328 @Override 329 public NavigableSet<K> navigableKeySet() { 330 return delegate().navigableKeySet(); 331 } 332 333 /** 334 * A sensible implementation of {@link NavigableMap#navigableKeySet} in terms of the methods of 335 * this {@code NavigableMap}. In many cases, you may wish to override {@link 336 * ForwardingNavigableMap#navigableKeySet} to forward to this implementation or a subclass 337 * thereof. 338 * 339 * @since 12.0 340 */ 341 protected class StandardNavigableKeySet extends Maps.NavigableKeySet<K, V> { 342 /** Constructor for use by subclasses. */ 343 public StandardNavigableKeySet() { 344 super(ForwardingNavigableMap.this); 345 } 346 } 347 348 @Override 349 public NavigableSet<K> descendingKeySet() { 350 return delegate().descendingKeySet(); 351 } 352 353 /** 354 * A sensible definition of {@link #descendingKeySet} as the {@code navigableKeySet} of {@link 355 * #descendingMap}. (The {@link StandardDescendingMap} implementation implements {@code 356 * navigableKeySet} on its own, so as not to cause an infinite loop.) If you override {@code 357 * descendingMap}, you may wish to override {@code descendingKeySet} to forward to this 358 * implementation. 359 */ 360 protected NavigableSet<K> standardDescendingKeySet() { 361 return descendingMap().navigableKeySet(); 362 } 363 364 /** 365 * A sensible definition of {@link #subMap(Object, Object)} in terms of {@link #subMap(Object, 366 * boolean, Object, boolean)}. If you override {@code subMap(K, boolean, K, boolean)}, you may 367 * wish to override {@code subMap} to forward to this implementation. 368 */ 369 @Override 370 protected SortedMap<K, V> standardSubMap( 371 @ParametricNullness K fromKey, @ParametricNullness K toKey) { 372 return subMap(fromKey, true, toKey, false); 373 } 374 375 @Override 376 public NavigableMap<K, V> subMap( 377 @ParametricNullness K fromKey, 378 boolean fromInclusive, 379 @ParametricNullness K toKey, 380 boolean toInclusive) { 381 return delegate().subMap(fromKey, fromInclusive, toKey, toInclusive); 382 } 383 384 @Override 385 public NavigableMap<K, V> headMap(@ParametricNullness K toKey, boolean inclusive) { 386 return delegate().headMap(toKey, inclusive); 387 } 388 389 @Override 390 public NavigableMap<K, V> tailMap(@ParametricNullness K fromKey, boolean inclusive) { 391 return delegate().tailMap(fromKey, inclusive); 392 } 393 394 /** 395 * A sensible definition of {@link #headMap(Object)} in terms of {@link #headMap(Object, 396 * boolean)}. If you override {@code headMap(K, boolean)}, you may wish to override {@code 397 * headMap} to forward to this implementation. 398 */ 399 protected SortedMap<K, V> standardHeadMap(@ParametricNullness K toKey) { 400 return headMap(toKey, false); 401 } 402 403 /** 404 * A sensible definition of {@link #tailMap(Object)} in terms of {@link #tailMap(Object, 405 * boolean)}. If you override {@code tailMap(K, boolean)}, you may wish to override {@code 406 * tailMap} to forward to this implementation. 407 */ 408 protected SortedMap<K, V> standardTailMap(@ParametricNullness K fromKey) { 409 return tailMap(fromKey, true); 410 } 411}