| 1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.ConcurrentModificationException ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.ListIterator ; 24 25 64 public class FastArrayList extends ArrayList { 65 66 67 69 70 73 public FastArrayList() { 74 75 super(); 76 this.list = new ArrayList (); 77 78 } 79 80 81 86 public FastArrayList(int capacity) { 87 88 super(); 89 this.list = new ArrayList (capacity); 90 91 } 92 93 94 101 public FastArrayList(Collection collection) { 102 103 super(); 104 this.list = new ArrayList (collection); 105 106 } 107 108 109 111 112 115 protected ArrayList list = null; 116 117 118 120 121 124 protected boolean fast = false; 125 126 127 132 public boolean getFast() { 133 return (this.fast); 134 } 135 136 141 public void setFast(boolean fast) { 142 this.fast = fast; 143 } 144 145 146 148 149 154 public boolean add(Object element) { 155 156 if (fast) { 157 synchronized (this) { 158 ArrayList temp = (ArrayList ) list.clone(); 159 boolean result = temp.add(element); 160 list = temp; 161 return (result); 162 } 163 } else { 164 synchronized (list) { 165 return (list.add(element)); 166 } 167 } 168 169 } 170 171 172 181 public void add(int index, Object element) { 182 183 if (fast) { 184 synchronized (this) { 185 ArrayList temp = (ArrayList ) list.clone(); 186 temp.add(index, element); 187 list = temp; 188 } 189 } else { 190 synchronized (list) { 191 list.add(index, element); 192 } 193 } 194 195 } 196 197 198 205 public boolean addAll(Collection collection) { 206 207 if (fast) { 208 synchronized (this) { 209 ArrayList temp = (ArrayList ) list.clone(); 210 boolean result = temp.addAll(collection); 211 list = temp; 212 return (result); 213 } 214 } else { 215 synchronized (list) { 216 return (list.addAll(collection)); 217 } 218 } 219 220 } 221 222 223 233 public boolean addAll(int index, Collection collection) { 234 235 if (fast) { 236 synchronized (this) { 237 ArrayList temp = (ArrayList ) list.clone(); 238 boolean result = temp.addAll(index, collection); 239 list = temp; 240 return (result); 241 } 242 } else { 243 synchronized (list) { 244 return (list.addAll(index, collection)); 245 } 246 } 247 248 } 249 250 251 258 public void clear() { 259 260 if (fast) { 261 synchronized (this) { 262 ArrayList temp = (ArrayList ) list.clone(); 263 temp.clear(); 264 list = temp; 265 } 266 } else { 267 synchronized (list) { 268 list.clear(); 269 } 270 } 271 272 } 273 274 275 279 public Object clone() { 280 281 FastArrayList results = null; 282 if (fast) { 283 results = new FastArrayList(list); 284 } else { 285 synchronized (list) { 286 results = new FastArrayList(list); 287 } 288 } 289 results.setFast(getFast()); 290 return (results); 291 292 } 293 294 295 300 public boolean contains(Object element) { 301 302 if (fast) { 303 return (list.contains(element)); 304 } else { 305 synchronized (list) { 306 return (list.contains(element)); 307 } 308 } 309 310 } 311 312 313 319 public boolean containsAll(Collection collection) { 320 321 if (fast) { 322 return (list.containsAll(collection)); 323 } else { 324 synchronized (list) { 325 return (list.containsAll(collection)); 326 } 327 } 328 329 } 330 331 332 339 public void ensureCapacity(int capacity) { 340 341 if (fast) { 342 synchronized (this) { 343 ArrayList temp = (ArrayList ) list.clone(); 344 temp.ensureCapacity(capacity); 345 list = temp; 346 } 347 } else { 348 synchronized (list) { 349 list.ensureCapacity(capacity); 350 } 351 } 352 353 } 354 355 356 364 public boolean equals(Object o) { 365 366 if (o == this) 368 return (true); 369 else if (!(o instanceof List )) 370 return (false); 371 List lo = (List ) o; 372 373 if (fast) { 375 ListIterator li1 = list.listIterator(); 376 ListIterator li2 = lo.listIterator(); 377 while (li1.hasNext() && li2.hasNext()) { 378 Object o1 = li1.next(); 379 Object o2 = li2.next(); 380 if (!(o1 == null ? o2 == null : o1.equals(o2))) 381 return (false); 382 } 383 return (!(li1.hasNext() || li2.hasNext())); 384 } else { 385 synchronized (list) { 386 ListIterator li1 = list.listIterator(); 387 ListIterator li2 = lo.listIterator(); 388 while (li1.hasNext() && li2.hasNext()) { 389 Object o1 = li1.next(); 390 Object o2 = li2.next(); 391 if (!(o1 == null ? o2 == null : o1.equals(o2))) 392 return (false); 393 } 394 return (!(li1.hasNext() || li2.hasNext())); 395 } 396 } 397 398 } 399 400 401 408 public Object get(int index) { 409 410 if (fast) { 411 return (list.get(index)); 412 } else { 413 synchronized (list) { 414 return (list.get(index)); 415 } 416 } 417 418 } 419 420 421 426 public int hashCode() { 427 428 if (fast) { 429 int hashCode = 1; 430 java.util.Iterator i = list.iterator(); 431 while (i.hasNext()) { 432 Object o = i.next(); 433 hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode()); 434 } 435 return (hashCode); 436 } else { 437 synchronized (list) { 438 int hashCode = 1; 439 java.util.Iterator i = list.iterator(); 440 while (i.hasNext()) { 441 Object o = i.next(); 442 hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode()); 443 } 444 return (hashCode); 445 } 446 } 447 448 } 449 450 451 458 public int indexOf(Object element) { 459 460 if (fast) { 461 return (list.indexOf(element)); 462 } else { 463 synchronized (list) { 464 return (list.indexOf(element)); 465 } 466 } 467 468 } 469 470 471 474 public boolean isEmpty() { 475 476 if (fast) { 477 return (list.isEmpty()); 478 } else { 479 synchronized (list) { 480 return (list.isEmpty()); 481 } 482 } 483 484 } 485 486 487 496 public Iterator iterator() { 497 if (fast) { 498 return new ListIter(0); 499 } else { 500 return list.iterator(); 501 } 502 } 503 504 505 512 public int lastIndexOf(Object element) { 513 514 if (fast) { 515 return (list.lastIndexOf(element)); 516 } else { 517 synchronized (list) { 518 return (list.lastIndexOf(element)); 519 } 520 } 521 522 } 523 524 525 529 public ListIterator listIterator() { 530 if (fast) { 531 return new ListIter(0); 532 } else { 533 return list.listIterator(); 534 } 535 } 536 537 538 547 public ListIterator listIterator(int index) { 548 if (fast) { 549 return new ListIter(index); 550 } else { 551 return list.listIterator(index); 552 } 553 } 554 555 556 564 public Object remove(int index) { 565 566 if (fast) { 567 synchronized (this) { 568 ArrayList temp = (ArrayList ) list.clone(); 569 Object result = temp.remove(index); 570 list = temp; 571 return (result); 572 } 573 } else { 574 synchronized (list) { 575 return (list.remove(index)); 576 } 577 } 578 579 } 580 581 582 588 public boolean remove(Object element) { 589 590 if (fast) { 591 synchronized (this) { 592 ArrayList temp = (ArrayList ) list.clone(); 593 boolean result = temp.remove(element); 594 list = temp; 595 return (result); 596 } 597 } else { 598 synchronized (list) { 599 return (list.remove(element)); 600 } 601 } 602 603 } 604 605 606 615 public boolean removeAll(Collection collection) { 616 617 if (fast) { 618 synchronized (this) { 619 ArrayList temp = (ArrayList ) list.clone(); 620 boolean result = temp.removeAll(collection); 621 list = temp; 622 return (result); 623 } 624 } else { 625 synchronized (list) { 626 return (list.removeAll(collection)); 627 } 628 } 629 630 } 631 632 633 642 public boolean retainAll(Collection collection) { 643 644 if (fast) { 645 synchronized (this) { 646 ArrayList temp = (ArrayList ) list.clone(); 647 boolean result = temp.retainAll(collection); 648 list = temp; 649 return (result); 650 } 651 } else { 652 synchronized (list) { 653 return (list.retainAll(collection)); 654 } 655 } 656 657 } 658 659 660 673 public Object set(int index, Object element) { 674 675 if (fast) { 676 return (list.set(index, element)); 677 } else { 678 synchronized (list) { 679 return (list.set(index, element)); 680 } 681 } 682 683 } 684 685 686 689 public int size() { 690 691 if (fast) { 692 return (list.size()); 693 } else { 694 synchronized (list) { 695 return (list.size()); 696 } 697 } 698 699 } 700 701 702 714 public List subList(int fromIndex, int toIndex) { 715 if (fast) { 716 return new SubList(fromIndex, toIndex); 717 } else { 718 return list.subList(fromIndex, toIndex); 719 } 720 } 721 722 723 727 public Object [] toArray() { 728 729 if (fast) { 730 return (list.toArray()); 731 } else { 732 synchronized (list) { 733 return (list.toArray()); 734 } 735 } 736 737 } 738 739 740 752 public Object [] toArray(Object array[]) { 753 754 if (fast) { 755 return (list.toArray(array)); 756 } else { 757 synchronized (list) { 758 return (list.toArray(array)); 759 } 760 } 761 762 } 763 764 765 768 public String toString() { 769 770 StringBuffer sb = new StringBuffer ("FastArrayList["); 771 sb.append(list.toString()); 772 sb.append("]"); 773 return (sb.toString()); 774 775 } 776 777 778 783 public void trimToSize() { 784 785 if (fast) { 786 synchronized (this) { 787 ArrayList temp = (ArrayList ) list.clone(); 788 temp.trimToSize(); 789 list = temp; 790 } 791 } else { 792 synchronized (list) { 793 list.trimToSize(); 794 } 795 } 796 797 } 798 799 800 801 private class SubList implements List { 802 803 private int first; 804 private int last; 805 private List expected; 806 807 808 public SubList(int first, int last) { 809 this.first = first; 810 this.last = last; 811 this.expected = list; 812 } 813 814 private List get(List l) { 815 if (list != expected) { 816 throw new ConcurrentModificationException (); 817 } 818 return l.subList(first, last); 819 } 820 821 public void clear() { 822 if (fast) { 823 synchronized (FastArrayList.this) { 824 ArrayList temp = (ArrayList ) list.clone(); 825 get(temp).clear(); 826 last = first; 827 list = temp; 828 expected = temp; 829 } 830 } else { 831 synchronized (list) { 832 get(expected).clear(); 833 } 834 } 835 } 836 837 public boolean remove(Object o) { 838 if (fast) { 839 synchronized (FastArrayList.this) { 840 ArrayList temp = (ArrayList ) list.clone(); 841 boolean r = get(temp).remove(o); 842 if (r) last--; 843 list = temp; 844 expected = temp; 845 return r; 846 } 847 } else { 848 synchronized (list) { 849 return get(expected).remove(o); 850 } 851 } 852 } 853 854 public boolean removeAll(Collection o) { 855 if (fast) { 856 synchronized (FastArrayList.this) { 857 ArrayList temp = (ArrayList ) list.clone(); 858 List sub = get(temp); 859 boolean r = sub.removeAll(o); 860 if (r) last = first + sub.size(); 861 list = temp; 862 expected = temp; 863 return r; 864 } 865 } else { 866 synchronized (list) { 867 return get(expected).removeAll(o); 868 } 869 } 870 } 871 872 public boolean retainAll(Collection o) { 873 if (fast) { 874 synchronized (FastArrayList.this) { 875 ArrayList temp = (ArrayList ) list.clone(); 876 List sub = get(temp); 877 boolean r = sub.retainAll(o); 878 if (r) last = first + sub.size(); 879 list = temp; 880 expected = temp; 881 return r; 882 } 883 } else { 884 synchronized (list) { 885 return get(expected).retainAll(o); 886 } 887 } 888 } 889 890 public int size() { 891 if (fast) { 892 return get(expected).size(); 893 } else { 894 synchronized (list) { 895 return get(expected).size(); 896 } 897 } 898 } 899 900 901 public boolean isEmpty() { 902 if (fast) { 903 return get(expected).isEmpty(); 904 } else { 905 synchronized (list) { 906 return get(expected).isEmpty(); 907 } 908 } 909 } 910 911 public boolean contains(Object o) { 912 if (fast) { 913 return get(expected).contains(o); 914 } else { 915 synchronized (list) { 916 return get(expected).contains(o); 917 } 918 } 919 } 920 921 public boolean containsAll(Collection
|