KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > ArrayTable


1 /*
2  * @(#)ArrayTable.java 1.4 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.io.IOException JavaDoc;
10 import java.io.ObjectOutputStream JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Hashtable JavaDoc;
14
15 /*
16  * Private storage mechanism for Action key-value pairs.
17  * In most cases this will be an array of alternating
18  * key-value pairs. As it grows larger it is scaled
19  * up to a Hashtable.
20  * <p>
21  * This does no synchronization, if you need thread safety synchronize on
22  * another object before calling this.
23  *
24  * @version 1.4 12/19/03
25  * @author Georges Saab
26  * @author Scott Violet
27  */

28 class ArrayTable implements Cloneable JavaDoc {
29     // Our field for storage
30
private Object JavaDoc table = null;
31     private static final int ARRAY_BOUNDARY = 8;
32
33
34     /**
35      * Writes the passed in ArrayTable to the passed in ObjectOutputStream.
36      * The data is saved as an integer indicating how many key/value
37      * pairs are being archived, followed by the the key/value pairs. If
38      * <code>table</code> is null, 0 will be written to <code>s</code>.
39      * <p>
40      * This is a convenience method that ActionMap/InputMap and
41      * AbstractAction use to avoid having the same code in each class.
42      */

43     static void writeArrayTable(ObjectOutputStream JavaDoc s, ArrayTable JavaDoc table) throws IOException JavaDoc {
44         Object JavaDoc keys[];
45
46         if (table == null || (keys = table.getKeys(null)) == null) {
47             s.writeInt(0);
48         }
49         else {
50             // Determine how many keys have Serializable values, when
51
// done all non-null values in keys identify the Serializable
52
// values.
53
int validCount = 0;
54
55             for (int counter = 0; counter < keys.length; counter++) {
56                 if ((keys[counter] instanceof Serializable JavaDoc) &&
57                     (table.get(keys[counter]) instanceof Serializable JavaDoc)) {
58                     validCount++;
59                 }
60                 else {
61                     keys[counter] = null;
62                 }
63             }
64             // Write ou the Serializable key/value pairs.
65
s.writeInt(validCount);
66             if (validCount > 0) {
67                 for (int counter = 0; counter < keys.length; counter++) {
68                     if (keys[counter] != null) {
69                         s.writeObject(keys[counter]);
70                         s.writeObject(table.get(keys[counter]));
71                         if (--validCount == 0) {
72                             break;
73                         }
74                     }
75                 }
76             }
77         }
78     }
79
80
81     /*
82      * Put the key-value pair into storage
83      */

84     public void put(Object JavaDoc key, Object JavaDoc value){
85         if (table==null) {
86             table = new Object JavaDoc[] {key, value};
87         } else {
88             int size = size();
89             if (size < ARRAY_BOUNDARY) { // We are an array
90
if (containsKey(key)) {
91                     Object JavaDoc[] tmp = (Object JavaDoc[])table;
92                     for (int i = 0; i<tmp.length-1; i+=2) {
93                         if (tmp[i].equals(key)) {
94                             tmp[i+1]=value;
95                             break;
96                         }
97                     }
98                 } else {
99                     Object JavaDoc[] array = (Object JavaDoc[])table;
100                     int i = array.length;
101                     Object JavaDoc[] tmp = new Object JavaDoc[i+2];
102                     System.arraycopy(array, 0, tmp, 0, i);
103             
104                     tmp[i] = key;
105                     tmp[i+1] = value;
106                     table = tmp;
107                 }
108             } else { // We are a hashtable
109
if ((size==ARRAY_BOUNDARY) && isArray()) {
110                     grow();
111                 }
112                 ((Hashtable JavaDoc)table).put(key, value);
113             }
114         }
115     }
116     
117     /*
118      * Gets the value for key
119      */

120     public Object JavaDoc get(Object JavaDoc key) {
121         Object JavaDoc value = null;
122         if (table !=null) {
123             if (isArray()) {
124                 Object JavaDoc[] array = (Object JavaDoc[])table;
125                 for (int i = 0; i<array.length-1; i+=2) {
126                     if (array[i].equals(key)) {
127                         value = array[i+1];
128                         break;
129                     }
130                 }
131             } else {
132                 value = ((Hashtable JavaDoc)table).get(key);
133             }
134         }
135         return value;
136     }
137     
138     /*
139      * Returns the number of pairs in storage
140      */

141     public int size() {
142         int size;
143         if (table==null)
144             return 0;
145         if (isArray()) {
146             size = ((Object JavaDoc[])table).length/2;
147         } else {
148             size = ((Hashtable JavaDoc)table).size();
149         }
150         return size;
151     }
152     
153     /*
154      * Returns true if we have a value for the key
155      */

156     public boolean containsKey(Object JavaDoc key) {
157         boolean contains = false;
158         if (table !=null) {
159             if (isArray()) {
160                 Object JavaDoc[] array = (Object JavaDoc[])table;
161                 for (int i = 0; i<array.length-1; i+=2) {
162                     if (array[i].equals(key)) {
163                         contains = true;
164                         break;
165                     }
166                 }
167             } else {
168                 contains = ((Hashtable JavaDoc)table).containsKey(key);
169             }
170         }
171         return contains;
172     }
173     
174     /*
175      * Removes the key and its value
176      * Returns the value for the pair removed
177      */

178     public Object JavaDoc remove(Object JavaDoc key){
179         Object JavaDoc value = null;
180         if (key==null) {
181             return null;
182         }
183         if (table !=null) {
184             if (isArray()){
185                 // Is key on the list?
186
int index = -1;
187                 Object JavaDoc[] array = (Object JavaDoc[])table;
188                 for (int i = array.length-2; i>=0; i-=2) {
189                     if (array[i].equals(key)) {
190                         index = i;
191                         value = array[i+1];
192                         break;
193                     }
194                 }
195             
196                 // If so, remove it
197
if (index != -1) {
198                     Object JavaDoc[] tmp = new Object JavaDoc[array.length-2];
199                     // Copy the list up to index
200
System.arraycopy(array, 0, tmp, 0, index);
201                     // Copy from two past the index, up to
202
// the end of tmp (which is two elements
203
// shorter than the old list)
204
if (index < tmp.length)
205                         System.arraycopy(array, index+2, tmp, index,
206                                          tmp.length - index);
207                     // set the listener array to the new array or null
208
table = (tmp.length == 0) ? null : tmp;
209                 }
210             } else {
211                 value = ((Hashtable JavaDoc)table).remove(key);
212             }
213             if (size()==ARRAY_BOUNDARY - 1 && !isArray()) {
214                 shrink();
215             }
216         }
217         return value;
218     }
219
220     /**
221      * Removes all the mappings.
222      */

223     public void clear() {
224         table = null;
225     }
226
227     /*
228      * Returns a clone of the <code>ArrayTable</code>.
229      */

230     public Object JavaDoc clone() {
231         ArrayTable JavaDoc newArrayTable = new ArrayTable JavaDoc();
232         if (isArray()) {
233             Object JavaDoc[] array = (Object JavaDoc[])table;
234             for (int i = 0 ;i < array.length-1 ; i+=2) {
235                 newArrayTable.put(array[i], array[i+1]);
236             }
237         } else {
238             Hashtable JavaDoc tmp = (Hashtable JavaDoc)table;
239             Enumeration JavaDoc keys = tmp.keys();
240             while (keys.hasMoreElements()) {
241                 Object JavaDoc o = keys.nextElement();
242                 newArrayTable.put(o,tmp.get(o));
243             }
244         }
245         return newArrayTable;
246     }
247
248     /**
249      * Returns the keys of the table, or <code>null</code> if there
250      * are currently no bindings.
251      * @param keys array of keys
252      * @return an array of bindings
253      */

254     public Object JavaDoc[] getKeys(Object JavaDoc[] keys) {
255         if (table == null) {
256             return null;
257         }
258         if (isArray()) {
259             Object JavaDoc[] array = (Object JavaDoc[])table;
260             if (keys == null) {
261                 keys = new Object JavaDoc[array.length / 2];
262             }
263             for (int i = 0, index = 0 ;i < array.length-1 ; i+=2,
264                      index++) {
265                 keys[index] = array[i];
266             }
267         } else {
268             Hashtable JavaDoc tmp = (Hashtable JavaDoc)table;
269             Enumeration JavaDoc enum_ = tmp.keys();
270             int counter = tmp.size();
271             if (keys == null) {
272                 keys = new Object JavaDoc[counter];
273             }
274             while (counter > 0) {
275                 keys[--counter] = enum_.nextElement();
276             }
277         }
278         return keys;
279     }
280
281     /*
282      * Returns true if the current storage mechanism is
283      * an array of alternating key-value pairs.
284      */

285     private boolean isArray(){
286         return (table instanceof Object JavaDoc[]);
287     }
288
289     /*
290      * Grows the storage from an array to a hashtable.
291      */

292     private void grow() {
293         Object JavaDoc[] array = (Object JavaDoc[])table;
294         Hashtable JavaDoc tmp = new Hashtable JavaDoc(array.length/2);
295         for (int i = 0; i<array.length; i+=2) {
296             tmp.put(array[i], array[i+1]);
297         }
298         table = tmp;
299     }
300     
301     /*
302      * Shrinks the storage from a hashtable to an array.
303      */

304     private void shrink() {
305         Hashtable JavaDoc tmp = (Hashtable JavaDoc)table;
306         Object JavaDoc[] array = new Object JavaDoc[tmp.size()*2];
307         Enumeration JavaDoc keys = tmp.keys();
308         int j = 0;
309     
310         while (keys.hasMoreElements()) {
311             Object JavaDoc o = keys.nextElement();
312             array[j] = o;
313             array[j+1] = tmp.get(o);
314             j+=2;
315         }
316         table = array;
317     }
318 }
319
Popular Tags