001/* 002 * Copyright (C) 2007 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; 020 021import com.google.common.annotations.GwtCompatible; 022import java.util.Comparator; 023import java.util.NoSuchElementException; 024import java.util.SortedMap; 025import org.jspecify.annotations.Nullable; 026 027/** 028 * A sorted map which forwards all its method calls to another sorted map. Subclasses should 029 * override one or more methods to modify the behavior of the backing sorted map as desired per the 030 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 031 * 032 * <p><b>Warning:</b> The methods of {@code ForwardingSortedMap} forward <i>indiscriminately</i> to 033 * the methods of the delegate. For example, overriding {@link #put} alone <i>will not</i> change 034 * the behavior of {@link #putAll}, which can lead to unexpected behavior. In this case, you should 035 * override {@code putAll} as well, either providing your own implementation, or delegating to the 036 * provided {@code standardPutAll} method. 037 * 038 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 039 * default} methods. Instead, it inherits their default implementations. When those implementations 040 * invoke methods, they invoke methods on the {@code ForwardingSortedMap}. 041 * 042 * <p>Each of the {@code standard} methods, where appropriate, use the comparator of the map to test 043 * equality for both keys and values, unlike {@code ForwardingMap}. 044 * 045 * <p>The {@code standard} methods and the collection views they return are not guaranteed to be 046 * thread-safe, even when all of the methods that they depend on are thread-safe. 047 * 048 * @author Mike Bostock 049 * @author Louis Wasserman 050 * @since 2.0 051 */ 052@GwtCompatible 053public abstract class ForwardingSortedMap<K extends @Nullable Object, V extends @Nullable Object> 054 extends ForwardingMap<K, V> implements SortedMap<K, V> { 055 // TODO(lowasser): identify places where thread safety is actually lost 056 057 /** Constructor for use by subclasses. */ 058 protected ForwardingSortedMap() {} 059 060 @Override 061 protected abstract SortedMap<K, V> delegate(); 062 063 @Override 064 public @Nullable Comparator<? super K> comparator() { 065 return delegate().comparator(); 066 } 067 068 @Override 069 @ParametricNullness 070 public K firstKey() { 071 return delegate().firstKey(); 072 } 073 074 @Override 075 public SortedMap<K, V> headMap(@ParametricNullness K toKey) { 076 return delegate().headMap(toKey); 077 } 078 079 @Override 080 @ParametricNullness 081 public K lastKey() { 082 return delegate().lastKey(); 083 } 084 085 @Override 086 public SortedMap<K, V> subMap(@ParametricNullness K fromKey, @ParametricNullness K toKey) { 087 return delegate().subMap(fromKey, toKey); 088 } 089 090 @Override 091 public SortedMap<K, V> tailMap(@ParametricNullness K fromKey) { 092 return delegate().tailMap(fromKey); 093 } 094 095 /** 096 * A sensible implementation of {@link SortedMap#keySet} in terms of the methods of {@code 097 * ForwardingSortedMap}. In many cases, you may wish to override {@link 098 * ForwardingSortedMap#keySet} to forward to this implementation or a subclass thereof. 099 * 100 * @since 15.0 101 */ 102 protected class StandardKeySet extends Maps.SortedKeySet<K, V> { 103 /** Constructor for use by subclasses. */ 104 public StandardKeySet() { 105 super(ForwardingSortedMap.this); 106 } 107 } 108 109 // unsafe, but worst case is a CCE or NPE is thrown, which callers will be expecting 110 @SuppressWarnings({"unchecked", "nullness"}) 111 static int unsafeCompare( 112 @Nullable Comparator<?> comparator, @Nullable Object o1, @Nullable Object o2) { 113 if (comparator == null) { 114 return ((Comparable<@Nullable Object>) o1).compareTo(o2); 115 } else { 116 return ((Comparator<@Nullable Object>) comparator).compare(o1, o2); 117 } 118 } 119 120 /** 121 * A sensible definition of {@link #containsKey} in terms of the {@code firstKey()} method of 122 * {@link #tailMap}. If you override {@link #tailMap}, you may wish to override {@link 123 * #containsKey} to forward to this implementation. 124 * 125 * @since 7.0 126 */ 127 @Override 128 protected boolean standardContainsKey(@Nullable Object key) { 129 try { 130 // any CCE or NPE will be caught 131 @SuppressWarnings({"unchecked", "nullness"}) 132 SortedMap<@Nullable Object, V> self = (SortedMap<@Nullable Object, V>) this; 133 Object ceilingKey = self.tailMap(key).firstKey(); 134 return unsafeCompare(comparator(), ceilingKey, key) == 0; 135 } catch (ClassCastException | NoSuchElementException | NullPointerException e) { 136 return false; 137 } 138 } 139 140 /** 141 * A sensible default implementation of {@link #subMap(Object, Object)} in terms of {@link 142 * #headMap(Object)} and {@link #tailMap(Object)}. In some situations, you may wish to override 143 * {@link #subMap(Object, Object)} to forward to this implementation. 144 * 145 * @since 7.0 146 */ 147 protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) { 148 checkArgument(unsafeCompare(comparator(), fromKey, toKey) <= 0, "fromKey must be <= toKey"); 149 return tailMap(fromKey).headMap(toKey); 150 } 151}