- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathArrayTable.java
346 lines (323 loc) · 11.1 KB
/
ArrayTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing;
importjava.io.IOException;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;
importjava.util.Enumeration;
importjava.util.Hashtable;
/*
* Private storage mechanism for Action key-value pairs.
* In most cases this will be an array of alternating
* key-value pairs. As it grows larger it is scaled
* up to a Hashtable.
* <p>
* This does no synchronization, if you need thread safety synchronize on
* another object before calling this.
*
* @author Georges Saab
* @author Scott Violet
*/
classArrayTableimplementsCloneable {
// Our field for storage
privateObjecttable = null;
privatestaticfinalintARRAY_BOUNDARY = 8;
/**
* Writes the passed in ArrayTable to the passed in ObjectOutputStream.
* The data is saved as an integer indicating how many key/value
* pairs are being archived, followed by the key/value pairs. If
* <code>table</code> is null, 0 will be written to <code>s</code>.
* <p>
* This is a convenience method that ActionMap/InputMap and
* AbstractAction use to avoid having the same code in each class.
*/
staticvoidwriteArrayTable(ObjectOutputStreams, ArrayTabletable) throwsIOException {
Object[] keys;
if (table == null || (keys = table.getKeys(null)) == null) {
s.writeInt(0);
}
else {
// Determine how many keys have Serializable values, when
// done all non-null values in keys identify the Serializable
// values.
intvalidCount = 0;
for (intcounter = 0; counter < keys.length; counter++) {
Objectkey = keys[counter];
/* include in Serialization when both keys and values are Serializable */
if ( (keyinstanceofSerializable
&& table.get(key) instanceofSerializable)
||
/* include these only so that we get the appropriate exception below */
(keyinstanceofClientPropertyKey
&& ((ClientPropertyKey)key).getReportValueNotSerializable())) {
validCount++;
} else {
keys[counter] = null;
}
}
// Write ou the Serializable key/value pairs.
s.writeInt(validCount);
if (validCount > 0) {
for (Objectkey : keys) {
if (key != null) {
s.writeObject(key);
s.writeObject(table.get(key));
if (--validCount == 0) {
break;
}
}
}
}
}
}
/*
* Put the key-value pair into storage
*/
publicvoidput(Objectkey, Objectvalue){
if (table==null) {
table = newObject[] {key, value};
} else {
intsize = size();
if (size < ARRAY_BOUNDARY) { // We are an array
if (containsKey(key)) {
Object[] tmp = (Object[])table;
for (inti = 0; i<tmp.length-1; i+=2) {
if (tmp[i].equals(key)) {
tmp[i+1]=value;
break;
}
}
} else {
Object[] array = (Object[])table;
inti = array.length;
Object[] tmp = newObject[i+2];
System.arraycopy(array, 0, tmp, 0, i);
tmp[i] = key;
tmp[i+1] = value;
table = tmp;
}
} else { // We are a hashtable
if ((size==ARRAY_BOUNDARY) && isArray()) {
grow();
}
@SuppressWarnings("unchecked")
Hashtable<Object,Object> tmp = (Hashtable<Object,Object>)table;
tmp.put(key, value);
}
}
}
/*
* Gets the value for key
*/
publicObjectget(Objectkey) {
Objectvalue = null;
if (table !=null) {
if (isArray()) {
Object[] array = (Object[])table;
for (inti = 0; i<array.length-1; i+=2) {
if (array[i].equals(key)) {
value = array[i+1];
break;
}
}
} else {
value = ((Hashtable)table).get(key);
}
}
returnvalue;
}
/*
* Returns the number of pairs in storage
*/
publicintsize() {
intsize;
if (table==null)
return0;
if (isArray()) {
size = ((Object[])table).length/2;
} else {
size = ((Hashtable)table).size();
}
returnsize;
}
/*
* Returns true if we have a value for the key
*/
publicbooleancontainsKey(Objectkey) {
booleancontains = false;
if (table !=null) {
if (isArray()) {
Object[] array = (Object[])table;
for (inti = 0; i<array.length-1; i+=2) {
if (array[i].equals(key)) {
contains = true;
break;
}
}
} else {
contains = ((Hashtable)table).containsKey(key);
}
}
returncontains;
}
/*
* Removes the key and its value
* Returns the value for the pair removed
*/
publicObjectremove(Objectkey){
Objectvalue = null;
if (key==null) {
returnnull;
}
if (table !=null) {
if (isArray()){
// Is key on the list?
intindex = -1;
Object[] array = (Object[])table;
for (inti = array.length-2; i>=0; i-=2) {
if (array[i].equals(key)) {
index = i;
value = array[i+1];
break;
}
}
// If so, remove it
if (index != -1) {
Object[] tmp = newObject[array.length-2];
// Copy the list up to index
System.arraycopy(array, 0, tmp, 0, index);
// Copy from two past the index, up to
// the end of tmp (which is two elements
// shorter than the old list)
if (index < tmp.length)
System.arraycopy(array, index+2, tmp, index,
tmp.length - index);
// set the listener array to the new array or null
table = (tmp.length == 0) ? null : tmp;
}
} else {
value = ((Hashtable)table).remove(key);
}
if (size()==ARRAY_BOUNDARY - 1 && !isArray()) {
shrink();
}
}
returnvalue;
}
/**
* Removes all the mappings.
*/
publicvoidclear() {
table = null;
}
/*
* Returns a clone of the <code>ArrayTable</code>.
*/
publicObjectclone() {
ArrayTablenewArrayTable = newArrayTable();
if (table != null) {
if (isArray()) {
Object[] array = (Object[]) table;
for (inti = 0; i < array.length - 1; i += 2) {
newArrayTable.put(array[i], array[i + 1]);
}
} else {
Hashtable<?, ?> tmp = (Hashtable) table;
Enumeration<?> keys = tmp.keys();
while (keys.hasMoreElements()) {
Objecto = keys.nextElement();
newArrayTable.put(o, tmp.get(o));
}
}
}
returnnewArrayTable;
}
/**
* Returns the keys of the table, or <code>null</code> if there
* are currently no bindings.
* @param keys array of keys
* @return an array of bindings
*/
publicObject[] getKeys(Object[] keys) {
if (table == null) {
returnnull;
}
if (isArray()) {
Object[] array = (Object[])table;
if (keys == null) {
keys = newObject[array.length / 2];
}
for (inti = 0, index = 0 ;i < array.length-1 ; i+=2,
index++) {
keys[index] = array[i];
}
} else {
Hashtable<?,?> tmp = (Hashtable)table;
Enumeration<?> enum_ = tmp.keys();
intcounter = tmp.size();
if (keys == null) {
keys = newObject[counter];
}
while (counter > 0) {
keys[--counter] = enum_.nextElement();
}
}
returnkeys;
}
/*
* Returns true if the current storage mechanism is
* an array of alternating key-value pairs.
*/
privatebooleanisArray(){
return (tableinstanceofObject[]);
}
/*
* Grows the storage from an array to a hashtable.
*/
privatevoidgrow() {
Object[] array = (Object[])table;
Hashtable<Object, Object> tmp = newHashtable<Object, Object>(array.length/2);
for (inti = 0; i<array.length; i+=2) {
tmp.put(array[i], array[i+1]);
}
table = tmp;
}
/*
* Shrinks the storage from a hashtable to an array.
*/
privatevoidshrink() {
Hashtable<?,?> tmp = (Hashtable)table;
Object[] array = newObject[tmp.size()*2];
Enumeration<?> keys = tmp.keys();
intj = 0;
while (keys.hasMoreElements()) {
Objecto = keys.nextElement();
array[j] = o;
array[j+1] = tmp.get(o);
j+=2;
}
table = array;
}
}