1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections.keyvalue;
18
19 import java.util.Map;
20
21 /***
22 * Abstract Pair class to assist with creating correct Map Entry implementations.
23 *
24 * @since Commons Collections 3.0
25 * @version $Revision: 438363 $ $Date: 2006-08-30 05:48:00 +0100 (Wed, 30 Aug 2006) $
26 *
27 * @author James Strachan
28 * @author Michael A. Smith
29 * @author Neil O'Toole
30 * @author Stephen Colebourne
31 */
32 public abstract class AbstractMapEntry extends AbstractKeyValue implements Map.Entry {
33
34 /***
35 * Constructs a new entry with the given key and given value.
36 *
37 * @param key the key for the entry, may be null
38 * @param value the value for the entry, may be null
39 */
40 protected AbstractMapEntry(Object key, Object value) {
41 super(key, value);
42 }
43
44
45
46 /***
47 * Sets the value stored in this Map Entry.
48 * <p>
49 * This Map Entry is not connected to a Map, so only the local data is changed.
50 *
51 * @param value the new value
52 * @return the previous value
53 */
54 public Object setValue(Object value) {
55 Object answer = this.value;
56 this.value = value;
57 return answer;
58 }
59
60 /***
61 * Compares this Map Entry with another Map Entry.
62 * <p>
63 * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
64 *
65 * @param obj the object to compare to
66 * @return true if equal key and value
67 */
68 public boolean equals(Object obj) {
69 if (obj == this) {
70 return true;
71 }
72 if (obj instanceof Map.Entry == false) {
73 return false;
74 }
75 Map.Entry other = (Map.Entry) obj;
76 return
77 (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
78 (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
79 }
80
81 /***
82 * Gets a hashCode compatible with the equals method.
83 * <p>
84 * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
85 *
86 * @return a suitable hash code
87 */
88 public int hashCode() {
89 return (getKey() == null ? 0 : getKey().hashCode()) ^
90 (getValue() == null ? 0 : getValue().hashCode());
91 }
92
93 }