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