rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 27 Feb 2015 18:11:54 +0100
changeset 1798 18b3a9a85716
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
addIfMissing to iterate over the array just once
jtulach@165
     1
/**
jtulach@165
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@165
     4
 *
jtulach@165
     5
 * This program is free software: you can redistribute it and/or modify
jtulach@165
     6
 * it under the terms of the GNU General Public License as published by
jtulach@165
     7
 * the Free Software Foundation, version 2 of the License.
jtulach@165
     8
 *
jtulach@165
     9
 * This program is distributed in the hope that it will be useful,
jtulach@165
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jtulach@165
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jtulach@165
    12
 * GNU General Public License for more details.
jtulach@165
    13
 *
jtulach@165
    14
 * You should have received a copy of the GNU General Public License
jtulach@165
    15
 * along with this program. Look for COPYING file in the top folder.
jtulach@165
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jtulach@162
    17
 */
jtulach@162
    18
package org.apidesign.vm4brwsr;
jtulach@162
    19
jtulach@162
    20
/**
jtulach@162
    21
 *
jtulach@162
    22
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@162
    23
 */
jtulach@162
    24
class StringArray {
jtulach@162
    25
    private String[] arr;
jtulach@162
    26
jtulach@162
    27
    public StringArray() {
jtulach@162
    28
    }
jtulach@162
    29
jtulach@162
    30
    private StringArray(String[] arr) {
jtulach@162
    31
        this.arr = arr;
jtulach@162
    32
    }
jtulach@162
    33
    
jtulach@162
    34
    public void add(String s) {
jaroslav@1798
    35
        add(s, false);
jaroslav@1798
    36
    }
jaroslav@1798
    37
    private boolean add(String s, boolean check) {
jtulach@162
    38
        if (arr == null) {
jtulach@162
    39
            arr = new String[1];
jtulach@162
    40
        } else {
jtulach@162
    41
            String[] tmp = new String[arr.length + 1];
jtulach@162
    42
            for (int i = 0; i < arr.length; i++) {
jaroslav@1798
    43
                if (check && s.equals(arr[i])) {
jaroslav@1798
    44
                    return false;
jaroslav@1798
    45
                }
jtulach@162
    46
                tmp[i] = arr[i];
jtulach@162
    47
            }
jtulach@162
    48
            arr = tmp;
jtulach@162
    49
        }
jtulach@162
    50
        arr[arr.length - 1] = s;
jaroslav@1798
    51
        return true;
jtulach@162
    52
    }
jaroslav@874
    53
jaroslav@874
    54
    StringArray addAndNew(String... values) {
jaroslav@874
    55
        int j;
jaroslav@874
    56
        String[] tmp;
jaroslav@874
    57
        if (arr == null) {
jaroslav@874
    58
            tmp = new String[values.length];
jaroslav@874
    59
            j = 0;
jaroslav@874
    60
        } else {
jaroslav@874
    61
            tmp = new String[arr.length + values.length];
jaroslav@874
    62
            for (int i = 0; i < arr.length; i++) {
jaroslav@874
    63
                tmp[i] = arr[i];
jaroslav@874
    64
            }
jaroslav@874
    65
            j = arr.length;
jaroslav@874
    66
        }
jaroslav@874
    67
        for (int i = 0; i < values.length;) {
jaroslav@874
    68
            tmp[j++] = values[i++];
jaroslav@874
    69
        }
jaroslav@874
    70
        return new StringArray(tmp);
jaroslav@874
    71
    }
jtulach@162
    72
    
jtulach@162
    73
    public String[] toArray() {
jtulach@162
    74
        return arr == null ? new String[0] : arr;
jtulach@162
    75
    }
jtulach@162
    76
    
jaroslav@272
    77
    static StringArray asList(String... names) {
jtulach@162
    78
        return new StringArray(names);
jtulach@162
    79
    }
jtulach@162
    80
jtulach@162
    81
    void reverse() {
jtulach@162
    82
        for (int i = 0, j = arr.length; i < j; i++) {
jtulach@162
    83
            String s = arr[i];
jtulach@162
    84
            arr[i] = arr[--j];
jtulach@162
    85
            arr[j] = s;
jtulach@162
    86
        }
jtulach@162
    87
    }
jtulach@162
    88
jtulach@162
    89
    boolean contains(String n) {
jtulach@162
    90
        if (arr == null) {
jtulach@162
    91
            return false;
jtulach@162
    92
        }
jtulach@162
    93
        for (int i = 0; i < arr.length; i++) {
jtulach@162
    94
            if (n.equals(arr[i])) {
jtulach@162
    95
                return true;
jtulach@162
    96
            }
jtulach@162
    97
        }
jtulach@162
    98
        return false;
jtulach@162
    99
    }
jtulach@162
   100
jtulach@162
   101
    void delete(int indx) {
Martin@243
   102
        if (arr == null || indx < 0 || indx >= arr.length) {
jtulach@162
   103
            return;
jtulach@162
   104
        }
jtulach@162
   105
        String[] tmp = new String[arr.length - 1];
jtulach@162
   106
        for (int i = 0, j = 0; i < arr.length; i++) {
Martin@243
   107
            if (i != indx) {
Martin@243
   108
                tmp[j++] = arr[i];
jtulach@162
   109
            }
jtulach@162
   110
        }
Martin@243
   111
        arr = tmp;
jtulach@162
   112
    }
jaroslav@1587
   113
    void remove(String item) {
jaroslav@1587
   114
        int f = indexOf(item);
jaroslav@1587
   115
        if (f != -1) {
jaroslav@1587
   116
            delete(f);
jaroslav@1587
   117
        }
jaroslav@1587
   118
    }
jtulach@162
   119
jtulach@162
   120
    int indexOf(String ic) {
jaroslav@1457
   121
        if (arr != null) for (int i = 0; i < arr.length; i++) {
jtulach@162
   122
            if (ic.equals(arr[i])) {
jtulach@162
   123
                return i;
jtulach@162
   124
            }
jtulach@162
   125
        }
jtulach@162
   126
        return -1;
jtulach@162
   127
    }
jaroslav@1455
   128
jaroslav@1466
   129
    String getAndClear(int count, boolean clear) {
jaroslav@1455
   130
        String s = arr[count];
jaroslav@1466
   131
        if (clear) {
jaroslav@1466
   132
            arr[count] = null;
jaroslav@1466
   133
        }
jaroslav@1455
   134
        return s;
jaroslav@1455
   135
    }
jaroslav@1455
   136
jaroslav@1455
   137
    void clear() {
jaroslav@1455
   138
        arr = null;
jaroslav@1455
   139
    }
jaroslav@1798
   140
jaroslav@1798
   141
    boolean addIfMissing(String s) {
jaroslav@1798
   142
        return add(s, true);
jaroslav@1798
   143
    }
jtulach@162
   144
}