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 com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtCompatible;
021import com.google.common.base.Objects;
022
023import java.util.Map;
024import java.util.Map.Entry;
025
026import 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
053 */
054@GwtCompatible
055public abstract class ForwardingMapEntry<K, V> extends ForwardingObject implements Map.Entry<K, V> {
056  // TODO(lowasser): identify places where thread safety is actually lost
057
058  /** Constructor for use by subclasses. */
059  protected ForwardingMapEntry() {}
060
061  @Override
062  protected abstract Map.Entry<K, V> delegate();
063
064  @Override
065  public K getKey() {
066    return delegate().getKey();
067  }
068
069  @Override
070  public V getValue() {
071    return delegate().getValue();
072  }
073
074  @Override
075  public V setValue(V value) {
076    return delegate().setValue(value);
077  }
078
079  @Override
080  public boolean equals(@Nullable Object object) {
081    return delegate().equals(object);
082  }
083
084  @Override
085  public int hashCode() {
086    return delegate().hashCode();
087  }
088
089  /**
090   * A sensible definition of {@link #equals(Object)} in terms of {@link
091   * #getKey()} and {@link #getValue()}. If you override either of these
092   * methods, you may wish to override {@link #equals(Object)} to forward to
093   * this implementation.
094   *
095   * @since 7.0
096   */
097  protected boolean standardEquals(@Nullable Object object) {
098    if (object instanceof Entry) {
099      Entry<?, ?> that = (Entry<?, ?>) object;
100      return Objects.equal(this.getKey(), that.getKey())
101          && Objects.equal(this.getValue(), that.getValue());
102    }
103    return false;
104  }
105
106  /**
107   * A sensible definition of {@link #hashCode()} in terms of {@link #getKey()}
108   * and {@link #getValue()}. If you override either of these methods, you may
109   * wish to override {@link #hashCode()} to forward to this implementation.
110   *
111   * @since 7.0
112   */
113  protected int standardHashCode() {
114    K k = getKey();
115    V v = getValue();
116    return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
117  }
118
119  /**
120   * A sensible definition of {@link #toString} in terms of {@link
121   * #getKey} and {@link #getValue}. If you override either of these
122   * methods, you may wish to override {@link #equals} to forward to this
123   * implementation.
124   *
125   * @since 7.0
126   */
127  @Beta
128  protected String standardToString() {
129    return getKey() + "=" + getValue();
130  }
131}