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.Maps.keyOrNull; 020import static com.google.common.collect.Maps.snapshotFirst; 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 snapshotFirst(entrySet(), /* remove= */ false); 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 snapshotFirst(descendingMap().entrySet(), /* remove= */ false); 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 snapshotFirst(entrySet(), /* remove= */ true); 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 snapshotFirst(descendingMap().entrySet(), /* remove= */ true); 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 final Iterator<Entry<K, V>> internalEntryIterator() { 294 return entryIterator(); 295 } 296 297 protected Iterator<Entry<K, V>> entryIterator() { 298 return new Iterator<Entry<K, V>>() { 299 @Nullable Entry<K, V> toRemove = null; 300 @Nullable Entry<K, V> nextOrNull = forward().lastEntry(); 301 302 @Override 303 public boolean hasNext() { 304 return nextOrNull != null; 305 } 306 307 @Override 308 public Entry<K, V> next() { 309 if (nextOrNull == null) { 310 throw new NoSuchElementException(); 311 } 312 try { 313 return nextOrNull; 314 } finally { 315 toRemove = nextOrNull; 316 nextOrNull = forward().lowerEntry(nextOrNull.getKey()); 317 } 318 } 319 320 @Override 321 public void remove() { 322 if (toRemove == null) { 323 throw new IllegalStateException("no calls to next() since the last call to remove()"); 324 } 325 forward().remove(toRemove.getKey()); 326 toRemove = null; 327 } 328 }; 329 } 330 } 331 332 @Override 333 public NavigableSet<K> navigableKeySet() { 334 return delegate().navigableKeySet(); 335 } 336 337 /** 338 * A sensible implementation of {@link NavigableMap#navigableKeySet} in terms of the methods of 339 * this {@code NavigableMap}. In many cases, you may wish to override {@link 340 * ForwardingNavigableMap#navigableKeySet} to forward to this implementation or a subclass 341 * thereof. 342 * 343 * @since 12.0 344 */ 345 protected class StandardNavigableKeySet extends Maps.NavigableKeySet<K, V> { 346 /** Constructor for use by subclasses. */ 347 public StandardNavigableKeySet() { 348 super(ForwardingNavigableMap.this); 349 } 350 } 351 352 @Override 353 public NavigableSet<K> descendingKeySet() { 354 return delegate().descendingKeySet(); 355 } 356 357 /** 358 * A sensible definition of {@link #descendingKeySet} as the {@code navigableKeySet} of {@link 359 * #descendingMap}. (The {@link StandardDescendingMap} implementation implements {@code 360 * navigableKeySet} on its own, so as not to cause an infinite loop.) If you override {@code 361 * descendingMap}, you may wish to override {@code descendingKeySet} to forward to this 362 * implementation. 363 */ 364 protected NavigableSet<K> standardDescendingKeySet() { 365 return descendingMap().navigableKeySet(); 366 } 367 368 /** 369 * A sensible definition of {@link #subMap(Object, Object)} in terms of {@link #subMap(Object, 370 * boolean, Object, boolean)}. If you override {@code subMap(K, boolean, K, boolean)}, you may 371 * wish to override {@code subMap} to forward to this implementation. 372 */ 373 @Override 374 protected SortedMap<K, V> standardSubMap( 375 @ParametricNullness K fromKey, @ParametricNullness K toKey) { 376 return subMap(fromKey, true, toKey, false); 377 } 378 379 @Override 380 public NavigableMap<K, V> subMap( 381 @ParametricNullness K fromKey, 382 boolean fromInclusive, 383 @ParametricNullness K toKey, 384 boolean toInclusive) { 385 return delegate().subMap(fromKey, fromInclusive, toKey, toInclusive); 386 } 387 388 @Override 389 public NavigableMap<K, V> headMap(@ParametricNullness K toKey, boolean inclusive) { 390 return delegate().headMap(toKey, inclusive); 391 } 392 393 @Override 394 public NavigableMap<K, V> tailMap(@ParametricNullness K fromKey, boolean inclusive) { 395 return delegate().tailMap(fromKey, inclusive); 396 } 397 398 /** 399 * A sensible definition of {@link #headMap(Object)} in terms of {@link #headMap(Object, 400 * boolean)}. If you override {@code headMap(K, boolean)}, you may wish to override {@code 401 * headMap} to forward to this implementation. 402 */ 403 protected SortedMap<K, V> standardHeadMap(@ParametricNullness K toKey) { 404 return headMap(toKey, false); 405 } 406 407 /** 408 * A sensible definition of {@link #tailMap(Object)} in terms of {@link #tailMap(Object, 409 * boolean)}. If you override {@code tailMap(K, boolean)}, you may wish to override {@code 410 * tailMap} to forward to this implementation. 411 */ 412 protected SortedMap<K, V> standardTailMap(@ParametricNullness K fromKey) { 413 return tailMap(fromKey, true); 414 } 415}