001 /* 002 * Copyright (C) 2007 Google Inc. 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 public int size() { 070 return delegate().size(); 071 } 072 073 public boolean isEmpty() { 074 return delegate().isEmpty(); 075 } 076 077 public V remove(Object object) { 078 return delegate().remove(object); 079 } 080 081 public void clear() { 082 delegate().clear(); 083 } 084 085 public boolean containsKey(Object key) { 086 return delegate().containsKey(key); 087 } 088 089 public boolean containsValue(Object value) { 090 return delegate().containsValue(value); 091 } 092 093 public V get(Object key) { 094 return delegate().get(key); 095 } 096 097 public V put(K key, V value) { 098 return delegate().put(key, value); 099 } 100 101 public void putAll(Map<? extends K, ? extends V> map) { 102 delegate().putAll(map); 103 } 104 105 public Set<K> keySet() { 106 return delegate().keySet(); 107 } 108 109 public Collection<V> values() { 110 return delegate().values(); 111 } 112 113 public Set<Entry<K, V>> entrySet() { 114 return delegate().entrySet(); 115 } 116 117 @Override public boolean equals(@Nullable Object object) { 118 return object == this || delegate().equals(object); 119 } 120 121 @Override public int hashCode() { 122 return delegate().hashCode(); 123 } 124 125 /** 126 * A sensible definition of {@link #putAll(Map)} in terms of {@link 127 * #put(Object, Object)}. If you override {@link #put(Object, Object)}, you 128 * may wish to override {@link #putAll(Map)} to forward to this 129 * implementation. 130 * 131 * @since 7 132 */ 133 @Beta protected void standardPutAll(Map<? extends K, ? extends V> map) { 134 Maps.putAllImpl(this, map); 135 } 136 137 /** 138 * A sensible, albeit inefficient, definition of {@link #remove} in terms of 139 * the {@code iterator} method of {@link #entrySet}. If you override {@link 140 * #entrySet}, you may wish to override {@link #remove} to forward to this 141 * implementation. 142 * 143 * <p>Alternately, you may wish to override {@link #remove} with {@code 144 * keySet().remove}, assuming that approach would not lead to an infinite 145 * loop. 146 * 147 * @since 7 148 */ 149 @Beta protected V standardRemove(@Nullable Object key) { 150 Iterator<Entry<K, V>> entryIterator = entrySet().iterator(); 151 while (entryIterator.hasNext()) { 152 Entry<K, V> entry = entryIterator.next(); 153 if (Objects.equal(entry.getKey(), key)) { 154 V value = entry.getValue(); 155 entryIterator.remove(); 156 return value; 157 } 158 } 159 return null; 160 } 161 162 /** 163 * A sensible definition of {@link #clear} in terms of the {@code iterator} 164 * method of {@link #entrySet}. In many cases, you may wish to override 165 * {@link #clear} to forward to this implementation. 166 * 167 * @since 7 168 */ 169 @Beta protected void standardClear() { 170 Iterator<Entry<K, V>> entryIterator = entrySet().iterator(); 171 while (entryIterator.hasNext()) { 172 entryIterator.next(); 173 entryIterator.remove(); 174 } 175 } 176 177 /** 178 * A sensible definition of {@link #keySet} in terms of the following methods: 179 * {@link #clear}, {@link #containsKey}, {@link #isEmpty}, {@link #remove}, 180 * {@link #size}, and the {@code iterator} method of {@link #entrySet}. In 181 * many cases, you may wish to override {@link #keySet} to forward to this 182 * implementation. 183 * 184 * @since 7 185 */ 186 @Beta protected Set<K> standardKeySet() { 187 return Maps.keySetImpl(this); 188 } 189 190 /** 191 * A sensible, albeit inefficient, definition of {@link #containsKey} in terms 192 * of the {@code iterator} method of {@link #entrySet}. If you override {@link 193 * #entrySet}, you may wish to override {@link #containsKey} to forward to 194 * this implementation. 195 * 196 * @since 7 197 */ 198 @Beta protected boolean standardContainsKey(@Nullable Object key) { 199 return Maps.containsKeyImpl(this, key); 200 } 201 202 /** 203 * A sensible definition of {@link #values} in terms of the following methods: 204 * {@link #clear}, {@link #containsValue}, {@link #isEmpty}, {@link #size}, 205 * and the {@code iterator} method of {@link #entrySet}. In many cases, you 206 * may wish to override {@link #values} to forward to this implementation. 207 * 208 * @since 7 209 */ 210 @Beta protected Collection<V> standardValues() { 211 return Maps.valuesImpl(this); 212 } 213 214 /** 215 * A sensible definition of {@link #containsValue} in terms of the {@code 216 * iterator} method of {@link #entrySet}. If you override {@link #entrySet}, 217 * you may wish to override {@link #containsValue} to forward to this 218 * implementation. 219 * 220 * @since 7 221 */ 222 @Beta protected boolean standardContainsValue(@Nullable Object value) { 223 return Maps.containsValueImpl(this, value); 224 } 225 226 /** 227 * A sensible definition of {@link #entrySet} in terms of the specified {@code 228 * Supplier}, which is used to generate iterators over the entry set, and in 229 * terms of the following methods: {@link #clear}, {@link #containsKey}, 230 * {@link #get}, {@link #isEmpty}, {@link #remove}, and {@link #size}. In many 231 * cases, you may wish to override {@link #entrySet} to forward to this 232 * implementation. 233 * 234 * @param entryIteratorSupplier A creator for iterators over the entry set. 235 * Each call to {@code get} must return an iterator that will 236 * traverse the entire entry set. 237 * 238 * @since 7 239 */ 240 @Beta protected Set<Entry<K, V>> standardEntrySet( 241 Supplier<Iterator<Entry<K, V>>> entryIteratorSupplier) { 242 return Maps.entrySetImpl(this, entryIteratorSupplier); 243 } 244 245 /** 246 * A sensible definition of {@link #isEmpty} in terms of the {@code iterator} 247 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 248 * wish to override {@link #isEmpty} to forward to this implementation. 249 * 250 * @since 7 251 */ 252 @Beta protected boolean standardIsEmpty() { 253 return !entrySet().iterator().hasNext(); 254 } 255 256 /** 257 * A sensible definition of {@link #equals} in terms of the {@code equals} 258 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 259 * wish to override {@link #equals} to forward to this implementation. 260 * 261 * @since 7 262 */ 263 @Beta protected boolean standardEquals(@Nullable Object object) { 264 return Maps.equalsImpl(this, object); 265 } 266 267 /** 268 * A sensible definition of {@link #hashCode} in terms of the {@code iterator} 269 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 270 * wish to override {@link #hashCode} to forward to this implementation. 271 * 272 * @since 7 273 */ 274 @Beta protected int standardHashCode() { 275 return Sets.hashCodeImpl(entrySet()); 276 } 277 278 /** 279 * A sensible definition of {@link #toString} in terms of the {@code iterator} 280 * method of {@link #entrySet}. If you override {@link #entrySet}, you may 281 * wish to override {@link #toString} to forward to this implementation. 282 * 283 * @since 7 284 */ 285 @Beta protected String standardToString() { 286 return Maps.toStringImpl(this); 287 } 288 }