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;
022
023import java.util.Map;
024import java.util.Map.Entry;
025
026import javax.annotation.Nullable;
027
028/**
029 * A map entry which forwards all its method calls to another map entry.
030 * Subclasses should override one or more methods to modify the behavior of the
031 * backing map entry as desired per the <a
032 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
033 *
034 * <p><i>Warning:</i> The methods of {@code ForwardingMapEntry} forward
035 * <i>indiscriminately</i> to the methods of the delegate. For example,
036 * overriding {@link #getValue} alone <i>will not</i> change the behavior of
037 * {@link #equals}, which can lead to unexpected behavior. In this case, you
038 * should override {@code equals} as well, either providing your own
039 * implementation, or delegating to the provided {@code standardEquals} method.
040 *
041 * <p>Each of the {@code standard} methods, where appropriate, use {@link
042 * Objects#equal} to test equality for both keys and values. This may not be
043 * the desired behavior for map implementations that use non-standard notions of
044 * key equality, such as the entry of a {@code SortedMap} whose comparator is
045 * not consistent with {@code equals}.
046 *
047 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
048 * when all of the methods that they depend on are thread-safe.
049 *
050 * @author Mike Bostock
051 * @author Louis Wasserman
052 * @since 2.0 (imported from Google Collections Library)
053 */
054@GwtCompatible
055public abstract class ForwardingMapEntry<K, V>
056    extends ForwardingObject implements Map.Entry<K, V> {
057  // TODO(user): identify places where thread safety is actually lost
058
059  /** Constructor for use by subclasses. */
060  protected ForwardingMapEntry() {}
061
062  @Override protected abstract Map.Entry<K, V> delegate();
063
064  @Override
065  public K getKey() {
066    return delegate().getKey();
067  }
068
069  @Override
070  public V getValue() {
071    return delegate().getValue();
072  }
073
074  @Override
075  public V setValue(V value) {
076    return delegate().setValue(value);
077  }
078
079  @Override public boolean equals(@Nullable Object object) {
080    return delegate().equals(object);
081  }
082
083  @Override 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 protected String standardToString() {
126    return getKey() + "=" + getValue();
127  }
128}