1 30 31 32 package org.hsqldb.lib; 33 34 import java.lang.reflect.Array ; 35 36 38 46 public class HsqlArrayList extends BaseList implements HsqlList { 47 48 72 private static final int DEFAULT_INITIAL_CAPACITY = 10; 73 private static final float DEFAULT_RESIZE_FACTOR = 2.0f; 74 private Object [] elementData; 75 private boolean minimizeOnClear; 76 77 78 public HsqlArrayList() { 79 80 elementData = new Object [DEFAULT_INITIAL_CAPACITY]; 82 } 83 84 88 public HsqlArrayList(boolean minimize) { 89 90 elementData = new Object [DEFAULT_INITIAL_CAPACITY]; 92 minimizeOnClear = minimize; 93 } 94 95 96 public HsqlArrayList(int initialCapacity) { 97 98 if (initialCapacity < 0) { 100 throw new NegativeArraySizeException ( 101 "Invalid initial capacity given"); 102 } 103 104 if (initialCapacity == 0) { 105 elementData = new Object [1]; 106 } else { 107 elementData = new Object [initialCapacity]; 108 } 109 } 110 111 112 public void add(int index, Object element) { 113 114 if (index > elementCount) { 116 throw new IndexOutOfBoundsException ("Index out of bounds: " 117 + index + ">" + elementCount); 118 } 119 120 if (index < 0) { 121 throw new IndexOutOfBoundsException ("Index out of bounds: " 122 + index + " < 0"); 123 } 124 125 if (elementCount >= elementData.length) { 126 increaseCapacity(); 127 } 128 129 for (int i = elementCount; i > index; i--) { 130 elementData[i] = elementData[i - 1]; 131 } 132 133 elementData[index] = element; 134 135 elementCount++; 136 } 137 138 139 public boolean add(Object element) { 140 141 if (elementCount >= elementData.length) { 143 increaseCapacity(); 144 } 145 146 elementData[elementCount] = element; 147 148 elementCount++; 149 150 return true; 151 } 152 153 154 public Object get(int index) { 155 156 if (index >= elementCount) { 157 throw new IndexOutOfBoundsException ("Index out of bounds: " 158 + index + " >= " 159 + elementCount); 160 } 161 162 if (index < 0) { 163 throw new IndexOutOfBoundsException ("Index out of bounds: " 164 + index + " < 0"); 165 } 166 167 return elementData[index]; 168 } 169 170 171 public int indexOf(Object o) { 172 173 for (int i = 0; i < elementCount; i++) { 174 if (elementData[i].equals(o)) { 175 return i; 176 } 177 } 178 179 return -1; 180 } 181 182 183 public Object remove(int index) { 184 185 if (index >= elementCount) { 186 throw new IndexOutOfBoundsException ("Index out of bounds: " 187 + index + " >= " 188 + elementCount); 189 } 190 191 if (index < 0) { 192 throw new IndexOutOfBoundsException ("Index out of bounds: " 193 + index + " < 0"); 194 } 195 196 Object removedObj = elementData[index]; 197 198 for (int i = index; i < elementCount - 1; i++) { 199 elementData[i] = elementData[i + 1]; 200 } 201 202 elementCount--; 203 204 elementData[elementCount] = null; 205 206 if (minimizeOnClear && elementCount == 0) { 207 elementData = new Object [DEFAULT_INITIAL_CAPACITY]; 208 } 209 210 return removedObj; 211 } 212 213 214 public Object set(int index, Object element) { 215 216 if (index >= elementCount) { 217 throw new IndexOutOfBoundsException ("Index out of bounds: " 218 + index + " >= " 219 + elementCount); 220 } 221 222 if (index < 0) { 223 throw new IndexOutOfBoundsException ("Index out of bounds: " 224 + index + " < 0"); 225 } 226 227 Object replacedObj = elementData[index]; 228 229 elementData[index] = element; 230 231 return replacedObj; 232 } 233 234 235 public final int size() { 236 return elementCount; 237 } 238 239 private void increaseCapacity() { 240 241 int baseSize = elementData.length == 0 ? 1 242 : elementData.length; 243 Object [] newArray = 244 new Object [(int) (baseSize * DEFAULT_RESIZE_FACTOR)]; 245 246 System.arraycopy(elementData, 0, newArray, 0, elementData.length); 247 248 elementData = newArray; 249 newArray = null; 250 } 251 252 257 283 284 285 public void trim() { 286 287 Object [] newArray = new Object [elementCount]; 289 290 System.arraycopy(elementData, 0, newArray, 0, elementCount); 291 292 elementData = newArray; 293 newArray = null; 294 } 295 296 public void clear() { 298 299 if (minimizeOnClear 300 && elementData.length > DEFAULT_INITIAL_CAPACITY) { 301 elementData = new Object [DEFAULT_INITIAL_CAPACITY]; 302 elementCount = 0; 303 304 return; 305 } 306 307 for (int i = 0; i < elementCount; i++) { 308 elementData[i] = null; 309 } 310 311 elementCount = 0; 312 } 313 314 public void setSize(int newSize) { 315 316 if (newSize < elementCount) { 317 if (minimizeOnClear && newSize == 0 318 && elementData.length > DEFAULT_INITIAL_CAPACITY) { 319 elementData = new Object [DEFAULT_INITIAL_CAPACITY]; 320 elementCount = 0; 321 322 return; 323 } 324 325 for (int i = newSize; i < elementCount; i++) { 326 elementData[i] = null; 327 } 328 } 329 330 elementCount = newSize; 331 332 for (; elementCount > elementData.length; ) { 333 increaseCapacity(); 334 } 335 } 336 337 public Object [] toArray() { 339 340 Object [] a = new Object [elementCount]; 341 342 System.arraycopy(elementData, 0, a, 0, elementCount); 343 344 return a; 345 } 346 347 public Object [] toArray(int start, int limit) { 348 349 Object [] a = new Object [elementCount - limit]; 350 351 System.arraycopy(elementData, start, a, 0, elementCount - limit); 352 353 return a; 354 } 355 356 364 public Object toArray(Object a) { 365 366 if (Array.getLength(a) < elementCount) { 367 a = Array.newInstance(a.getClass().getComponentType(), 368 elementCount); 369 } 370 371 System.arraycopy(elementData, 0, a, 0, elementCount); 372 373 return a; 374 } 375 376 public void sort(ObjectComparator c) { 377 378 if (elementCount < 2) { 379 return; 380 } 381 382 Sort.sort(elementData, c, 0, elementCount - 1); 383 } 384 385 public Object [] getArray() { 386 return elementData; 387 } 388 } 389 | Popular Tags |