jdk/src/java.desktop/share/classes/javax/swing/ArrayTable.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 11268
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Private storage mechanism for Action key-value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * In most cases this will be an array of alternating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * key-value pairs.  As it grows larger it is scaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * up to a Hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This does no synchronization, if you need thread safety synchronize on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * another object before calling this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class ArrayTable implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Our field for storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private Object table = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int ARRAY_BOUNDARY = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Writes the passed in ArrayTable to the passed in ObjectOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * The data is saved as an integer indicating how many key/value
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    54
     * pairs are being archived, followed by the key/value pairs. If
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <code>table</code> is null, 0 will be written to <code>s</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * This is a convenience method that ActionMap/InputMap and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * AbstractAction use to avoid having the same code in each class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static void writeArrayTable(ObjectOutputStream s, ArrayTable table) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Object keys[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (table == null || (keys = table.getKeys(null)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            s.writeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // Determine how many keys have Serializable values, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // done all non-null values in keys identify the Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            // values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            int validCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            for (int counter = 0; counter < keys.length; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                Object key = keys[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                /* include in Serialization when both keys and values are Serializable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                if (    (key instanceof Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                         && table.get(key) instanceof Serializable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                             ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                         /* include these only so that we get the appropriate exception below */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        (key instanceof ClientPropertyKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                         && ((ClientPropertyKey)key).getReportValueNotSerializable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    validCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    keys[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // Write ou the Serializable key/value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            s.writeInt(validCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (validCount > 0) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    91
                for (Object key : keys) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    92
                    if (key != null) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    93
                        s.writeObject(key);
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    94
                        s.writeObject(table.get(key));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        if (--validCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Put the key-value pair into storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void put(Object key, Object value){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (table==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            table = new Object[] {key, value};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (size < ARRAY_BOUNDARY) {              // We are an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (containsKey(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    Object[] tmp = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    for (int i = 0; i<tmp.length-1; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        if (tmp[i].equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            tmp[i+1]=value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    int i = array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    Object[] tmp = new Object[i+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    System.arraycopy(array, 0, tmp, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    tmp[i] = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    tmp[i+1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    table = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } else {                 // We are a hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                if ((size==ARRAY_BOUNDARY) && isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   136
                @SuppressWarnings("unchecked")
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   137
                Hashtable<Object,Object> tmp = (Hashtable<Object,Object>)table;
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23010
diff changeset
   138
                tmp.put(key, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Gets the value for key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (table !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                for (int i = 0; i<array.length-1; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    if (array[i].equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        value = array[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                value = ((Hashtable)table).get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the number of pairs in storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (table==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            size = ((Object[])table).length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            size = ((Hashtable)table).size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns true if we have a value for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        boolean contains = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (table !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                for (int i = 0; i<array.length-1; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    if (array[i].equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        contains = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                contains = ((Hashtable)table).containsKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return contains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Removes the key and its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returns the value for the pair removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public Object remove(Object key){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (key==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (table !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (isArray()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                // Is key on the list?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                for (int i = array.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    if (array[i].equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        value = array[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // If so,  remove it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    Object[] tmp = new Object[array.length-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    // Copy the list up to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    System.arraycopy(array, 0, tmp, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    // Copy from two past the index, up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    // the end of tmp (which is two elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    // shorter than the old list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    if (index < tmp.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        System.arraycopy(array, index+2, tmp, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                         tmp.length - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    // set the listener array to the new array or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    table = (tmp.length == 0) ? null : tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                value = ((Hashtable)table).remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (size()==ARRAY_BOUNDARY - 1 && !isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                shrink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Removes all the mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        table = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Returns a clone of the <code>ArrayTable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        ArrayTable newArrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            for (int i = 0 ;i < array.length-1 ; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                newArrayTable.put(array[i], array[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } else {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   264
            Hashtable<?,?> tmp = (Hashtable)table;
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   265
            Enumeration<?> keys = tmp.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                Object o = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                newArrayTable.put(o,tmp.get(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return newArrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns the keys of the table, or <code>null</code> if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * are currently no bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param keys  array of keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @return an array of bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public Object[] getKeys(Object[] keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (table == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                keys = new Object[array.length / 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            for (int i = 0, index = 0 ;i < array.length-1 ; i+=2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                     index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                keys[index] = array[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   294
            Hashtable<?,?> tmp = (Hashtable)table;
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   295
            Enumeration<?> enum_ = tmp.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            int counter = tmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                keys = new Object[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            while (counter > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                keys[--counter] = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Returns true if the current storage mechanism is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * an array of alternating key-value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private boolean isArray(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return (table instanceof Object[]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Grows the storage from an array to a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private void grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        Object[] array = (Object[])table;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   320
        Hashtable<Object, Object> tmp = new Hashtable<Object, Object>(array.length/2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        for (int i = 0; i<array.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            tmp.put(array[i], array[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        table = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Shrinks the storage from a hashtable to an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private void shrink() {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   331
        Hashtable<?,?> tmp = (Hashtable)table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Object[] array = new Object[tmp.size()*2];
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   333
        Enumeration<?> keys = tmp.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            Object o = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            array[j] = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            array[j+1] = tmp.get(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            j+=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        table = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}