rt/javap/src/main/java/org/apidesign/javap/Vector.java
changeset 772 d382dacfd73f
parent 443 9359b006782b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/javap/src/main/java/org/apidesign/javap/Vector.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,89 @@
     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 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.24 +import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
    1.25 +
    1.26 +/** A JavaScript ready replacement for java.util.Vector
    1.27 + *
    1.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.29 + */
    1.30 +@JavaScriptPrototype(prototype = "new Array" )
    1.31 +final class Vector {
    1.32 +    private Object[] arr;
    1.33 +    
    1.34 +    Vector() {
    1.35 +    }
    1.36 +
    1.37 +    Vector(int i) {
    1.38 +    }
    1.39 +
    1.40 +    void add(Object objectType) {
    1.41 +        addElement(objectType);
    1.42 +    }
    1.43 +    @JavaScriptBody(args = { "obj" }, body = 
    1.44 +        "this.push(obj);"
    1.45 +    )
    1.46 +    void addElement(Object obj) {
    1.47 +        final int s = size();
    1.48 +        setSize(s + 1);
    1.49 +        setElementAt(obj, s);
    1.50 +    }
    1.51 +
    1.52 +    @JavaScriptBody(args = { }, body = 
    1.53 +        "return this.length;"
    1.54 +    )
    1.55 +    int size() {
    1.56 +        return arr == null ? 0 : arr.length;
    1.57 +    }
    1.58 +
    1.59 +    @JavaScriptBody(args = { "newArr" }, body =
    1.60 +        "for (var i = 0; i < this.length; i++) {\n"
    1.61 +      + "  newArr[i] = this[i];\n"
    1.62 +      + "}\n")
    1.63 +    void copyInto(Object[] newArr) {
    1.64 +        if (arr == null) {
    1.65 +            return;
    1.66 +        }
    1.67 +        int min = Math.min(newArr.length, arr.length);
    1.68 +        for (int i = 0; i < min; i++) {
    1.69 +            newArr[i] = arr[i];
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    @JavaScriptBody(args = { "index" }, body =
    1.74 +        "return this[index];"
    1.75 +    )
    1.76 +    Object elementAt(int index) {
    1.77 +        return arr[index];
    1.78 +    }
    1.79 +
    1.80 +    private void setSize(int len) {
    1.81 +        Object[] newArr = new Object[len];
    1.82 +        copyInto(newArr);
    1.83 +        arr = newArr;
    1.84 +    }
    1.85 +
    1.86 +    @JavaScriptBody(args = { "val", "index" }, body = 
    1.87 +        "this[index] = val;"
    1.88 +    )
    1.89 +    void setElementAt(Object val, int index) {
    1.90 +        arr[index] = val;
    1.91 +    }
    1.92 +}