1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.chain.web;
18
19
20 import java.util.Map;
21
22
23 /**
24 * <p>Map.Entry implementation that can be constructed to either be read-only
25 * or not.</p>
26 *
27 * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
28 */
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 } else {
96 throw new UnsupportedOperationException();
97 }
98 }
99
100
101 /**
102 * <p>Determines if this entry is equal to the passed object.</p>
103 *
104 * @param o The object to test
105 * @return True if equal, else false
106 */
107 public boolean equals(Object o) {
108 if (o != null && o instanceof Map.Entry) {
109 Map.Entry entry = (Map.Entry)o;
110 return (this.getKey() == null ?
111 entry.getKey() == null : this.getKey().equals(entry.getKey())) &&
112 (this.getValue() == null ?
113 entry.getValue() == null : this.getValue().equals(entry.getValue()));
114 }
115 return false;
116 }
117
118
119 /**
120 * <p>Returns the hashcode for this entry.</p>
121 *
122 * @return The and'ed hashcode of the key and value
123 */
124 public int hashCode() {
125 return (this.getKey() == null ? 0 : this.getKey().hashCode()) ^
126 (this.getValue() == null ? 0 : this.getValue().hashCode());
127 }
128 }