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