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 017 package com.google.common.collect; 018 019 import com.google.common.annotations.Beta; 020 import com.google.common.annotations.GwtCompatible; 021 import com.google.common.base.Objects; 022 import com.google.common.base.Supplier; 023 024 import java.util.Collection; 025 import java.util.Iterator; 026 import java.util.Map; 027 import java.util.Set; 028 029 import javax.annotation.Nullable; 030 031 /** 032 * A map which forwards all its method calls to another map. Subclasses should 033 * override one or more methods to modify the behavior of the backing map as 034 * desired per the <a 035 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 036 * 037 * <p><em>Warning</em>: The methods of {@code ForwardingMap} forward 038 * <em>indiscriminately</em> to the methods of the delegate. For example, 039 * overriding {@link #put} alone <em>will not</em> change the behavior of {@link 040 * #putAll}, which can lead to unexpected behavior. In this case, you should 041 * override {@code putAll} as well, either providing your own implementation, or 042 * delegating to the provided {@code standardPutAll} method. 043 * 044 * <p>Each of the {@code standard} methods, where appropriate, use {@link 045 * Objects#equal} to test equality for both keys and values. This may not be 046 * the desired behavior for map implementations that use non-standard notions of 047 * key equality, such as a {@code SortedMap} whose comparator is not consistent 048 * with {@code equals}. 049 * 050 * <p>The {@code standard} methods and the collection views they return are not 051 * guaranteed to be thread-safe, even when all of the methods that they depend 052 * on are thread-safe. 053 * 054 * @author Kevin Bourrillion 055 * @author Jared Levy 056 * @author Louis Wasserman 057 * @since 2 (imported from Google Collections Library) 058 */ 059 @GwtCompatible 060 public abstract class ForwardingMap<K, V> extends ForwardingObject 061 implements Map<K, V> { 062 // TODO(user): identify places where thread safety is actually lost 063 064 /** Constructor for use by subclasses. */ 065 protected ForwardingMap() {} 066 067 @Override protected abstract Map<K, V> delegate(); 068 069 @Override 070 public int size() { 071 return delegate().size(); 072 } 073 074 @Override 075 public boolean isEmpty() { 076 return delegate().isEmpty(); 077 } 078 079 @Override 080 public V remove(Object object) { 081 return delegate().remove(object); 082 } 083 084 @Override 085 public void clear() { 086 delegate().clear(); 087 } 088 089 @Override 090 public boolean containsKey(Object key) { 091 return delegate().containsKey(key); 092 } 093 094 @Override 095 public boolean containsValue(Object value) { 096 return delegate().containsValue(value); 097 } 098 099 @Override 100 public V get(Object key) { 101 return delegate().get(key); 102 } 103 104 @Override 105 public V put(K key, V value) { 106 return delegate().put(key, value); 107 } 108 109 @Override 110 public void putAll(Map<? extends K, ? extends V> map) { 111 delegate().putAll(map); 112 } 113 114 @Override 115 public Set<K> keySet() { 116 return delegate().keySet(); 117 } 118 119 @Override 120 public Collection<V> values() { 121 return delegate().values(); 122 } 123 124 @Override 125 public Set<Entry<K, V>> entrySet() { 126 return delegate().entrySet(); 127 } 128 129 @Override public boolean equals(@Nullable Object object) { 130 return object == this || delegate().equals(object); 131 } 132 133 @Override public int hashCode() { 134 return delegate().hashCode(); 135 } 136 137 /** 138 * A sensible definition of {@link #putAll(Map)} in terms of {@link 139 * #put(Object, Object)}. If you override {@link #put(Object, Object)}, you 140 * may wish to override {@link #putAll(Map)} to forward to this 141 * implementation. 142 * 143 * @since 7 144 */ 145 @Beta protected void standardPutAll(Map<? extends K, ? extends V> map) { 146 Maps.putAllImpl(this, map); 147 } 148 149 /** 150 * A sensible, albeit inefficient, definition of {@link #remove} in terms of 151 * the {@code iterator} method of {@link #entrySet}. If you override {@link 152 * #entrySet}, you may wish to override {@link #remove} to forward to this 153 * implementation. 154 * 155 * <p>Alternately, you may wish to override {@link #remove} with {@code 156 * keySet().remove}, assuming that approach would not lead to an infinite 157 * loop. 158 * 159 * @since 7 160 */ 161 @Beta protected V standardRemove(@Nullable Object key) { 162 Iterator<Entry<K, V>> entryIterator = entrySet().iterator(); 163 while (entryIterator.hasNext()) { 164 Entry<K, V> entry = entryIterator.next(); 165 if (Objects.equal(entry.getKey(), key)) { 166 V value = entry.getValue(); 167 entryIterator.remove(); 168 return value; 169 } 170 } 171 return null; 172 } 173 174 /** 175 * A sensible definition of {@link #clear} in terms of the {@code iterator} 176 * method of {@link #entrySet}. In many cases, you may wish to override 177 * {@link #clear} to forward to this implementation. 178 * 179 * @since 7 180 */ 181 @Beta protected void standardClear() { 182 Iterator<Entry<K, V>> entryIterator = entrySet().iterator(); 183 while (entryIterator.hasNext()) { 184 entryIterator.next(); 185 entryIterator.remove(); 186 } 187 } 188 189 /** 190 * A sensible definition of {@link #keySet} in terms of the following methods: 191 * {@link #clear}, {@link #containsKey}, {@link #isEmpty}, {@link #remove}, 192 * {@link #size}, and the {@code iterator} method of {@link #entrySet}. In 193 * many cases, you may wish to override {@link #keySet} to forward to this 194 * implementation. 195 * 196 * @since 7 197 */ 198 @Beta protected Set<K> standardKeySet() { 199 return Maps.keySetImpl(this); 200 } 201 202 /** 203 * A sensible, albeit inefficient, definition of {@link #containsKey} in terms 204 * of the {@code iterator} method of {@link #entrySet}. If you override {@link 205 * #entrySet}, you may wish to override {@link #containsKey} to forward to 206 * this implementation. 207 * 208 * @since 7 209 */ 210 @Beta protected boolean standardContainsKey(@Nullable Object key) { 211 return Maps.containsKeyImpl(this, key); 212 } 213 214 /** 215 * A sensible definition of {@link #values} in terms of the following methods: 216 * {@link #clear}, {@link #containsValue}, {@link #isEmpty}, {@link #size}, 217 * and the {@code iterator} method of {@link #entrySet}. In many cases, you 218 * may wish to override {@link #values} to forward to this implementation. 219 * 220 * @since 7 221 */ 222 @Beta protected Collection<V> standardValues() { 223 return Maps.valuesImpl(this); 224 } 225 226 /** 227 * A sensible definition of {@link #containsValue} in terms of the {@code 228 * iterator} method of {@link #entrySet}. If you override {@link #entrySet}, 229 * you may wish to override {@link #containsValue} to forward to this 230 * implementation. 231 * 232 * @since 7 233 */ 234 @Beta protected boolean standardContainsValue(@Nullable Object value) { 235 return Maps.containsValueImpl(this, value); 236 } 237 238 /** 239 * A sensible definition of {@link #entrySet} in terms of the specified {@code 240 * Supplier}, which is used to generate iterators over the entry set, and in 241 * terms of the following methods: {@link #clear}, {@link #containsKey}, 242 * {@link #get}, {@link #isEmpty}, {@link #remove}, and {@link #size}. In many 243 * cases, you may wish to override {@link #entrySet} to forward to this 244 * implementation. 245 * 246 * @param entryIteratorSupplier A creator for iterators over the entry set. 247 * Each call to {@code get} must return an iterator that will 248 * traverse the entire entry set. 249 * 250 * @since 7 251 */ 252 @Beta protected Set<Entry<K, V>> standardEntrySet( 253 Supplier<Iterator<Entry<K, V>>> entryIteratorSupplier) { 254 return Maps.entrySetImpl(this, entryIteratorSupplier); 255 } 256 257 /** 258 * A sensible definition of {@link #isEmpty} in terms of the {@code iterator} 259 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 260 * wish to override {@link #isEmpty} to forward to this implementation. 261 * 262 * @since 7 263 */ 264 @Beta protected boolean standardIsEmpty() { 265 return !entrySet().iterator().hasNext(); 266 } 267 268 /** 269 * A sensible definition of {@link #equals} in terms of the {@code equals} 270 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 271 * wish to override {@link #equals} to forward to this implementation. 272 * 273 * @since 7 274 */ 275 @Beta protected boolean standardEquals(@Nullable Object object) { 276 return Maps.equalsImpl(this, object); 277 } 278 279 /** 280 * A sensible definition of {@link #hashCode} in terms of the {@code iterator} 281 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 282 * wish to override {@link #hashCode} to forward to this implementation. 283 * 284 * @since 7 285 */ 286 @Beta protected int standardHashCode() { 287 return Sets.hashCodeImpl(entrySet()); 288 } 289 290 /** 291 * A sensible definition of {@link #toString} in terms of the {@code iterator} 292 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 293 * wish to override {@link #toString} to forward to this implementation. 294 * 295 * @since 7 296 */ 297 @Beta protected String standardToString() { 298 return Maps.toStringImpl(this); 299 } 300 }