Trivial implementation of Hashtable and Vector javap
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 10 Nov 2012 20:31:39 +0100
branchjavap
changeset 150945817561b9a
parent 149 32653a09f0db
child 151 40f95fe90cdc
Trivial implementation of Hashtable and Vector
javap/src/main/java/sun/tools/javap/Hashtable.java
javap/src/main/java/sun/tools/javap/Vector.java
     1.1 --- a/javap/src/main/java/sun/tools/javap/Hashtable.java	Sat Nov 10 19:01:28 2012 +0100
     1.2 +++ b/javap/src/main/java/sun/tools/javap/Hashtable.java	Sat Nov 10 20:31:39 2012 +0100
     1.3 @@ -9,25 +9,73 @@
     1.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     1.5   */
     1.6  final class Hashtable {
     1.7 +    private Object[] keys;
     1.8 +    private Object[] values;
     1.9  
    1.10      Hashtable(int i) {
    1.11 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.12 +        this();
    1.13      }
    1.14  
    1.15      Hashtable(int i, double d) {
    1.16 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.17 +        this();
    1.18      }
    1.19  
    1.20      Hashtable() {
    1.21 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.22      }
    1.23  
    1.24 -    void put(Object keys, Object val) {
    1.25 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.26 +    synchronized void put(Object key, Object val) {
    1.27 +        int[] where = { -1, -1 };
    1.28 +        Object found = get(key, where);
    1.29 +        if (where[0] != -1) {
    1.30 +            // key exists
    1.31 +            values[where[0]] = val;
    1.32 +        } else {
    1.33 +            if (where[1] != -1) {
    1.34 +                // null found
    1.35 +                keys[where[1]] = key;
    1.36 +                values[where[1]] = val;
    1.37 +            } else {
    1.38 +                if (keys == null) {
    1.39 +                    keys = new Object[11];
    1.40 +                    values = new Object[11];
    1.41 +                    keys[0] = key;
    1.42 +                    values[0] = val;
    1.43 +                } else {
    1.44 +                    Object[] newKeys = new Object[keys.length * 2];
    1.45 +                    Object[] newValues = new Object[values.length * 2];
    1.46 +                    for (int i = 0; i < keys.length; i++) {
    1.47 +                        newKeys[i] = keys[i];
    1.48 +                        newValues[i] = values[i];
    1.49 +                    }
    1.50 +                    newKeys[keys.length] = key;
    1.51 +                    newValues[keys.length] = val;
    1.52 +                    keys = newKeys;
    1.53 +                    values = newValues;
    1.54 +                }
    1.55 +            }
    1.56 +        }
    1.57      }
    1.58  
    1.59 -    Object get(String mnem) {
    1.60 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    1.61 +    Object get(Object key) {
    1.62 +        return get(key, null);
    1.63 +    }
    1.64 +    private synchronized Object get(Object key, int[] foundAndNull) {
    1.65 +        if (keys == null) {
    1.66 +            return null;
    1.67 +        }
    1.68 +        for (int i = 0; i < keys.length; i++) {
    1.69 +            if (keys[i] == null) {
    1.70 +                if (foundAndNull != null) {
    1.71 +                    foundAndNull[1] = i;
    1.72 +                }
    1.73 +            } else if (keys[i].equals(key)) {
    1.74 +                if (foundAndNull != null) {
    1.75 +                    foundAndNull[0] = i;
    1.76 +                }
    1.77 +                return values[i];
    1.78 +            }
    1.79 +        }
    1.80 +        return null;
    1.81      }
    1.82      
    1.83  }
     2.1 --- a/javap/src/main/java/sun/tools/javap/Vector.java	Sat Nov 10 19:01:28 2012 +0100
     2.2 +++ b/javap/src/main/java/sun/tools/javap/Vector.java	Sat Nov 10 20:31:39 2012 +0100
     2.3 @@ -9,37 +9,49 @@
     2.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     2.5   */
     2.6  final class Vector {
     2.7 +    private Object[] arr;
     2.8 +    
     2.9      Vector() {
    2.10      }
    2.11  
    2.12      Vector(int i) {
    2.13 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.14 +        this();
    2.15      }
    2.16  
    2.17      void add(Object objectType) {
    2.18 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.19 +        addElement(objectType);
    2.20      }
    2.21      void addElement(Object obj) {
    2.22 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.23 +        setSize(size() + 1);
    2.24 +        setElementAt(obj, size());
    2.25      }
    2.26  
    2.27      int size() {
    2.28 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.29 +        return arr == null ? 0 : arr.length;
    2.30      }
    2.31  
    2.32 -    void copyInto(String[] accflags) {
    2.33 +    void copyInto(Object[] accflags) {
    2.34          throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.35      }
    2.36  
    2.37      Object elementAt(int index) {
    2.38 -        throw new UnsupportedOperationException("Not supported yet.");
    2.39 +        return arr[index];
    2.40      }
    2.41  
    2.42 -    void setSize(int i) {
    2.43 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.44 +    void setSize(int len) {
    2.45 +        if (arr == null) {
    2.46 +            arr = new Object[len];
    2.47 +        } else {
    2.48 +            Object[] newArr = new Object[len];
    2.49 +            int min = Math.min(len, arr.length);
    2.50 +            for (int i = 0; i < min; i++) {
    2.51 +                newArr[i] = arr[i];
    2.52 +            }
    2.53 +            arr = newArr;
    2.54 +        }
    2.55      }
    2.56  
    2.57 -    void setElementAt(String id, int token) {
    2.58 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    2.59 +    void setElementAt(Object val, int index) {
    2.60 +        arr[index] = val;
    2.61      }
    2.62  }