jdk/src/share/classes/javax/swing/ArrayTable.java
author alexsch
Tue, 13 Dec 2011 18:38:39 +0400
changeset 11268 f0e59c4852de
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
7116950: Reduce number of warnings in swing Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2003, 2008, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * pairs are being archived, followed by the the key/value pairs. If
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
                }
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   136
                ((Hashtable<Object,Object>)table).put(key, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
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
     * Gets the value for key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (table !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                for (int i = 0; i<array.length-1; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    if (array[i].equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        value = array[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                value = ((Hashtable)table).get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Returns the number of pairs in storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (table==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            size = ((Object[])table).length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            size = ((Hashtable)table).size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns true if we have a value for the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        boolean contains = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (table !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                for (int i = 0; i<array.length-1; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    if (array[i].equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        contains = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                contains = ((Hashtable)table).containsKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return contains;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Removes the key and its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns the value for the pair removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Object remove(Object key){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (key==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (table !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (isArray()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // Is key on the list?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                for (int i = array.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    if (array[i].equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        value = array[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // If so,  remove it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    Object[] tmp = new Object[array.length-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    // Copy the list up to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    System.arraycopy(array, 0, tmp, 0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    // Copy from two past the index, up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    // the end of tmp (which is two elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    // shorter than the old list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    if (index < tmp.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        System.arraycopy(array, index+2, tmp, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                         tmp.length - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    // set the listener array to the new array or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    table = (tmp.length == 0) ? null : tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                value = ((Hashtable)table).remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (size()==ARRAY_BOUNDARY - 1 && !isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                shrink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Removes all the mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        table = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Returns a clone of the <code>ArrayTable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        ArrayTable newArrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for (int i = 0 ;i < array.length-1 ; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                newArrayTable.put(array[i], array[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   262
            Hashtable<?,?> tmp = (Hashtable)table;
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   263
            Enumeration<?> keys = tmp.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                Object o = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                newArrayTable.put(o,tmp.get(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return newArrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns the keys of the table, or <code>null</code> if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * are currently no bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param keys  array of keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return an array of bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public Object[] getKeys(Object[] keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (table == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            Object[] array = (Object[])table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                keys = new Object[array.length / 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            for (int i = 0, index = 0 ;i < array.length-1 ; i+=2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                     index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                keys[index] = array[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   292
            Hashtable<?,?> tmp = (Hashtable)table;
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   293
            Enumeration<?> enum_ = tmp.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            int counter = tmp.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                keys = new Object[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            while (counter > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                keys[--counter] = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Returns true if the current storage mechanism is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * an array of alternating key-value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private boolean isArray(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return (table instanceof Object[]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Grows the storage from an array to a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private void grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        Object[] array = (Object[])table;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   318
        Hashtable<Object, Object> tmp = new Hashtable<Object, Object>(array.length/2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        for (int i = 0; i<array.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            tmp.put(array[i], array[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        table = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Shrinks the storage from a hashtable to an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private void shrink() {
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   329
        Hashtable<?,?> tmp = (Hashtable)table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Object[] array = new Object[tmp.size()*2];
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   331
        Enumeration<?> keys = tmp.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        while (keys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            Object o = keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            array[j] = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            array[j+1] = tmp.get(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            j+=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        table = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}