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.Nullable;
025
026/**
027 * A map entry which forwards all its method calls to another map entry.
028 * Subclasses should override one or more methods to modify the behavior of the
029 * 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
033 * <i>indiscriminately</i> to the methods of the delegate. For example,
034 * overriding {@link #getValue} alone <i>will not</i> change the behavior of
035 * {@link #equals}, which can lead to unexpected behavior. In this case, you
036 * should override {@code equals} as well, either providing your own
037 * implementation, or delegating to the provided {@code standardEquals} method.
038 *
039 * <p>Each of the {@code standard} methods, where appropriate, use {@link
040 * Objects#equal} to test equality for both keys and values. This may not be
041 * the desired behavior for map implementations that use non-standard notions of
042 * key equality, such as the entry of a {@code SortedMap} whose comparator is
043 * not consistent with {@code equals}.
044 *
045 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
046 * when all of the methods that they depend on are thread-safe.
047 *
048 * @author Mike Bostock
049 * @author Louis Wasserman
050 * @since 2.0
051 */
052@GwtCompatible
053public abstract class ForwardingMapEntry<K, V> 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  public K getKey() {
064    return delegate().getKey();
065  }
066
067  @Override
068  public V getValue() {
069    return delegate().getValue();
070  }
071
072  @Override
073  public V setValue(V value) {
074    return delegate().setValue(value);
075  }
076
077  @Override
078  public boolean equals(@Nullable Object object) {
079    return delegate().equals(object);
080  }
081
082  @Override
083  public int hashCode() {
084    return delegate().hashCode();
085  }
086
087  /**
088   * A sensible definition of {@link #equals(Object)} in terms of {@link
089   * #getKey()} and {@link #getValue()}. If you override either of these
090   * methods, you may wish to override {@link #equals(Object)} to forward to
091   * this implementation.
092   *
093   * @since 7.0
094   */
095  protected boolean standardEquals(@Nullable Object object) {
096    if (object instanceof Entry) {
097      Entry<?, ?> that = (Entry<?, ?>) object;
098      return Objects.equal(this.getKey(), that.getKey())
099          && Objects.equal(this.getValue(), that.getValue());
100    }
101    return false;
102  }
103
104  /**
105   * A sensible definition of {@link #hashCode()} in terms of {@link #getKey()}
106   * and {@link #getValue()}. If you override either of these methods, you may
107   * wish to override {@link #hashCode()} to forward to this implementation.
108   *
109   * @since 7.0
110   */
111  protected int standardHashCode() {
112    K k = getKey();
113    V v = getValue();
114    return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
115  }
116
117  /**
118   * A sensible definition of {@link #toString} in terms of {@link
119   * #getKey} and {@link #getValue}. If you override either of these
120   * methods, you may wish to override {@link #equals} to forward to this
121   * implementation.
122   *
123   * @since 7.0
124   */
125  @Beta
126  protected String standardToString() {
127    return getKey() + "=" + getValue();
128  }
129}