Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MultiKey |
|
| 5.142857142857143;5.143 |
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.util; | |
16 | ||
17 | import org.apache.tapestry.Tapestry; | |
18 | ||
19 | import java.io.Externalizable; | |
20 | import java.io.IOException; | |
21 | import java.io.ObjectInput; | |
22 | import java.io.ObjectOutput; | |
23 | ||
24 | /** | |
25 | * A complex key that may be used as an alternative to nested {@link java.util.Map}s. | |
26 | * | |
27 | * @author Howard Lewis Ship | |
28 | */ | |
29 | ||
30 | public class MultiKey implements Externalizable | |
31 | { | |
32 | ||
33 | /** | |
34 | * @since 2.0.4 | |
35 | */ | |
36 | ||
37 | private static final long serialVersionUID = 4465448607415788806L; | |
38 | ||
39 | private static final int HASH_CODE_UNSET = -1; | |
40 | ||
41 | 0 | private transient int hashCode = HASH_CODE_UNSET; |
42 | ||
43 | private Object[] _keys; | |
44 | ||
45 | /** | |
46 | * Public no-arguments constructor needed to be compatible with | |
47 | * {@link Externalizable}; this leaves the new MultiKey in a non-usable | |
48 | * state and shouldn't be used by user code. | |
49 | */ | |
50 | ||
51 | public MultiKey() | |
52 | 0 | { |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Builds a <code>MultiKey</code> from an array of keys. If the array is | |
57 | * not copied, then it must not be modified. | |
58 | * | |
59 | * @param keys | |
60 | * The components of the key. | |
61 | * @param makeCopy | |
62 | * If true, a copy of the keys is created. If false, the keys are | |
63 | * simple retained by the <code>MultiKey</code>. | |
64 | * @throws IllegalArgumentException | |
65 | * if keys is null, of if the first element of keys is null. | |
66 | */ | |
67 | ||
68 | public MultiKey(Object[] keys, boolean makeCopy) | |
69 | { | |
70 | 0 | super(); |
71 | ||
72 | 0 | if (keys == null || keys.length == 0) |
73 | 0 | throw new IllegalArgumentException(Tapestry.getMessage("MultiKey.null-keys")); |
74 | ||
75 | 0 | if (keys[0] == null) |
76 | 0 | throw new IllegalArgumentException(Tapestry.getMessage("MultiKey.first-element-may-not-be-null")); |
77 | ||
78 | 0 | if (makeCopy) |
79 | { | |
80 | 0 | _keys = new Object[keys.length]; |
81 | 0 | System.arraycopy(keys, 0, this._keys, 0, keys.length); |
82 | } | |
83 | else | |
84 | 0 | _keys = keys; |
85 | 0 | } |
86 | ||
87 | /** | |
88 | * Returns true if. : | |
89 | * <ul> | |
90 | * <li>The other object is a <code>MultiKey</code> | |
91 | * <li>They have the same number of key elements | |
92 | * <li>Every element is an exact match or is equal | |
93 | * </ul> | |
94 | */ | |
95 | ||
96 | public boolean equals(Object other) | |
97 | { | |
98 | int i; | |
99 | ||
100 | 0 | if (other == null) |
101 | 0 | return false; |
102 | ||
103 | 0 | if (_keys == null) |
104 | 0 | throw new IllegalStateException(Tapestry.getMessage("MultiKey.no-keys")); |
105 | ||
106 | // Would a hashCode check be worthwhile here? | |
107 | ||
108 | try | |
109 | { | |
110 | 0 | MultiKey otherMulti = (MultiKey) other; |
111 | ||
112 | 0 | if (_keys.length != otherMulti._keys.length) return false; |
113 | ||
114 | 0 | for(i = 0; i < _keys.length; i++) |
115 | { | |
116 | // On an exact match, continue. This means that null matches | |
117 | // null. | |
118 | ||
119 | 0 | if (_keys[i] == otherMulti._keys[i]) |
120 | 0 | continue; |
121 | ||
122 | // If either is null, but not both, then | |
123 | // not a match. | |
124 | ||
125 | 0 | if (_keys[i] == null || otherMulti._keys[i] == null) |
126 | 0 | return false; |
127 | ||
128 | 0 | if (!_keys[i].equals(otherMulti._keys[i])) |
129 | 0 | return false; |
130 | } | |
131 | ||
132 | // Every key equal. A match. | |
133 | ||
134 | 0 | return true; |
135 | } | |
136 | 0 | catch (ClassCastException e) |
137 | { | |
138 | } | |
139 | ||
140 | 0 | return false; |
141 | } | |
142 | ||
143 | /** | |
144 | * Returns the hash code of the receiver, which is computed from all the | |
145 | * non-null key elements. This value is computed once and then cached, so | |
146 | * elements should not change their hash codes once created (note that this | |
147 | * is the same constraint that would be used if the individual key elements | |
148 | * were themselves {@link java.util.Map} keys. | |
149 | */ | |
150 | ||
151 | public int hashCode() | |
152 | { | |
153 | 0 | if (hashCode == HASH_CODE_UNSET) |
154 | { | |
155 | 0 | hashCode = _keys[0].hashCode(); |
156 | ||
157 | 0 | for(int i = 1; i < _keys.length; i++) |
158 | { | |
159 | 0 | if (_keys[i] != null) hashCode ^= _keys[i].hashCode(); |
160 | } | |
161 | } | |
162 | ||
163 | 0 | return hashCode; |
164 | } | |
165 | ||
166 | /** | |
167 | * Identifies all the keys stored by this <code>MultiKey</code>. | |
168 | */ | |
169 | ||
170 | public String toString() | |
171 | { | |
172 | StringBuffer buffer; | |
173 | int i; | |
174 | ||
175 | 0 | buffer = new StringBuffer("MultiKey["); |
176 | ||
177 | 0 | for(i = 0; i < _keys.length; i++) |
178 | { | |
179 | 0 | if (i > 0) buffer.append(", "); |
180 | ||
181 | 0 | if (_keys[i] == null) |
182 | 0 | buffer.append("<null>"); |
183 | 0 | else buffer.append(_keys[i]); |
184 | } | |
185 | ||
186 | 0 | buffer.append(']'); |
187 | ||
188 | 0 | return buffer.toString(); |
189 | } | |
190 | ||
191 | /** | |
192 | * Writes a count of the keys, then writes each individual key. | |
193 | */ | |
194 | ||
195 | public void writeExternal(ObjectOutput out) | |
196 | throws IOException | |
197 | { | |
198 | 0 | out.writeInt(_keys.length); |
199 | ||
200 | 0 | for(int i = 0; i < _keys.length; i++) |
201 | 0 | out.writeObject(_keys[i]); |
202 | 0 | } |
203 | ||
204 | /** | |
205 | * Reads the state previously written by | |
206 | * {@link #writeExternal(ObjectOutput)}. | |
207 | */ | |
208 | ||
209 | public void readExternal(ObjectInput in) | |
210 | throws IOException, ClassNotFoundException | |
211 | { | |
212 | int count; | |
213 | ||
214 | 0 | count = in.readInt(); |
215 | 0 | _keys = new Object[count]; |
216 | ||
217 | 0 | for(int i = 0; i < count; i++) |
218 | 0 | _keys[i] = in.readObject(); |
219 | 0 | } |
220 | } |