javap/src/main/java/org/apidesign/javap/Hashtable.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 12:58:12 +0100
branchemul
changeset 694 0d277415ed02
parent 378 ccb1544a88bc
permissions -rw-r--r--
Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
jaroslav@378
     1
/**
jaroslav@378
     2
 * Back 2 Browser Bytecode Translator
jaroslav@378
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@378
     4
 *
jaroslav@378
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@378
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@378
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@378
     8
 *
jaroslav@378
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@378
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@378
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@378
    12
 * GNU General Public License for more details.
jaroslav@378
    13
 *
jaroslav@378
    14
 * You should have received a copy of the GNU General Public License
jaroslav@378
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@378
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@149
    17
 */
jtulach@167
    18
package org.apidesign.javap;
jaroslav@149
    19
jaroslav@149
    20
/** A JavaScript optimized replacement for Hashtable.
jaroslav@149
    21
 *
jaroslav@149
    22
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@149
    23
 */
jaroslav@149
    24
final class Hashtable {
jaroslav@150
    25
    private Object[] keys;
jaroslav@150
    26
    private Object[] values;
jaroslav@149
    27
jaroslav@149
    28
    Hashtable(int i) {
jaroslav@150
    29
        this();
jaroslav@149
    30
    }
jaroslav@149
    31
jaroslav@149
    32
    Hashtable(int i, double d) {
jaroslav@150
    33
        this();
jaroslav@149
    34
    }
jaroslav@149
    35
jaroslav@149
    36
    Hashtable() {
jaroslav@149
    37
    }
jaroslav@149
    38
jaroslav@150
    39
    synchronized void put(Object key, Object val) {
jaroslav@150
    40
        int[] where = { -1, -1 };
jaroslav@150
    41
        Object found = get(key, where);
jaroslav@150
    42
        if (where[0] != -1) {
jaroslav@150
    43
            // key exists
jaroslav@150
    44
            values[where[0]] = val;
jaroslav@150
    45
        } else {
jaroslav@150
    46
            if (where[1] != -1) {
jaroslav@150
    47
                // null found
jaroslav@150
    48
                keys[where[1]] = key;
jaroslav@150
    49
                values[where[1]] = val;
jaroslav@150
    50
            } else {
jaroslav@150
    51
                if (keys == null) {
jaroslav@150
    52
                    keys = new Object[11];
jaroslav@150
    53
                    values = new Object[11];
jaroslav@150
    54
                    keys[0] = key;
jaroslav@150
    55
                    values[0] = val;
jaroslav@150
    56
                } else {
jaroslav@150
    57
                    Object[] newKeys = new Object[keys.length * 2];
jaroslav@150
    58
                    Object[] newValues = new Object[values.length * 2];
jaroslav@150
    59
                    for (int i = 0; i < keys.length; i++) {
jaroslav@150
    60
                        newKeys[i] = keys[i];
jaroslav@150
    61
                        newValues[i] = values[i];
jaroslav@150
    62
                    }
jaroslav@150
    63
                    newKeys[keys.length] = key;
jaroslav@150
    64
                    newValues[keys.length] = val;
jaroslav@150
    65
                    keys = newKeys;
jaroslav@150
    66
                    values = newValues;
jaroslav@150
    67
                }
jaroslav@150
    68
            }
jaroslav@150
    69
        }
jaroslav@149
    70
    }
jaroslav@149
    71
jaroslav@150
    72
    Object get(Object key) {
jaroslav@150
    73
        return get(key, null);
jaroslav@150
    74
    }
jaroslav@150
    75
    private synchronized Object get(Object key, int[] foundAndNull) {
jaroslav@150
    76
        if (keys == null) {
jaroslav@150
    77
            return null;
jaroslav@150
    78
        }
jaroslav@150
    79
        for (int i = 0; i < keys.length; i++) {
jaroslav@150
    80
            if (keys[i] == null) {
jaroslav@150
    81
                if (foundAndNull != null) {
jaroslav@150
    82
                    foundAndNull[1] = i;
jaroslav@150
    83
                }
jaroslav@150
    84
            } else if (keys[i].equals(key)) {
jaroslav@150
    85
                if (foundAndNull != null) {
jaroslav@150
    86
                    foundAndNull[0] = i;
jaroslav@150
    87
                }
jaroslav@150
    88
                return values[i];
jaroslav@150
    89
            }
jaroslav@150
    90
        }
jaroslav@150
    91
        return null;
jaroslav@149
    92
    }
jaroslav@149
    93
    
jaroslav@149
    94
}