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