1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/javap/src/main/java/org/apidesign/javap/Hashtable.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,94 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.javap;
1.22 +
1.23 +/** A JavaScript optimized replacement for Hashtable.
1.24 + *
1.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.26 + */
1.27 +final class Hashtable {
1.28 + private Object[] keys;
1.29 + private Object[] values;
1.30 +
1.31 + Hashtable(int i) {
1.32 + this();
1.33 + }
1.34 +
1.35 + Hashtable(int i, double d) {
1.36 + this();
1.37 + }
1.38 +
1.39 + Hashtable() {
1.40 + }
1.41 +
1.42 + synchronized void put(Object key, Object val) {
1.43 + int[] where = { -1, -1 };
1.44 + Object found = get(key, where);
1.45 + if (where[0] != -1) {
1.46 + // key exists
1.47 + values[where[0]] = val;
1.48 + } else {
1.49 + if (where[1] != -1) {
1.50 + // null found
1.51 + keys[where[1]] = key;
1.52 + values[where[1]] = val;
1.53 + } else {
1.54 + if (keys == null) {
1.55 + keys = new Object[11];
1.56 + values = new Object[11];
1.57 + keys[0] = key;
1.58 + values[0] = val;
1.59 + } else {
1.60 + Object[] newKeys = new Object[keys.length * 2];
1.61 + Object[] newValues = new Object[values.length * 2];
1.62 + for (int i = 0; i < keys.length; i++) {
1.63 + newKeys[i] = keys[i];
1.64 + newValues[i] = values[i];
1.65 + }
1.66 + newKeys[keys.length] = key;
1.67 + newValues[keys.length] = val;
1.68 + keys = newKeys;
1.69 + values = newValues;
1.70 + }
1.71 + }
1.72 + }
1.73 + }
1.74 +
1.75 + Object get(Object key) {
1.76 + return get(key, null);
1.77 + }
1.78 + private synchronized Object get(Object key, int[] foundAndNull) {
1.79 + if (keys == null) {
1.80 + return null;
1.81 + }
1.82 + for (int i = 0; i < keys.length; i++) {
1.83 + if (keys[i] == null) {
1.84 + if (foundAndNull != null) {
1.85 + foundAndNull[1] = i;
1.86 + }
1.87 + } else if (keys[i].equals(key)) {
1.88 + if (foundAndNull != null) {
1.89 + foundAndNull[0] = i;
1.90 + }
1.91 + return values[i];
1.92 + }
1.93 + }
1.94 + return null;
1.95 + }
1.96 +
1.97 +}