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
023 import java.util.Map;
024 import java.util.Map.Entry;
025
026 import javax.annotation.Nullable;
027
028 /**
029 * A map entry which forwards all its method calls to another map entry.
030 * Subclasses should override one or more methods to modify the behavior of the
031 * backing map entry as desired per the <a
032 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
033 *
034 * <p><i>Warning:</i> The methods of {@code ForwardingMapEntry} forward
035 * <i>indiscriminately</i> to the methods of the delegate. For example,
036 * overriding {@link #getValue} alone <i>will not</i> change the behavior of
037 * {@link #equals}, which can lead to unexpected behavior. In this case, you
038 * should override {@code equals} as well, either providing your own
039 * implementation, or delegating to the provided {@code standardEquals} method.
040 *
041 * <p>Each of the {@code standard} methods, where appropriate, use {@link
042 * Objects#equal} to test equality for both keys and values. This may not be
043 * the desired behavior for map implementations that use non-standard notions of
044 * key equality, such as the entry of a {@code SortedMap} whose comparator is
045 * not consistent with {@code equals}.
046 *
047 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
048 * when all of the methods that they depend on are thread-safe.
049 *
050 * @author Mike Bostock
051 * @author Louis Wasserman
052 * @since 2.0 (imported from Google Collections Library)
053 */
054 @GwtCompatible
055 public abstract class ForwardingMapEntry<K, V>
056 extends ForwardingObject implements Map.Entry<K, V> {
057 // TODO(user): identify places where thread safety is actually lost
058
059 /** Constructor for use by subclasses. */
060 protected ForwardingMapEntry() {}
061
062
063 @Override
064 protected abstract Map.Entry<K, V> delegate();
065
066 public K getKey() {
067 return delegate().getKey();
068 }
069
070 public V getValue() {
071 return delegate().getValue();
072 }
073
074 public V setValue(V value) {
075 return delegate().setValue(value);
076 }
077
078
079 @Override
080 public boolean equals(@Nullable Object object) {
081 return delegate().equals(object);
082 }
083
084
085 @Override
086 public int hashCode() {
087 return delegate().hashCode();
088 }
089
090 /**
091 * A sensible definition of {@link #equals(Object)} in terms of {@link
092 * #getKey()} and {@link #getValue()}. If you override either of these
093 * methods, you may wish to override {@link #equals(Object)} to forward to
094 * this implementation.
095 *
096 * @since 7.0
097 */
098 @Beta protected boolean standardEquals(@Nullable Object object) {
099 if (object instanceof Entry) {
100 Entry<?, ?> that = (Entry<?, ?>) object;
101 return Objects.equal(this.getKey(), that.getKey())
102 && Objects.equal(this.getValue(), that.getValue());
103 }
104 return false;
105 }
106
107 /**
108 * A sensible definition of {@link #hashCode()} in terms of {@link #getKey()}
109 * and {@link #getValue()}. If you override either of these methods, you may
110 * wish to override {@link #hashCode()} to forward to this implementation.
111 *
112 * @since 7.0
113 */
114 @Beta protected int standardHashCode() {
115 K k = getKey();
116 V v = getValue();
117 return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
118 }
119
120 /**
121 * A sensible definition of {@link #toString} in terms of {@link
122 * #getKey} and {@link #getValue}. If you override either of these
123 * methods, you may wish to override {@link #equals} to forward to this
124 * implementation.
125 *
126 * @since 7.0
127 */
128 @Beta protected String standardToString() {
129 return getKey() + "=" + getValue();
130 }
131 }