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