1 19 20 package org.netbeans.lib.editor.util; 21 22 import java.util.AbstractList ; 23 import java.util.List ; 24 import java.util.RandomAccess ; 25 26 32 33 public final class ArrayUtilities { 34 35 private static boolean[] EMPTY_BOOLEAN_ARRAY; 36 37 private static char[] EMPTY_CHAR_ARRAY; 38 39 private static int[] EMPTY_INT_ARRAY; 40 41 private ArrayUtilities() { 42 } 44 45 public static boolean[] booleanArray(boolean[] oldArray) { 46 return booleanArray(oldArray, (oldArray != null) ? oldArray.length << 1 : 1); 47 } 48 49 public static boolean[] booleanArray(boolean[] oldArray, int newSize) { 50 return booleanArray(oldArray, newSize, 51 (oldArray != null) ? Math.min(newSize, oldArray.length) : 0, true); 52 } 53 54 public static boolean[] booleanArray(boolean[] oldArray, int newSize, int copyLen, boolean forwardFill) { 55 boolean[] newArray = new boolean[newSize]; 56 if (copyLen > 0) 57 if (forwardFill) 58 System.arraycopy(oldArray, 0, newArray, 0, copyLen); 59 else System.arraycopy(oldArray, oldArray.length - copyLen, 61 newArray, newSize - copyLen, copyLen); 62 return newArray; 63 } 64 65 public static char[] charArray(char[] oldArray) { 66 return charArray(oldArray, (oldArray != null) ? oldArray.length << 1 : 1); 67 } 68 69 public static char[] charArray(char[] oldArray, int newSize) { 70 return charArray(oldArray, newSize, 71 (oldArray != null) ? Math.min(newSize, oldArray.length) : 0, true); 72 } 73 74 public static char[] charArray(char[] oldArray, int newSize, int copyLen, boolean forwardFill) { 75 char[] newArray = new char[newSize]; 76 if (copyLen > 0) 77 if (forwardFill) 78 System.arraycopy(oldArray, 0, newArray, 0, copyLen); 79 else System.arraycopy(oldArray, oldArray.length - copyLen, 81 newArray, newSize - copyLen, copyLen); 82 return newArray; 83 } 84 85 public static char[] charArray(char[] oldArray, int newSize, int gapStart, int gapLength) { 86 char[] newArray = new char[newSize]; 87 if (gapStart > 0) 88 System.arraycopy(oldArray, 0, newArray, 0, gapStart); 89 gapStart += gapLength; 90 gapLength = oldArray.length - gapStart; 91 if (gapLength > 0) 92 System.arraycopy(oldArray, gapStart, 93 newArray, newSize - gapLength, gapLength); 94 return newArray; 95 } 96 97 public static int[] intArray(int[] oldArray) { 98 return intArray(oldArray, (oldArray != null) ? oldArray.length << 1 : 1); 99 } 100 101 public static int[] intArray(int[] oldArray, int newSize) { 102 return intArray(oldArray, newSize, 103 (oldArray != null) ? Math.min(newSize, oldArray.length) : 0, true); 104 } 105 106 public static int[] intArray(int[] oldArray, int newSize, int copyLen, boolean forwardFill) { 107 int[] newArray = new int[newSize]; 108 if (copyLen > 0) 109 if (forwardFill) 110 System.arraycopy(oldArray, 0, newArray, 0, copyLen); 111 else System.arraycopy(oldArray, oldArray.length - copyLen, 113 newArray, newSize - copyLen, copyLen); 114 return newArray; 115 } 116 117 public static int[] intArray(int[] oldArray, int newSize, int gapStart, int gapLength) { 118 int[] newArray = new int[newSize]; 119 if (gapStart > 0) 120 System.arraycopy(oldArray, 0, newArray, 0, gapStart); 121 gapStart += gapLength; 122 gapLength = oldArray.length - gapStart; 123 if (gapLength > 0) 124 System.arraycopy(oldArray, gapStart, 125 newArray, newSize - gapLength, gapLength); 126 return newArray; 127 } 128 129 public static boolean[] emptyBooleanArray() { 130 if (EMPTY_BOOLEAN_ARRAY == null) { EMPTY_BOOLEAN_ARRAY = new boolean[0]; 132 } 133 return EMPTY_BOOLEAN_ARRAY; 134 } 135 136 public static char[] emptyCharArray() { 137 if (EMPTY_CHAR_ARRAY == null) { EMPTY_CHAR_ARRAY = new char[0]; 139 } 140 return EMPTY_CHAR_ARRAY; 141 } 142 143 public static int[] emptyIntArray() { 144 if (EMPTY_INT_ARRAY == null) { EMPTY_INT_ARRAY = new int[0]; 146 } 147 return EMPTY_INT_ARRAY; 148 } 149 150 public static int digitCount(int number) { 151 return String.valueOf(number).length(); 152 } 153 154 public static void appendIndex(StringBuilder sb, int index, int maxDigitCount) { 155 String indexStr = String.valueOf(index); 156 appendSpaces(sb, maxDigitCount - indexStr.length()); 157 sb.append(indexStr); 158 } 159 160 public static void appendIndex(StringBuffer sb, int index, int maxDigitCount) { 161 String indexStr = String.valueOf(index); 162 appendSpaces(sb, maxDigitCount - indexStr.length()); 163 sb.append(indexStr); 164 } 165 166 public static void appendSpaces(StringBuilder sb, int spaceCount) { 167 while (--spaceCount >= 0) { 168 sb.append(' '); 169 } 170 } 171 172 public static void appendSpaces(StringBuffer sb, int spaceCount) { 173 while (--spaceCount >= 0) { 174 sb.append(' '); 175 } 176 } 177 178 public static void appendBracketedIndex(StringBuilder sb, int index, int maxDigitCount) { 179 sb.append('['); 180 appendIndex(sb, index, maxDigitCount); 181 sb.append("]: "); 182 } 183 184 public static void appendBracketedIndex(StringBuffer sb, int index, int maxDigitCount) { 185 sb.append('['); 186 appendIndex(sb, index, maxDigitCount); 187 sb.append("]: "); 188 } 189 190 198 public static <E> List <E> unmodifiableList(E[] array) { 199 return new UnmodifiableList<E>(array); 200 } 201 202 public static String toString(Object [] array) { 203 StringBuilder sb = new StringBuilder (); 204 int maxDigitCount = digitCount(array.length); 205 for (int i = 0; i < array.length; i++) { 206 appendBracketedIndex(sb, i, maxDigitCount); 207 sb.append(array[i]); 208 sb.append('\n'); 209 } 210 return sb.toString(); 211 } 212 213 public static String toString(int[] array) { 214 StringBuilder sb = new StringBuilder (); 215 int maxDigitCount = digitCount(array.length); 216 for (int i = 0; i < array.length; i++) { 217 appendBracketedIndex(sb, i, maxDigitCount); 218 sb.append(array[i]); 219 sb.append('\n'); 220 } 221 return sb.toString(); 222 } 223 224 private static final class UnmodifiableList<E> extends AbstractList <E> 225 implements RandomAccess { 226 227 private E[] array; 228 229 UnmodifiableList(E[] array) { 230 this.array = array; 231 } 232 233 public E get(int index) { 234 if (index >= 0 && index < array.length) { 235 return array[index]; 236 } else { 237 throw new IndexOutOfBoundsException ("index = " + index + ", size = " + array.length); } 239 } 240 241 public int size() { 242 return array.length; 243 } 244 245 246 public Object [] toArray() { 247 return array.clone(); 248 } 249 250 public <T> T[] toArray(T[] a) { 251 if (a.length < array.length) { 252 @SuppressWarnings ("unchecked") 253 T[] aa = (T[])java.lang.reflect.Array. 254 newInstance(a.getClass().getComponentType(), array.length); 255 a = aa; 256 } 257 System.arraycopy(array, 0, a, 0, array.length); 258 if (a.length > array.length) 259 a[array.length] = null; 260 return a; 261 } 262 263 } 264 265 } 266 | Popular Tags |