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.GwtCompatible; 20 import com.google.common.base.Objects; 21 import java.util.Map.Entry; 22 import javax.annotation.CheckForNull; 23 import org.checkerframework.checker.nullness.qual.Nullable; 24 25 /** 26 * Implementation of the {@code equals}, {@code hashCode}, and {@code toString} methods of {@code 27 * Entry}. 28 * 29 * @author Jared Levy 30 */ 31 @GwtCompatible 32 @ElementTypesAreNonnullByDefault 33 abstract class AbstractMapEntry<K extends @Nullable Object, V extends @Nullable Object> 34 implements Entry<K, V> { 35 36 @Override 37 @ParametricNullness getKey()38 public abstract K getKey(); 39 40 @Override 41 @ParametricNullness getValue()42 public abstract V getValue(); 43 44 @Override 45 @ParametricNullness setValue(@arametricNullness V value)46 public V setValue(@ParametricNullness V value) { 47 throw new UnsupportedOperationException(); 48 } 49 50 @Override equals(@heckForNull Object object)51 public boolean equals(@CheckForNull Object object) { 52 if (object instanceof Entry) { 53 Entry<?, ?> that = (Entry<?, ?>) object; 54 return Objects.equal(this.getKey(), that.getKey()) 55 && Objects.equal(this.getValue(), that.getValue()); 56 } 57 return false; 58 } 59 60 @Override hashCode()61 public int hashCode() { 62 K k = getKey(); 63 V v = getValue(); 64 return ((k == null) ? 0 : k.hashCode()) ^ ((v == null) ? 0 : v.hashCode()); 65 } 66 67 /** Returns a string representation of the form {@code {key}={value}}. */ 68 @Override toString()69 public String toString() { 70 return getKey() + "=" + getValue(); 71 } 72 } 73