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;
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  public V setValue(@ParametricNullness V value) {
077    return delegate().setValue(value);
078  }
079
080  @Override
081  public boolean equals(@CheckForNull Object object) {
082    return delegate().equals(object);
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 #getKey()} and {@link
092   * #getValue()}. If you override either of these methods, you may wish to override {@link
093   * #equals(Object)} to forward to this implementation.
094   *
095   * @since 7.0
096   */
097  protected boolean standardEquals(@CheckForNull 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()} and {@link
108   * #getValue()}. If you override either of these methods, you may wish to override {@link
109   * #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 #getKey} and {@link #getValue}.
121   * If you override either of these methods, you may wish to override {@link #equals} to forward to
122   * this implementation.
123   *
124   * @since 7.0
125   */
126  @Beta
127  protected String standardToString() {
128    return getKey() + "=" + getValue();
129  }
130}