1 10 11 package com.triactive.jdo.util; 12 13 import java.util.Arrays ; 14 15 16 26 27 public class IntArrayList implements Cloneable 28 { 29 private int[] elements; 30 private int size = 0; 31 32 33 36 37 public IntArrayList() 38 { 39 this(10); 40 } 41 42 43 52 53 public IntArrayList(int initialCapacity) 54 { 55 if (initialCapacity < 0) 56 throw new IllegalArgumentException ("Illegal capacity: "+ initialCapacity); 57 58 elements = new int[initialCapacity]; 59 } 60 61 62 70 71 public IntArrayList(int[] initialContents) 72 { 73 size = initialContents.length; 74 elements = new int[(int)Math.min((size * 11L)/10, Integer.MAX_VALUE)]; 75 76 System.arraycopy(initialContents, 0, elements, 0, size); 77 } 78 79 80 86 87 public void trimToSize() 88 { 89 if (size < elements.length) 90 { 91 int[] newelems = new int[size]; 92 System.arraycopy(elements, 0, newelems, 0, size); 93 elements = newelems; 94 } 95 } 96 97 98 106 107 public void ensureCapacity(int minCapacity) 108 { 109 int oldCapacity = elements.length; 110 111 if (minCapacity > oldCapacity) 112 { 113 int[] newelems = new int[(int)Math.min((oldCapacity * 3L)/2, Integer.MAX_VALUE)]; 114 System.arraycopy(elements, 0, newelems, 0, size); 115 elements = newelems; 116 } 117 } 118 119 120 125 126 public int size() 127 { 128 return size; 129 } 130 131 132 138 139 public boolean isEmpty() 140 { 141 return size == 0; 142 } 143 144 145 153 154 public boolean contains(int elem) 155 { 156 return indexOf(elem) >= 0; 157 } 158 159 160 169 170 public int indexOf(int elem) 171 { 172 for (int i = 0; i < size; ++i) 173 { 174 if (elements[i] == elem) 175 return i; 176 } 177 178 return -1; 179 } 180 181 182 192 193 public int lastIndexOf(int elem) 194 { 195 for (int i = size - 1; i >= 0; --i) 196 { 197 if (elements[i] == elem) 198 return i; 199 } 200 201 return -1; 202 } 203 204 205 210 211 public Object clone() 212 { 213 try 214 { 215 IntArrayList l = (IntArrayList)super.clone(); 216 l.elements = new int[size]; 217 System.arraycopy(elements, 0, l.elements, 0, size); 218 return l; 219 } 220 catch (CloneNotSupportedException e) 221 { 222 223 throw new InternalError (); 224 } 225 } 226 227 228 233 234 public int[] toArray() 235 { 236 int[] result = new int[size]; 237 System.arraycopy(elements, 0, result, 0, size); 238 return result; 239 } 240 241 242 246 247 private void rangeCheck(int index) 248 { 249 if (index < 0 || index >= size) 250 throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); 251 } 252 253 254 266 267 public int get(int index) 268 { 269 rangeCheck(index); 270 271 return elements[index]; 272 } 273 274 275 290 291 public int set(int index, int element) 292 { 293 rangeCheck(index); 294 295 int oldValue = elements[index]; 296 elements[index] = element; 297 return oldValue; 298 } 299 300 301 307 308 public void add(int element) 309 { 310 ensureCapacity(size + 1); 311 elements[size++] = element; 312 } 313 314 315 329 330 public void add(int index, int element) 331 { 332 if (index < 0 || index > size) 333 throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); 334 335 ensureCapacity(size + 1); 336 System.arraycopy(elements, index, elements, index + 1, size - index); 337 elements[index] = element; 338 ++size; 339 } 340 341 342 356 357 public int remove(int index) 358 { 359 rangeCheck(index); 360 361 int oldValue = elements[index]; 362 int numMoved = size - index - 1; 363 if (numMoved > 0) 364 System.arraycopy(elements, index + 1, elements, index, numMoved); 365 366 --size; 367 368 return oldValue; 369 } 370 371 372 376 377 public void clear() 378 { 379 size = 0; 380 } 381 382 383 390 391 public void addAll(int[] ai) 392 { 393 int numNew = ai.length; 394 ensureCapacity(size + numNew); 395 396 System.arraycopy(ai, 0, elements, size, numNew); 397 } 398 399 400 416 417 public void addAll(int index, int[] ai) 418 { 419 if (index < 0 || index > size) 420 throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); 421 422 int numNew = ai.length; 423 ensureCapacity(size + numNew); 424 425 int numMoved = size - index; 426 if (numMoved > 0) 427 System.arraycopy(elements, index, elements, index + numNew, numMoved); 428 429 System.arraycopy(ai, 0, elements, index, numNew); 430 431 size += numNew; 432 } 433 434 435 453 454 public boolean containsAll(int[] ai) 455 { 456 for (int i = 0; i < ai.length; ++i) 457 { 458 if (!contains(ai[i])) 459 return false; 460 } 461 462 return true; 463 } 464 465 466 483 484 public boolean removeAll(int[] ai) 485 { 486 IntArrayList l = new IntArrayList(ai); 487 boolean modified = false; 488 489 for (int i = 0; i < size; ++i) 490 { 491 while (i < size && l.contains(elements[i])) 492 { 493 remove(i); 494 modified = true; 495 } 496 } 497 498 return modified; 499 } 500 501 502 521 522 public boolean retainAll(int[] ai) 523 { 524 IntArrayList l = new IntArrayList(ai); 525 boolean modified = false; 526 527 for (int i = 0; i < size; ++i) 528 { 529 while (i < size && !l.contains(elements[i])) 530 { 531 remove(i); 532 modified = true; 533 } 534 } 535 536 return modified; 537 } 538 539 540 550 551 public boolean equals(Object o) 552 { 553 if (o == this) 554 return true; 555 556 if (!(o instanceof IntArrayList)) 557 return false; 558 559 return Arrays.equals(toArray(), ((IntArrayList)o).toArray()); 560 } 561 562 563 568 569 public int hashCode() 570 { 571 int hashCode = 1; 572 573 for (int i = 0; i < size; ++i) 574 hashCode = 31 * hashCode + elements[i]; 575 576 return hashCode; 577 } 578 579 580 591 592 public String toString() 593 { 594 StringBuffer buf = new StringBuffer (); 595 buf.append('['); 596 597 for (int i = 0; i < size; ++i) 598 { 599 if (i > 0) 600 buf.append(", "); 601 602 buf.append(elements[i]); 603 } 604 605 buf.append(']'); 606 607 return buf.toString(); 608 } 609 } 610 | Popular Tags |