/*
* @(#)ArrayTable.java 1.5 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package javax.swing;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Enumeration;
import java.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.
*
* @version 1.5 11/17/05
* @author Georges Saab
* @author Scott Violet
*/
class ArrayTable implements Cloneable {
// Our field for storage
private Object table = null;
private static final int ARRAY_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 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.
*/
static void writeArrayTable(ObjectOutputStream s, ArrayTable table) throws IOException {
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.
int validCount = 0;
for (int counter = 0; counter < keys.length; counter++) {
if ((keys[counter] instanceof Serializable) &&
(table.get(keys[counter]) instanceof Serializable)) {
validCount++;
}
else {
keys[counter] = null;
}
}
// Write ou the Serializable key/value pairs.
s.writeInt(validCount);
if (validCount > 0) {
for (int counter = 0; counter < keys.length; counter++) {
if (keys[counter] != null) {
s.writeObject(keys[counter]);
s.writeObject(table.get(keys[counter]));
if (--validCount == 0) {
break;
}
}
}
}
}
}
/*
* Put the key-value pair into storage
*/
public void put(Object key, Object value){
if (table==null) {
table = new Object[] {key, value};
} else {
int size = size();
if (size < ARRAY_BOUNDARY) { // We are an array
if (containsKey(key)) {
Object[] tmp = (Object[])table;
for (int i = 0; i<tmp.length-1; i+=2) {
if (tmp[i].equals(key)) {
tmp[i+1]=value;
break;
}
}
} else {
Object[] array = (Object[])table;
int i = array.length;
=1= |