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