rt/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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         add(s, false);
    36     }
    37     private boolean add(String s, boolean check) {
    38         if (arr == null) {
    39             arr = new String[1];
    40         } else {
    41             String[] tmp = new String[arr.length + 1];
    42             for (int i = 0; i < arr.length; i++) {
    43                 if (check && s.equals(arr[i])) {
    44                     return false;
    45                 }
    46                 tmp[i] = arr[i];
    47             }
    48             arr = tmp;
    49         }
    50         arr[arr.length - 1] = s;
    51         return true;
    52     }
    53 
    54     StringArray addAndNew(String... values) {
    55         int j;
    56         String[] tmp;
    57         if (arr == null) {
    58             tmp = new String[values.length];
    59             j = 0;
    60         } else {
    61             tmp = new String[arr.length + values.length];
    62             for (int i = 0; i < arr.length; i++) {
    63                 tmp[i] = arr[i];
    64             }
    65             j = arr.length;
    66         }
    67         for (int i = 0; i < values.length;) {
    68             tmp[j++] = values[i++];
    69         }
    70         return new StringArray(tmp);
    71     }
    72     
    73     public String[] toArray() {
    74         return arr == null ? new String[0] : arr;
    75     }
    76     
    77     static StringArray asList(String... names) {
    78         return new StringArray(names);
    79     }
    80 
    81     void reverse() {
    82         for (int i = 0, j = arr.length; i < j; i++) {
    83             String s = arr[i];
    84             arr[i] = arr[--j];
    85             arr[j] = s;
    86         }
    87     }
    88 
    89     boolean contains(String n) {
    90         if (arr == null) {
    91             return false;
    92         }
    93         for (int i = 0; i < arr.length; i++) {
    94             if (n.equals(arr[i])) {
    95                 return true;
    96             }
    97         }
    98         return false;
    99     }
   100 
   101     void delete(int indx) {
   102         if (arr == null || indx < 0 || indx >= arr.length) {
   103             return;
   104         }
   105         String[] tmp = new String[arr.length - 1];
   106         for (int i = 0, j = 0; i < arr.length; i++) {
   107             if (i != indx) {
   108                 tmp[j++] = arr[i];
   109             }
   110         }
   111         arr = tmp;
   112     }
   113     void remove(String item) {
   114         int f = indexOf(item);
   115         if (f != -1) {
   116             delete(f);
   117         }
   118     }
   119 
   120     int indexOf(String ic) {
   121         if (arr != null) for (int i = 0; i < arr.length; i++) {
   122             if (ic.equals(arr[i])) {
   123                 return i;
   124             }
   125         }
   126         return -1;
   127     }
   128 
   129     String getAndClear(int count, boolean clear) {
   130         String s = arr[count];
   131         if (clear) {
   132             arr[count] = null;
   133         }
   134         return s;
   135     }
   136 
   137     void clear() {
   138         arr = null;
   139     }
   140 
   141     boolean addIfMissing(String s) {
   142         return add(s, true);
   143     }
   144 }