1 package org.ozoneDB.data; 9 import java.util.Arrays ; 10 import java.util.Comparator ; 11 import java.util.Iterator ; 12 import java.util.NoSuchElementException ; 13 import java.util.Collection ; 14 15 25 public class SimpleArrayList { 26 27 28 protected Object data[]; 29 30 31 protected int size; 32 33 38 public SimpleArrayList(int bufferSize) { 39 data = new Object [bufferSize]; 40 } 41 42 48 public SimpleArrayList(int bufferSize,Iterator dataSource) { 49 this(bufferSize); 50 51 while (dataSource.hasNext()) 52 add(dataSource.next()); 53 } 54 55 56 62 public SimpleArrayList(int bufferSize,Collection dataSource) { 63 this(bufferSize,dataSource.iterator()); 64 } 65 66 71 public SimpleArrayList(Collection dataSource) { 72 this(dataSource.size(),dataSource); 73 } 74 75 81 protected void ensureCapacity(int minCapacity) throws OutOfMemoryError { 82 if (minCapacity>data.length) { 83 rebuild(calcNewMinCapacityAfterEnlarge(minCapacity)); 84 } 85 } 86 87 91 protected int calcNewMinCapacityAfterEnlarge() { 92 return (data.length*3)/2+1; 93 } 94 95 100 protected int calcNewMinCapacityAfterEnlarge(int minCapacity) { 101 int newSize = calcNewMinCapacityAfterEnlarge(); 102 103 if (newSize>minCapacity) 104 minCapacity = newSize; 105 106 return minCapacity; 107 } 108 109 115 protected void rebuild(int newCapacity) { 116 Object [] newData = new Object [newCapacity]; 117 118 System.arraycopy(data,0,newData,0,size); 119 data = newData; 120 } 121 122 127 public void add(Object o) { 128 ensureCapacity(size+1); 129 data[size++] = o; 130 } 131 132 public void push(Object o) { 133 add(o); 134 } 135 136 143 public void set(int index,Object o) { 144 ensureCapacity(index+1); 145 data[index] = o; 146 if (size<=index) 147 size = index+1; 148 } 149 150 157 public void setArea(int start,int end,Object o) { 158 ensureCapacity(end); 159 160 for (;start<end;start++) 161 data[start] = o; 162 163 if (size<end) 164 size = end; 165 } 166 167 170 public Object get(int index) { 171 return data[index]; 172 } 173 174 180 public int indexOf(Object o) { 181 return indexOf(o,0); 182 } 183 184 191 public int indexOf(Object o,int startIndex) { 192 for (;startIndex<size;startIndex++) 193 if (data[startIndex]==o) 194 return startIndex; 195 196 return -1; 197 } 198 199 205 public int lastIndexOf(Object o) { 206 return lastIndexOf(o,size); 207 } 208 209 216 public int lastIndexOf(Object o,int startIndex) { 217 for (;--startIndex>=0;) 218 if (data[startIndex]==o) 219 return startIndex; 220 221 return -1; 222 } 223 224 230 public boolean remove(Object o) { 231 int index = indexOf(o); 232 233 if (index>=0) { 234 remove(index); 235 236 return true; 237 } 238 239 return false; 240 } 241 242 248 public boolean removeL(Object o) { 249 int index = lastIndexOf(o); 250 251 if (index>=0) { 252 remove(index); 253 254 return true; 255 } 256 257 return false; 258 } 259 260 266 public Object remove(int index) { 267 Object o = data[index]; 268 269 System.arraycopy(data,index+1,data,index,size-index-1); 270 271 data[--size] = null; 273 return o; 274 } 275 276 281 public Object removeLast() { 282 int i = size-1; 283 284 if (i>=0) { 285 Object o = data[i]; 286 287 data[size = i] = null; return o; 289 } else 290 return null; 291 } 292 293 public Object pop() { 294 return removeLast(); 295 } 296 297 public Object peek() { 298 if (size!=0) { 299 return data[size-1]; 300 } else { 301 return null; 302 } 303 } 304 305 310 public Object removeFirst() { 311 if (size>0) { 312 Object o = data[0]; 313 314 System.arraycopy(data,0+1,data,0,--size); 315 316 data[size] = null; 318 return o; 319 } else 320 return null; 321 } 322 323 326 public void insertAtStart(Object o) { 327 insertSpaceAtStart(1); 328 data[0] = o; 329 } 330 331 335 public void insertSpaceAtStart(int elementCount) { 336 int newSize = size+elementCount; 337 338 if (newSize>data.length) { 339 int newCapacity = calcNewMinCapacityAfterEnlarge(elementCount); 340 Object [] newData = new Object [newCapacity]; 341 342 System.arraycopy(data,0,newData,elementCount,size); 343 data = newData; 344 } else { 345 System.arraycopy(data,0,data,elementCount,size); 346 } 347 size = newSize; 348 } 349 350 353 public int size() { 354 return size; 355 } 356 357 360 public void sort() { 361 sort(0,size); 362 } 363 364 370 public void sort(int start,int end) { 371 Arrays.sort(data,start,end); 372 } 373 374 379 public void sort(Comparator c) { 380 sort(c,0,size); 381 } 382 383 390 public void sort(Comparator c,int start,int end) { 391 Arrays.sort(data,start,end,c); 392 } 393 394 397 public void clear() { 398 Arrays.fill(data,0,size,null); 399 size = 0; 400 } 401 402 406 public Iterator iterator() { 407 return new SimpleIterator() { 408 int index = 0; 409 410 public boolean hasNext() { 411 return index<size; 412 } 413 414 public Object next() { 415 if (index<size) { 416 return data[index++]; 417 } else 418 throw new NoSuchElementException (); 419 } 420 }; 421 } 422 423 426 public String toString() { 427 int size = size(); 428 StringBuffer b = new StringBuffer (30+size*32); 429 430 b.append("SimpleArrayList[size="); 431 b.append(size); 432 b.append(",data={"); 433 434 Iterator i = iterator(); 435 436 while (i.hasNext()) { 437 b.append(i.next().toString()); 438 439 if (i.hasNext()) 440 b.append(','); 441 } 442 b.append("}]"); 443 return b.toString(); 444 } 445 } 446 | Popular Tags |