01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.util;
17:
18: import static java.util.Utility.equalsWithNullCheck;
19:
20: /**
21: * An {@link Map.Entry} shared by several {@link Map} implementations.
22: */
23: class MapEntryImpl<K, V> implements Map.Entry<K, V> {
24:
25: /**
26: * Helper method for constructing Map.Entry objects from JSNI code.
27: */
28: static <K, V> Map.Entry<K, V> create(K key, V value) {
29: return new MapEntryImpl<K, V>(key, value);
30: }
31:
32: private K key;
33:
34: private V value;
35:
36: /**
37: * Constructor for <code>MapEntryImpl</code>.
38: */
39: public MapEntryImpl(K key, V value) {
40: this .key = key;
41: this .value = value;
42: }
43:
44: @Override
45: public boolean equals(Object other) {
46: if (other instanceof Map.Entry) {
47: Map.Entry<?, ?> entry = (Map.Entry<?, ?>) other;
48: if (equalsWithNullCheck(key, entry.getKey())
49: && equalsWithNullCheck(value, entry.getValue())) {
50: return true;
51: }
52: }
53: return false;
54: }
55:
56: public K getKey() {
57: return key;
58: }
59:
60: public V getValue() {
61: return value;
62: }
63:
64: /**
65: * Calculate the hash code using Sun's specified algorithm.
66: */
67: @Override
68: public int hashCode() {
69: int keyHash = 0;
70: int valueHash = 0;
71: if (key != null) {
72: keyHash = key.hashCode();
73: }
74: if (value != null) {
75: valueHash = value.hashCode();
76: }
77: return keyHash ^ valueHash;
78: }
79:
80: public V setValue(V object) {
81: V old = value;
82: value = object;
83: return old;
84: }
85:
86: @Override
87: public String toString() {
88: return getKey() + "=" + getValue();
89: }
90: }
|