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