rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java
changeset 772 d382dacfd73f
parent 272 a6a23aa7a546
child 874 2bcbe348dbec
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,97 @@
     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.vm4brwsr;
    1.22 +
    1.23 +/**
    1.24 + *
    1.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.26 + */
    1.27 +class StringArray {
    1.28 +    private String[] arr;
    1.29 +
    1.30 +    public StringArray() {
    1.31 +    }
    1.32 +
    1.33 +    private StringArray(String[] arr) {
    1.34 +        this.arr = arr;
    1.35 +    }
    1.36 +    
    1.37 +    public void add(String s) {
    1.38 +        if (arr == null) {
    1.39 +            arr = new String[1];
    1.40 +        } else {
    1.41 +            String[] tmp = new String[arr.length + 1];
    1.42 +            for (int i = 0; i < arr.length; i++) {
    1.43 +                tmp[i] = arr[i];
    1.44 +            }
    1.45 +            arr = tmp;
    1.46 +        }
    1.47 +        arr[arr.length - 1] = s;
    1.48 +    }
    1.49 +    
    1.50 +    public String[] toArray() {
    1.51 +        return arr == null ? new String[0] : arr;
    1.52 +    }
    1.53 +    
    1.54 +    static StringArray asList(String... names) {
    1.55 +        return new StringArray(names);
    1.56 +    }
    1.57 +
    1.58 +    void reverse() {
    1.59 +        for (int i = 0, j = arr.length; i < j; i++) {
    1.60 +            String s = arr[i];
    1.61 +            arr[i] = arr[--j];
    1.62 +            arr[j] = s;
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    boolean contains(String n) {
    1.67 +        if (arr == null) {
    1.68 +            return false;
    1.69 +        }
    1.70 +        for (int i = 0; i < arr.length; i++) {
    1.71 +            if (n.equals(arr[i])) {
    1.72 +                return true;
    1.73 +            }
    1.74 +        }
    1.75 +        return false;
    1.76 +    }
    1.77 +
    1.78 +    void delete(int indx) {
    1.79 +        if (arr == null || indx < 0 || indx >= arr.length) {
    1.80 +            return;
    1.81 +        }
    1.82 +        String[] tmp = new String[arr.length - 1];
    1.83 +        for (int i = 0, j = 0; i < arr.length; i++) {
    1.84 +            if (i != indx) {
    1.85 +                tmp[j++] = arr[i];
    1.86 +            }
    1.87 +        }
    1.88 +        arr = tmp;
    1.89 +    }
    1.90 +
    1.91 +    int indexOf(String ic) {
    1.92 +        for (int i = 0; i < arr.length; i++) {
    1.93 +            if (ic.equals(arr[i])) {
    1.94 +                return i;
    1.95 +            }
    1.96 +        }
    1.97 +        return -1;
    1.98 +    }
    1.99 +    
   1.100 +}