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><i>Warning:</i> The methods of {@code ForwardingMap} forward
038 * <i>indiscriminately</i> to the methods of the delegate. For example,
039 * overriding {@link #put} alone <i>will not</i> 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.0 (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.0
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.0
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.0
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.0
197 * @deprecated Use the {@code StandardKeySet} constructor instead. This
198 * method will be removed from Guava in Guava release 11.0.
199 */
200 @Beta @Deprecated protected Set<K> standardKeySet() {
201 return new StandardKeySet();
202 }
203
204 /**
205 * A sensible implementation of {@link Map#keySet} in terms of the following
206 * methods: {@link ForwardingMap#clear}, {@link ForwardingMap#containsKey},
207 * {@link ForwardingMap#isEmpty}, {@link ForwardingMap#remove}, {@link
208 * ForwardingMap#size}, and the {@link Set#iterator} method of {@link
209 * ForwardingMap#entrySet}. In many cases, you may wish to override {@link
210 * ForwardingMap#keySet} to forward to this implementation or a subclass
211 * thereof.
212 *
213 * @since 10.0
214 */
215 @Beta
216 protected class StandardKeySet extends Maps.KeySet<K, V> {
217 public StandardKeySet() {}
218
219 @Override
220 Map<K, V> map() {
221 return ForwardingMap.this;
222 }
223 }
224
225 /**
226 * A sensible, albeit inefficient, definition of {@link #containsKey} in terms
227 * of the {@code iterator} method of {@link #entrySet}. If you override {@link
228 * #entrySet}, you may wish to override {@link #containsKey} to forward to
229 * this implementation.
230 *
231 * @since 7.0
232 */
233 @Beta protected boolean standardContainsKey(@Nullable Object key) {
234 return Maps.containsKeyImpl(this, key);
235 }
236
237 /**
238 * A sensible definition of {@link #values} in terms of the following methods:
239 * {@link #clear}, {@link #containsValue}, {@link #isEmpty}, {@link #size},
240 * and the {@code iterator} method of {@link #entrySet}. In many cases, you
241 * may wish to override {@link #values} to forward to this implementation.
242 *
243 * @since 7.0
244 * @deprecated Use the {@code StandardValues} constructor instead. This
245 * method will be removed from Guava in Guava release 11.0.
246 */
247 @Beta @Deprecated protected Collection<V> standardValues() {
248 return new StandardValues();
249 }
250
251 /**
252 * A sensible implementation of {@link Map#values} in terms of the following
253 * methods: {@link ForwardingMap#clear}, {@link ForwardingMap#containsValue},
254 * {@link ForwardingMap#isEmpty}, {@link ForwardingMap#size}, and the {@link
255 * Set#iterator} method of {@link ForwardingMap#entrySet}. In many cases, you
256 * may wish to override {@link ForwardingMap#values} to forward to this
257 * implementation or a subclass thereof.
258 *
259 * @since 10.0
260 */
261 @Beta
262 protected class StandardValues extends Maps.Values<K, V> {
263 public StandardValues() {}
264
265 @Override
266 Map<K, V> map() {
267 return ForwardingMap.this;
268 }
269 }
270
271 /**
272 * A sensible definition of {@link #containsValue} in terms of the {@code
273 * iterator} method of {@link #entrySet}. If you override {@link #entrySet},
274 * you may wish to override {@link #containsValue} to forward to this
275 * implementation.
276 *
277 * @since 7.0
278 */
279 @Beta protected boolean standardContainsValue(@Nullable Object value) {
280 return Maps.containsValueImpl(this, value);
281 }
282
283 /**
284 * A sensible definition of {@link #entrySet} in terms of the specified {@code
285 * Supplier}, which is used to generate iterators over the entry set, and in
286 * terms of the following methods: {@link #clear}, {@link #containsKey},
287 * {@link #get}, {@link #isEmpty}, {@link #remove}, and {@link #size}. In many
288 * cases, you may wish to override {@link #entrySet} to forward to this
289 * implementation.
290 *
291 * @param entryIteratorSupplier A creator for iterators over the entry set.
292 * Each call to {@code get} must return an iterator that will
293 * traverse the entire entry set.
294 *
295 * @since 7.0
296 * @deprecated Use {@code StandardEntrySet} instead. This method will be
297 * removed from Guava in Guava release 11.0.
298 */
299 @Deprecated @Beta
300 protected Set<Entry<K, V>> standardEntrySet(
301 final Supplier<Iterator<Entry<K, V>>> entryIteratorSupplier) {
302 return new StandardEntrySet() {
303 @Override public Iterator<Map.Entry<K, V>> iterator() {
304 return entryIteratorSupplier.get();
305 }
306 };
307 }
308
309 /**
310 * A sensible implementation of {@link Map#entrySet} in terms of the following
311 * methods: {@link ForwardingMap#clear}, {@link ForwardingMap#containsKey},
312 * {@link ForwardingMap#get}, {@link ForwardingMap#isEmpty}, {@link
313 * ForwardingMap#remove}, and {@link ForwardingMap#size}. In many cases, you
314 * may wish to override {@link #entrySet} to forward to this implementation
315 * or a subclass thereof.
316 *
317 * @since 10.0
318 */
319 @Beta
320 protected abstract class StandardEntrySet extends Maps.EntrySet<K, V> {
321 public StandardEntrySet() {}
322
323 @Override
324 Map<K, V> map() {
325 return ForwardingMap.this;
326 }
327 }
328
329 /**
330 * A sensible definition of {@link #isEmpty} in terms of the {@code iterator}
331 * method of {@link #entrySet}. If you override {@link #entrySet}, you may
332 * wish to override {@link #isEmpty} to forward to this implementation.
333 *
334 * @since 7.0
335 */
336 @Beta protected boolean standardIsEmpty() {
337 return !entrySet().iterator().hasNext();
338 }
339
340 /**
341 * A sensible definition of {@link #equals} in terms of the {@code equals}
342 * method of {@link #entrySet}. If you override {@link #entrySet}, you may
343 * wish to override {@link #equals} to forward to this implementation.
344 *
345 * @since 7.0
346 */
347 @Beta protected boolean standardEquals(@Nullable Object object) {
348 return Maps.equalsImpl(this, object);
349 }
350
351 /**
352 * A sensible definition of {@link #hashCode} in terms of the {@code iterator}
353 * method of {@link #entrySet}. If you override {@link #entrySet}, you may
354 * wish to override {@link #hashCode} to forward to this implementation.
355 *
356 * @since 7.0
357 */
358 @Beta protected int standardHashCode() {
359 return Sets.hashCodeImpl(entrySet());
360 }
361
362 /**
363 * A sensible definition of {@link #toString} in terms of the {@code iterator}
364 * method of {@link #entrySet}. If you override {@link #entrySet}, you may
365 * wish to override {@link #toString} to forward to this implementation.
366 *
367 * @since 7.0
368 */
369 @Beta protected String standardToString() {
370 return Maps.toStringImpl(this);
371 }
372 }