1 7 package javax.swing; 8 9 import java.io.IOException ; 10 import java.io.ObjectOutputStream ; 11 import java.io.Serializable ; 12 import java.util.Enumeration ; 13 import java.util.Hashtable ; 14 15 28 class ArrayTable implements Cloneable { 29 private Object table = null; 31 private static final int ARRAY_BOUNDARY = 8; 32 33 34 43 static void writeArrayTable(ObjectOutputStream s, ArrayTable table) throws IOException { 44 Object keys[]; 45 46 if (table == null || (keys = table.getKeys(null)) == null) { 47 s.writeInt(0); 48 } 49 else { 50 int validCount = 0; 54 55 for (int counter = 0; counter < keys.length; counter++) { 56 if ((keys[counter] instanceof Serializable ) && 57 (table.get(keys[counter]) instanceof Serializable )) { 58 validCount++; 59 } 60 else { 61 keys[counter] = null; 62 } 63 } 64 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 84 public void put(Object key, Object value){ 85 if (table==null) { 86 table = new Object [] {key, value}; 87 } else { 88 int size = size(); 89 if (size < ARRAY_BOUNDARY) { if (containsKey(key)) { 91 Object [] tmp = (Object [])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 [] array = (Object [])table; 100 int i = array.length; 101 Object [] tmp = new Object [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 { if ((size==ARRAY_BOUNDARY) && isArray()) { 110 grow(); 111 } 112 ((Hashtable )table).put(key, value); 113 } 114 } 115 } 116 117 120 public Object get(Object key) { 121 Object value = null; 122 if (table !=null) { 123 if (isArray()) { 124 Object [] array = (Object [])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 )table).get(key); 133 } 134 } 135 return value; 136 } 137 138 141 public int size() { 142 int size; 143 if (table==null) 144 return 0; 145 if (isArray()) { 146 size = ((Object [])table).length/2; 147 } else { 148 size = ((Hashtable )table).size(); 149 } 150 return size; 151 } 152 153 156 public boolean containsKey(Object key) { 157 boolean contains = false; 158 if (table !=null) { 159 if (isArray()) { 160 Object [] array = (Object [])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 )table).containsKey(key); 169 } 170 } 171 return contains; 172 } 173 174 178 public Object remove(Object key){ 179 Object value = null; 180 if (key==null) { 181 return null; 182 } 183 if (table !=null) { 184 if (isArray()){ 185 int index = -1; 187 Object [] array = (Object [])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 (index != -1) { 198 Object [] tmp = new Object [array.length-2]; 199 System.arraycopy(array, 0, tmp, 0, index); 201 if (index < tmp.length) 205 System.arraycopy(array, index+2, tmp, index, 206 tmp.length - index); 207 table = (tmp.length == 0) ? null : tmp; 209 } 210 } else { 211 value = ((Hashtable )table).remove(key); 212 } 213 if (size()==ARRAY_BOUNDARY - 1 && !isArray()) { 214 shrink(); 215 } 216 } 217 return value; 218 } 219 220 223 public void clear() { 224 table = null; 225 } 226 227 230 public Object clone() { 231 ArrayTable newArrayTable = new ArrayTable (); 232 if (isArray()) { 233 Object [] array = (Object [])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 tmp = (Hashtable )table; 239 Enumeration keys = tmp.keys(); 240 while (keys.hasMoreElements()) { 241 Object o = keys.nextElement(); 242 newArrayTable.put(o,tmp.get(o)); 243 } 244 } 245 return newArrayTable; 246 } 247 248 254 public Object [] getKeys(Object [] keys) { 255 if (table == null) { 256 return null; 257 } 258 if (isArray()) { 259 Object [] array = (Object [])table; 260 if (keys == null) { 261 keys = new Object [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 tmp = (Hashtable )table; 269 Enumeration enum_ = tmp.keys(); 270 int counter = tmp.size(); 271 if (keys == null) { 272 keys = new Object [counter]; 273 } 274 while (counter > 0) { 275 keys[--counter] = enum_.nextElement(); 276 } 277 } 278 return keys; 279 } 280 281 285 private boolean isArray(){ 286 return (table instanceof Object []); 287 } 288 289 292 private void grow() { 293 Object [] array = (Object [])table; 294 Hashtable tmp = new Hashtable (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 304 private void shrink() { 305 Hashtable tmp = (Hashtable )table; 306 Object [] array = new Object [tmp.size()*2]; 307 Enumeration keys = tmp.keys(); 308 int j = 0; 309 310 while (keys.hasMoreElements()) { 311 Object 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 |