001    /*
002     * Copyright (C) 2007 Google Inc.
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    
017    package com.google.common.collect;
018    
019    import com.google.common.annotations.Beta;
020    import com.google.common.annotations.GwtCompatible;
021    import com.google.common.base.Objects;
022    
023    import java.util.Map;
024    import java.util.Map.Entry;
025    
026    import 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><em>Warning</em>: The methods of {@code ForwardingMapEntry} forward
035     * <em>indiscriminately</em> to the methods of the delegate. For example,
036     * overriding {@link #getValue} alone <em>will not</em> 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 (imported from Google Collections Library)
053     */
054    @GwtCompatible
055    public 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      public K getKey() {
065        return delegate().getKey();
066      }
067    
068      public V getValue() {
069        return delegate().getValue();
070      }
071    
072      public V setValue(V value) {
073        return delegate().setValue(value);
074      }
075    
076      @Override public boolean equals(@Nullable Object object) {
077        return delegate().equals(object);
078      }
079    
080      @Override public int hashCode() {
081        return delegate().hashCode();
082      }
083    
084      /**
085       * A sensible definition of {@link #equals(Object)} in terms of {@link
086       * #getKey()} and {@link #getValue()}. If you override either of these
087       * methods, you may wish to override {@link #equals(Object)} to forward to
088       * this implementation.
089       *
090       * @since 7
091       */
092      @Beta protected boolean standardEquals(@Nullable Object object) {
093        if (object instanceof Entry) {
094          Entry<?, ?> that = (Entry<?, ?>) object;
095          return Objects.equal(this.getKey(), that.getKey())
096              && Objects.equal(this.getValue(), that.getValue());
097        }
098        return false;
099      }
100    
101      /**
102       * A sensible definition of {@link #hashCode()} in terms of {@link #getKey()}
103       * and {@link #getValue()}. If you override either of these methods, you may
104       * wish to override {@link #hashCode()} to forward to this implementation.
105       *
106       * @since 7
107       */
108      @Beta protected int standardHashCode() {
109        K k = getKey();
110        V v = getValue();
111        return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode());
112      }
113    
114      /**
115       * A sensible definition of {@link #toString} in terms of {@link
116       * #getKey} and {@link #getValue}. If you override either of these
117       * methods, you may wish to override {@link #equals} to forward to this
118       * implementation.
119       *
120       * @since 7
121       */
122      @Beta protected String standardToString() {
123        return getKey() + "=" + getValue();
124      }
125    }