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