1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.annotations.Beta; 20 import com.google.common.annotations.GwtCompatible; 21 import com.google.common.base.Objects; 22 import java.util.Map; 23 import java.util.Map.Entry; 24 import javax.annotation.CheckForNull; 25 import org.checkerframework.checker.nullness.qual.Nullable; 26 27 /** 28 * A map entry which forwards all its method calls to another map entry. Subclasses should override 29 * one or more methods to modify the behavior of the backing map entry as desired per the <a 30 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 31 * 32 * <p><b>Warning:</b> The methods of {@code ForwardingMapEntry} forward <i>indiscriminately</i> to 33 * the methods of the delegate. For example, overriding {@link #getValue} alone <i>will not</i> 34 * change the behavior of {@link #equals}, which can lead to unexpected behavior. In this case, you 35 * should override {@code equals} as well, either providing your own implementation, or delegating 36 * to the provided {@code standardEquals} method. 37 * 38 * <p>Each of the {@code standard} methods, where appropriate, use {@link Objects#equal} to test 39 * equality for both keys and values. This may not be the desired behavior for map implementations 40 * that use non-standard notions of key equality, such as the entry of a {@code SortedMap} whose 41 * comparator is not consistent with {@code equals}. 42 * 43 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even when all of the 44 * methods that they depend on are thread-safe. 45 * 46 * @author Mike Bostock 47 * @author Louis Wasserman 48 * @since 2.0 49 */ 50 @GwtCompatible 51 @ElementTypesAreNonnullByDefault 52 public abstract class ForwardingMapEntry<K extends @Nullable Object, V extends @Nullable Object> 53 extends ForwardingObject implements Map.Entry<K, V> { 54 // TODO(lowasser): identify places where thread safety is actually lost 55 56 /** Constructor for use by subclasses. */ ForwardingMapEntry()57 protected ForwardingMapEntry() {} 58 59 @Override delegate()60 protected abstract Entry<K, V> delegate(); 61 62 @Override 63 @ParametricNullness getKey()64 public K getKey() { 65 return delegate().getKey(); 66 } 67 68 @Override 69 @ParametricNullness getValue()70 public V getValue() { 71 return delegate().getValue(); 72 } 73 74 @Override 75 @ParametricNullness setValue(@arametricNullness V value)76 public V setValue(@ParametricNullness V value) { 77 return delegate().setValue(value); 78 } 79 80 @Override equals(@heckForNull Object object)81 public boolean equals(@CheckForNull Object object) { 82 return delegate().equals(object); 83 } 84 85 @Override hashCode()86 public int hashCode() { 87 return delegate().hashCode(); 88 } 89 90 /** 91 * A sensible definition of {@link #equals(Object)} in terms of {@link #getKey()} and {@link 92 * #getValue()}. If you override either of these methods, you may wish to override {@link 93 * #equals(Object)} to forward to this implementation. 94 * 95 * @since 7.0 96 */ standardEquals(@heckForNull Object object)97 protected boolean standardEquals(@CheckForNull Object object) { 98 if (object instanceof Entry) { 99 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 */ standardHashCode()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 standardToString()127 protected String standardToString() { 128 return getKey() + "=" + getValue(); 129 } 130 } 131