javap/src/main/java/org/apidesign/javap/Vector.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@316
    20
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@316
    21
import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
jaroslav@316
    22
jaroslav@149
    23
/** A JavaScript ready replacement for java.util.Vector
jaroslav@149
    24
 *
jaroslav@149
    25
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@149
    26
 */
jaroslav@316
    27
@JavaScriptPrototype(prototype = "new Array" )
jaroslav@288
    28
final class Vector {
jaroslav@150
    29
    private Object[] arr;
jaroslav@150
    30
    
jaroslav@149
    31
    Vector() {
jaroslav@149
    32
    }
jaroslav@149
    33
jaroslav@149
    34
    Vector(int i) {
jaroslav@149
    35
    }
jaroslav@149
    36
jaroslav@149
    37
    void add(Object objectType) {
jaroslav@150
    38
        addElement(objectType);
jaroslav@149
    39
    }
jaroslav@443
    40
    @JavaScriptBody(args = { "obj" }, body = 
jaroslav@443
    41
        "this.push(obj);"
jaroslav@316
    42
    )
jaroslav@149
    43
    void addElement(Object obj) {
jaroslav@151
    44
        final int s = size();
jaroslav@151
    45
        setSize(s + 1);
jaroslav@151
    46
        setElementAt(obj, s);
jaroslav@149
    47
    }
jaroslav@149
    48
jaroslav@443
    49
    @JavaScriptBody(args = { }, body = 
jaroslav@443
    50
        "return this.length;"
jaroslav@316
    51
    )
jaroslav@288
    52
    int size() {
jaroslav@150
    53
        return arr == null ? 0 : arr.length;
jaroslav@149
    54
    }
jaroslav@149
    55
jaroslav@443
    56
    @JavaScriptBody(args = { "newArr" }, body =
jaroslav@443
    57
        "for (var i = 0; i < this.length; i++) {\n"
jaroslav@443
    58
      + "  newArr[i] = this[i];\n"
jaroslav@316
    59
      + "}\n")
jaroslav@152
    60
    void copyInto(Object[] newArr) {
jaroslav@152
    61
        if (arr == null) {
jaroslav@152
    62
            return;
jaroslav@152
    63
        }
jaroslav@152
    64
        int min = Math.min(newArr.length, arr.length);
jaroslav@152
    65
        for (int i = 0; i < min; i++) {
jaroslav@152
    66
            newArr[i] = arr[i];
jaroslav@152
    67
        }
jaroslav@149
    68
    }
jaroslav@149
    69
jaroslav@443
    70
    @JavaScriptBody(args = { "index" }, body =
jaroslav@443
    71
        "return this[index];"
jaroslav@316
    72
    )
jaroslav@288
    73
    Object elementAt(int index) {
jaroslav@150
    74
        return arr[index];
jaroslav@149
    75
    }
jaroslav@149
    76
jaroslav@316
    77
    private void setSize(int len) {
jaroslav@152
    78
        Object[] newArr = new Object[len];
jaroslav@152
    79
        copyInto(newArr);
jaroslav@152
    80
        arr = newArr;
jaroslav@149
    81
    }
jaroslav@149
    82
jaroslav@443
    83
    @JavaScriptBody(args = { "val", "index" }, body = 
jaroslav@443
    84
        "this[index] = val;"
jaroslav@316
    85
    )
jaroslav@150
    86
    void setElementAt(Object val, int index) {
jaroslav@150
    87
        arr[index] = val;
jaroslav@149
    88
    }
jaroslav@149
    89
}