1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.context.support;
17
18
19 import java.util.Map;
20
21
22 /**
23 * <p>Map.Entry implementation that can be constructed to either be read-only
24 * or not.</p>
25 *
26 * <p>This class was copied from Jakarta Commons Chain.</p>
27 *
28 * @version $Revision: 1.1 $ $Date: 2005/01/10 05:28:48 $
29 */
30 public class MapEntry implements Map.Entry {
31
32
33 /**
34 * <p>The entry key.</p>
35 */
36 private Object key;
37
38 /**
39 * <p>The entry value.</p>
40 */
41 private Object value;
42
43 /**
44 * <p>Whether the entry can be modified.</p>
45 */
46 private boolean modifiable = false;
47
48
49 /**
50 * <p>Creates a map entry that can either allow modifications or not.</p>
51 *
52 * @param key The entry key
53 * @param value The entry value
54 * @param modifiable Whether the entry should allow modification or not
55 */
56 public MapEntry(Object key, Object value, boolean modifiable) {
57 this.key = key;
58 this.value = value;
59 this.modifiable = modifiable;
60 }
61
62
63 /**
64 * <p>Gets the entry key.</p>
65 *
66 * @return The entry key
67 */
68 public Object getKey() {
69 return key;
70 }
71
72
73 /**
74 * <p>Gets the entry value.</p>
75 *
76 * @return The entry key
77 */
78 public Object getValue() {
79 return value;
80 }
81
82
83 /**
84 * <p>Sets the entry value if the entry can be modified.</p>
85 *
86 * @param val The new value
87 * @return The old entry value
88 * @throws UnsupportedOperationException If the entry cannot be modified
89 */
90 public Object setValue(Object val) {
91 if (modifiable) {
92 Object oldVal = this.value;
93 this.value = val;
94 return oldVal;
95 }
96 throw new UnsupportedOperationException();
97 }
98
99
100 /**
101 * <p>Determines if this entry is equal to the passed object.</p>
102 *
103 * @param o The object to test
104 * @return True if equal, else false
105 */
106 public boolean equals(Object o) {
107 if (o != null && o instanceof Map.Entry) {
108 Map.Entry entry = (Map.Entry)o;
109 return (this.getKey()==null ?
110 entry.getKey()==null : this.getKey().equals(entry.getKey())) &&
111 (this.getValue()==null ?
112 entry.getValue()==null : this.getValue().equals(entry.getValue()));
113 }
114 return false;
115 }
116
117
118 /**
119 * <p>Returns the hashcode for this entry.</p>
120 *
121 * @return The and'ed hashcode of the key and value
122 */
123 public int hashCode() {
124 return (this.getKey()==null ? 0 : this.getKey().hashCode()) ^
125 (this.getValue()==null ? 0 : this.getValue().hashCode());
126 }
127 }