|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.util; |
|
16 |
| |
|
17 |
| import java.io.Externalizable; |
|
18 |
| import java.io.IOException; |
|
19 |
| import java.io.ObjectInput; |
|
20 |
| import java.io.ObjectOutput; |
|
21 |
| |
|
22 |
| import org.apache.tapestry.Tapestry; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public class MultiKey implements Externalizable |
|
33 |
| { |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| private static final long serialVersionUID = 4465448607415788806L; |
|
40 |
| |
|
41 |
| private static final int HASH_CODE_UNSET = -1; |
|
42 |
| |
|
43 |
| private transient int hashCode = HASH_CODE_UNSET; |
|
44 |
| |
|
45 |
| private Object[] keys; |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
0
| public MultiKey()
|
|
55 |
| { |
|
56 |
| } |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
1668
| public MultiKey(Object[] keys, boolean makeCopy)
|
|
72 |
| { |
|
73 |
1668
| super();
|
|
74 |
| |
|
75 |
1668
| if (keys == null || keys.length == 0)
|
|
76 |
0
| throw new IllegalArgumentException(Tapestry.getMessage("MultiKey.null-keys"));
|
|
77 |
| |
|
78 |
1668
| if (keys[0] == null)
|
|
79 |
0
| throw new IllegalArgumentException(Tapestry.getMessage("MultiKey.first-element-may-not-be-null"));
|
|
80 |
| |
|
81 |
1668
| if (makeCopy)
|
|
82 |
| { |
|
83 |
0
| this.keys = new Object[keys.length];
|
|
84 |
0
| System.arraycopy(keys, 0, this.keys, 0, keys.length);
|
|
85 |
| } |
|
86 |
| else |
|
87 |
1668
| this.keys = keys;
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
612
| public boolean equals(Object other)
|
|
101 |
| { |
|
102 |
612
| int i;
|
|
103 |
| |
|
104 |
612
| if (other == null)
|
|
105 |
0
| return false;
|
|
106 |
| |
|
107 |
612
| if (keys == null)
|
|
108 |
0
| throw new IllegalStateException(Tapestry.getMessage("MultiKey.no-keys"));
|
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
612
| try
|
|
113 |
| { |
|
114 |
612
| MultiKey otherMulti = (MultiKey) other;
|
|
115 |
| |
|
116 |
612
| if (keys.length != otherMulti.keys.length)
|
|
117 |
0
| return false;
|
|
118 |
| |
|
119 |
612
| for (i = 0; i < keys.length; i++)
|
|
120 |
| { |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
1101
| if (keys[i] == otherMulti.keys[i])
|
|
125 |
807
| continue;
|
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
294
| if (keys[i] == null || otherMulti.keys[i] == null)
|
|
131 |
0
| return false;
|
|
132 |
| |
|
133 |
294
| if (!keys[i].equals(otherMulti.keys[i]))
|
|
134 |
123
| return false;
|
|
135 |
| |
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
489
| return true;
|
|
141 |
| } |
|
142 |
| catch (ClassCastException e) |
|
143 |
| { |
|
144 |
| } |
|
145 |
| |
|
146 |
0
| return false;
|
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
2478
| public int hashCode()
|
|
162 |
| { |
|
163 |
2478
| if (hashCode == HASH_CODE_UNSET)
|
|
164 |
| { |
|
165 |
1668
| hashCode = keys[0].hashCode();
|
|
166 |
| |
|
167 |
1668
| for (int i = 1; i < keys.length; i++)
|
|
168 |
| { |
|
169 |
1668
| if (keys[i] != null)
|
|
170 |
1668
| hashCode ^= keys[i].hashCode();
|
|
171 |
| } |
|
172 |
| } |
|
173 |
| |
|
174 |
2478
| return hashCode;
|
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
93
| public String toString()
|
|
183 |
| { |
|
184 |
93
| StringBuffer buffer;
|
|
185 |
93
| int i;
|
|
186 |
| |
|
187 |
93
| buffer = new StringBuffer("MultiKey[");
|
|
188 |
| |
|
189 |
93
| for (i = 0; i < keys.length; i++)
|
|
190 |
| { |
|
191 |
186
| if (i > 0)
|
|
192 |
93
| buffer.append(", ");
|
|
193 |
| |
|
194 |
186
| if (keys[i] == null)
|
|
195 |
0
| buffer.append("<null>");
|
|
196 |
| else |
|
197 |
186
| buffer.append(keys[i]);
|
|
198 |
| } |
|
199 |
| |
|
200 |
93
| buffer.append(']');
|
|
201 |
| |
|
202 |
93
| return buffer.toString();
|
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
0
| public void writeExternal(ObjectOutput out) throws IOException
|
|
211 |
| { |
|
212 |
0
| out.writeInt(keys.length);
|
|
213 |
| |
|
214 |
0
| for (int i = 0; i < keys.length; i++)
|
|
215 |
0
| out.writeObject(keys[i]);
|
|
216 |
| } |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
0
| public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
|
|
224 |
| { |
|
225 |
0
| int count;
|
|
226 |
| |
|
227 |
0
| count = in.readInt();
|
|
228 |
0
| keys = new Object[count];
|
|
229 |
| |
|
230 |
0
| for (int i = 0; i < count; i++)
|
|
231 |
0
| keys[i] = in.readObject();
|
|
232 |
| } |
|
233 |
| } |