1 11 12 72 73 74 package org.logicalcobwebs.proxool.util; 75 76 77 import java.util.ArrayList ; 78 import java.util.Collection ; 79 import java.util.ConcurrentModificationException ; 80 import java.util.Iterator ; 81 import java.util.List ; 82 import java.util.ListIterator ; 83 84 85 124 125 public class FastArrayList extends ArrayList { 126 127 128 130 131 134 public FastArrayList() { 135 136 super(); 137 this.list = new ArrayList (); 138 139 } 140 141 142 147 public FastArrayList(int capacity) { 148 149 super(); 150 this.list = new ArrayList (capacity); 151 152 } 153 154 155 162 public FastArrayList(Collection collection) { 163 164 super(); 165 this.list = new ArrayList (collection); 166 167 } 168 169 170 172 173 176 private ArrayList list = null; 177 178 179 181 182 185 private boolean fast = false; 186 187 188 193 public boolean getFast() { 194 return (this.fast); 195 } 196 197 202 public void setFast(boolean fast) { 203 this.fast = fast; 204 } 205 206 207 209 210 215 public boolean add(Object element) { 216 217 if (fast) { 218 synchronized (this) { 219 ArrayList temp = (ArrayList ) list.clone(); 220 boolean result = temp.add(element); 221 list = temp; 222 return (result); 223 } 224 } else { 225 synchronized (list) { 226 return (list.add(element)); 227 } 228 } 229 230 } 231 232 233 242 public void add(int index, Object element) { 243 244 if (fast) { 245 synchronized (this) { 246 ArrayList temp = (ArrayList ) list.clone(); 247 temp.add(index, element); 248 list = temp; 249 } 250 } else { 251 synchronized (list) { 252 list.add(index, element); 253 } 254 } 255 256 } 257 258 259 266 public boolean addAll(Collection collection) { 267 268 if (fast) { 269 synchronized (this) { 270 ArrayList temp = (ArrayList ) list.clone(); 271 boolean result = temp.addAll(collection); 272 list = temp; 273 return (result); 274 } 275 } else { 276 synchronized (list) { 277 return (list.addAll(collection)); 278 } 279 } 280 281 } 282 283 284 294 public boolean addAll(int index, Collection collection) { 295 296 if (fast) { 297 synchronized (this) { 298 ArrayList temp = (ArrayList ) list.clone(); 299 boolean result = temp.addAll(index, collection); 300 list = temp; 301 return (result); 302 } 303 } else { 304 synchronized (list) { 305 return (list.addAll(index, collection)); 306 } 307 } 308 309 } 310 311 312 319 public void clear() { 320 321 if (fast) { 322 synchronized (this) { 323 ArrayList temp = (ArrayList ) list.clone(); 324 temp.clear(); 325 list = temp; 326 } 327 } else { 328 synchronized (list) { 329 list.clear(); 330 } 331 } 332 333 } 334 335 336 340 public Object clone() { 341 342 FastArrayList results = null; 343 if (fast) { 344 results = new FastArrayList(list); 345 } else { 346 synchronized (list) { 347 results = new FastArrayList(list); 348 } 349 } 350 results.setFast(getFast()); 351 return (results); 352 353 } 354 355 356 361 public boolean contains(Object element) { 362 363 if (fast) { 364 return (list.contains(element)); 365 } else { 366 synchronized (list) { 367 return (list.contains(element)); 368 } 369 } 370 371 } 372 373 374 380 public boolean containsAll(Collection collection) { 381 382 if (fast) { 383 return (list.containsAll(collection)); 384 } else { 385 synchronized (list) { 386 return (list.containsAll(collection)); 387 } 388 } 389 390 } 391 392 393 400 public void ensureCapacity(int capacity) { 401 402 if (fast) { 403 synchronized (this) { 404 ArrayList temp = (ArrayList ) list.clone(); 405 temp.ensureCapacity(capacity); 406 list = temp; 407 } 408 } else { 409 synchronized (list) { 410 list.ensureCapacity(capacity); 411 } 412 } 413 414 } 415 416 417 425 public boolean equals(Object o) { 426 427 if (o == this) { 429 return (true); 430 } else if (!(o instanceof List )) { 431 return (false); 432 } 433 List lo = (List ) o; 434 435 if (fast) { 437 ListIterator li1 = list.listIterator(); 438 ListIterator li2 = lo.listIterator(); 439 while (li1.hasNext() && li2.hasNext()) { 440 Object o1 = li1.next(); 441 Object o2 = li2.next(); 442 if (!(o1 == null ? o2 == null : o1.equals(o2))) { 443 return (false); 444 } 445 } 446 return (!(li1.hasNext() || li2.hasNext())); 447 } else { 448 synchronized (list) { 449 ListIterator li1 = list.listIterator(); 450 ListIterator li2 = lo.listIterator(); 451 while (li1.hasNext() && li2.hasNext()) { 452 Object o1 = li1.next(); 453 Object o2 = li2.next(); 454 if (!(o1 == null ? o2 == null : o1.equals(o2))) { 455 return (false); 456 } 457 } 458 return (!(li1.hasNext() || li2.hasNext())); 459 } 460 } 461 462 } 463 464 465 472 public Object get(int index) { 473 474 if (fast) { 475 return (list.get(index)); 476 } else { 477 synchronized (list) { 478 return (list.get(index)); 479 } 480 } 481 482 } 483 484 485 490 public int hashCode() { 491 492 if (fast) { 493 int hashCode = 1; 494 java.util.Iterator i = list.iterator(); 495 while (i.hasNext()) { 496 Object o = i.next(); 497 hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode()); 498 } 499 return (hashCode); 500 } else { 501 synchronized (list) { 502 int hashCode = 1; 503 java.util.Iterator i = list.iterator(); 504 while (i.hasNext()) { 505 Object o = i.next(); 506 hashCode = 31 * hashCode + (o == null ? 0 : o.hashCode()); 507 } 508 return (hashCode); 509 } 510 } 511 512 } 513 514 515 522 public int indexOf(Object element) { 523 524 if (fast) { 525 return (list.indexOf(element)); 526 } else { 527 synchronized (list) { 528 return (list.indexOf(element)); 529 } 530 } 531 532 } 533 534 535 538 public boolean isEmpty() { 539 540 if (fast) { 541 return (list.isEmpty()); 542 } else { 543 synchronized (list) { 544 return (list.isEmpty()); 545 } 546 } 547 548 } 549 550 551 560 public Iterator iterator() { 561 if (fast) { 562 return new ListIter(0); 563 } else { 564 return list.iterator(); 565 } 566 } 567 568 569 576 public int lastIndexOf(Object element) { 577 578 if (fast) { 579 return (list.lastIndexOf(element)); 580 } else { 581 synchronized (list) { 582 return (list.lastIndexOf(element)); 583 } 584 } 585 586 } 587 588 589 593 public ListIterator listIterator() { 594 if (fast) { 595 return new ListIter(0); 596 } else { 597 return list.listIterator(); 598 } 599 } 600 601 602 611 public ListIterator listIterator(int index) { 612 if (fast) { 613 return new ListIter(index); 614 } else { 615 return list.listIterator(index); 616 } 617 } 618 619 620 628 public Object remove(int index) { 629 630 if (fast) { 631 synchronized (this) { 632 ArrayList temp = (ArrayList ) list.clone(); 633 Object result = temp.remove(index); 634 list = temp; 635 return (result); 636 } 637 } else { 638 synchronized (list) { 639 return (list.remove(index)); 640 } 641 } 642 643 } 644 645 646 652 public boolean remove(Object element) { 653 654 if (fast) { 655 synchronized (this) { 656 ArrayList temp = (ArrayList ) list.clone(); 657 boolean result = temp.remove(element); 658 list = temp; 659 return (result); 660 } 661 } else { 662 synchronized (list) { 663 return (list.remove(element)); 664 } 665 } 666 667 } 668 669 670 679 public boolean removeAll(Collection collection) { 680 681 if (fast) { 682 synchronized (this) { 683 ArrayList temp = (ArrayList ) list.clone(); 684 boolean result = temp.removeAll(collection); 685 list = temp; 686 return (result); 687 } 688 } else { 689 synchronized (list) { 690 return (list.removeAll(collection)); 691 } 692 } 693 694 } 695 696 697 706 public boolean retainAll(Collection collection) { 707 708 if (fast) { 709 synchronized (this) { 710 ArrayList temp = (ArrayList ) list.clone(); 711 boolean result = temp.retainAll(collection); 712 list = temp; 713 return (result); 714 } 715 } else { 716 synchronized (list) { 717 return (list.retainAll(collection)); 718 } 719 } 720 721 } 722 723 724 737 public Object set(int index, Object element) { 738 739 if (fast) { 740 return (list.set(index, element)); 741 } else { 742 synchronized (list) { 743 return (list.set(index, element)); 744 } 745 } 746 747 } 748 749 750 753 public int size() { 754 755 if (fast) { 756 return (list.size()); 757 } else { 758 synchronized (list) { 759 return (list.size()); 760 } 761 } 762 763 } 764 765 766 778 public List subList(int fromIndex, int toIndex) { 779 if (fast) { 780 return new SubList(fromIndex, toIndex); 781 } else { 782 return list.subList(fromIndex, toIndex); 783 } 784 } 785 786 787 791 public Object [] toArray() { 792 793 if (fast) { 794 return (list.toArray()); 795 } else { 796 synchronized (list) { 797 return (list.toArray()); 798 } 799 } 800 801 } 802 803 804 816 public Object [] toArray(Object array[]) { 817 818 if (fast) { 819 return (list.toArray(array)); 820 } else { 821 synchronized (list) { 822 return (list.toArray(array)); 823 } 824 } 825 826 } 827 828 829 832 public String toString() { 833 834 StringBuffer sb = new StringBuffer ("FastArrayList["); 835 sb.append(list.toString()); 836 sb.append("]"); 837 return (sb.toString()); 838 839 } 840 841 842 847 public void trimToSize() { 848 849 if (fast) { 850 synchronized (this) { 851 ArrayList temp = (ArrayList ) list.clone(); 852 temp.trimToSize(); 853 list = temp; 854 } 855 } else { 856 synchronized (list) { 857 list.trimToSize(); 858 } 859 } 860 861 } 862 863 864 private class SubList implements List { 865 866 private int first; 867 private int last; 868 private List expected; 869 870 871 public SubList(int first, int last) { 872 this.first = first; 873 this.last = last; 874 this.expected = list; 875 } 876 877 private List get(List l) { 878 if (list != expected) { 879 throw new ConcurrentModificationException (); 880 } 881 return l.subList(first, last); 882 } 883 884 public void clear() { 885 if (fast) { 886 synchronized (FastArrayList.this) { 887 ArrayList temp = (ArrayList ) list.clone(); 888 get(temp).clear(); 889 last = first; 890 list = temp; 891 expected = temp; 892 } 893 } else { 894 synchronized (list) { 895 get(expected).clear(); 896 } 897 } 898 } 899 900 public boolean remove(Object o) { 901 if (fast) { 902 synchronized (FastArrayList.this) { 903 ArrayList temp = (ArrayList ) list.clone(); 904 boolean r = get(temp).remove(o); 905 if (r) { 906 last--; 907 } 908 list = temp; 909 expected = temp; 910 return r; 911 } 912 } else { 913 synchronized (list) { 914 return get(expected).remove(o); 915 } 916 } 917 } 918 919 public boolean removeAll(Collection o) { 920 if (fast) { 921 synchronized (FastArrayList.this) { 922 ArrayList temp = (ArrayList ) list.clone(); 923 List sub = get(temp); 924 boolean r = sub.removeAll(o); 925 if (r) { 926 last = first + sub.size(); 927 } 928 list = temp; 929 expected = temp; 930 return r; 931 } 932 } else { 933 synchronized (list) { 934 return get(expected).removeAll(o); 935 } 936 } 937 } 938 939 public boolean retainAll(Collection o) { 940 if (fast) { 941 synchronized (FastArrayList.this) { 942 ArrayList temp = (ArrayList ) list.clone(); 943 List sub = get(temp); 944 boolean r = sub.retainAll(o); 945 if (r) { 946 last = first + sub.size(); 947 } 948 list = temp; 949 expected = temp; 950 return r; 951 } 952 } else { 953 synchronized (list) { 954 return get(expected).retainAll(o); 955 } 956 } 957 } 958 959 public int size() { 960 if (fast) { 961 return get(expected).size(); 962 } else { 963 synchronized (list) { 964 return get(expected).size(); 965 } 966 } 967 } 968 969 970 public boolean isEmpty() { 971 if (fast) { 972 return get(expected).isEmpty(); 973 } else { 974 synchronized (list) { 975 return get(expected).isEmpty(); 976 } 977 } 978 } 979 980 public boolean contains(Object o) { 981 if (fast) { 982 return get(expected).contains(o); 983 } else { 984 synchronized (list) { 985 return get(expected).contains(o); 986 } 987 } 988 } 989 990 public boolean containsAll(Collection o) { 991 if (fast) { 992 return get(expected).containsAll(o); 993 } else { 994 synchronized (list) { 995 return get(expected).containsAll(o); 996 } 997 } 998 } 999 1000 public Object [] toArray(Object [] o) { 1001 if (fast) { 1002 return get(expected).toArray(o); 1003 } else { 1004 synchronized (list) { 1005 return get(expected).toArray(o); 1006 } 1007 } 1008 } 1009 1010 public Object [] toArray() { 1011 if (fast) { 1012 return get(expected).toArray(); 1013 } else { 1014 synchronized (list) { 1015 return get(expected).toArray(); 1016 } 1017 } 1018 } 1019 1020 1021 public boolean equals(Object o) { 1022 if (o == this) { 1023 return true; 1024 } 1025 if (fast) { 1026 return get(expected).equals(o); 1027 } else { 1028 synchronized (list) { 1029 return get(expected).equals(o); 1030 } 1031 } 1032 } 1033 1034 public int hashCode() { 1035 if (fast) { 1036 return get(expected).hashCode(); 1037 } else { 1038 synchronized (list) { 1039 return get(expected).hashCode(); 1040 } 1041 } 1042 } 1043 1044 public boolean add(Object o) { 1045 if (fast) { 1046 synchronized (FastArrayList.this) { 1047 ArrayList temp = (ArrayList ) list.clone(); 1048 boolean r = get(temp).add(o); 1049 if (r) { 1050 last++; 1051 } 1052 list = temp; 1053 expected = temp; 1054 return r; 1055 } 1056 } else { 1057 synchronized (list) { 1058 return get(expected).add(o); 1059 } 1060 } 1061 } 1062 1063 public boolean addAll(Collection o) { 1064 if (fast) { 1065 synchronized (FastArrayList.this) { 1066 ArrayList temp = (ArrayList ) list.clone(); 1067 boolean r = get(temp).addAll(o); 1068 if (r) { 1069 last += o.size(); 1070 } 1071 list = temp; 1072 expected = temp; 1073 return r; 1074 } 1075 } else { 1076 synchronized (list) { 1077 return get(expected).addAll(o); 1078 } 1079 } 1080 } 1081 1082 public void add(int i, Object o) { 1083 if (fast) { 1084 synchronized (FastArrayList.this) { 1085 ArrayList temp = (ArrayList ) list.clone(); 1086 get(temp).add(i, o); 1087 last++; 1088 list = temp; 1089 expected = temp; 1090 } 1091 } else { 1092 synchronized (list) { 1093 get(expected).add(i, o); 1094 } 1095 } 1096 } 1097 1098 public boolean addAll(int i, Collection o) { 1099 if (fast) { 1100 synchronized (FastArrayList.this) { 1101 ArrayList temp = (ArrayList ) list.clone(); 1102 boolean r = get(temp).addAll(i, o); 1103 list = temp; 1104 if (r) { 1105 last += o.size(); 1106 } 1107 expected = temp; 1108 return r; 1109 } 1110 } else { 1111 synchronized (list) { 1112 return get(expected).addAll(i, o); 1113 } 1114 } 1115 } 1116 1117 public Object remove(int i) { 1118 if (fast) { 1119 synchronized (FastArrayList.this) { 1120 ArrayList temp = (ArrayList ) list.clone(); 1121 Object o = get(temp).remove(i); 1122 last--; 1123 list = temp; 1124 expected = temp; 1125 return o; 1126 } 1127 } else { 1128 synchronized (list) { 1129 return get(expected).remove(i); 1130 } 1131 } 1132 } 1133 1134 public Object set(int i, Object a) { 1135 if (fast) { 1136 synchronized (FastArrayList.this) { 1137 ArrayList temp = (ArrayList ) list.clone(); 1138 Object o = get(temp).set(i, a); 1139 list = temp; 1140 expected = temp; 1141 return o; 1142 } 1143 } else { 1144 synchronized (list) { 1145 return get(expected).set(i, a); 1146 } 1147 } 1148 } 1149 1150 1151 public Iterator iterator() { 1152 return new SubListIter(0); 1153 } 1154 1155 public ListIterator listIterator() { 1156 return new SubListIter(0); 1157 } 1158 1159 public ListIterator listIterator(int i) { 1160 return new SubListIter(i); 1161 } 1162 1163 1164 public Object get(int i) { 1165 if (fast) { 1166 return get(expected).get(i); 1167 } else { 1168 synchronized (list) { 1169 return get(expected).get(i); 1170 } 1171 } 1172 } 1173 1174 public int indexOf(Object o) { 1175 if (fast) { 1176 return get(expected).indexOf(o); 1177 } else { 1178 synchronized (list) { 1179 return get(expected).indexOf(o); 1180 } 1181 } 1182 } 1183 1184 1185 public int lastIndexOf(Object o) { 1186 if (fast) { 1187 return get(expected).lastIndexOf(o); 1188 } else { 1189 synchronized (list) { 1190 return get(expected).lastIndexOf(o); 1191 } 1192 } 1193 } 1194 1195 1196 public List subList(int f, int l) { 1197 if (list != expected) { 1198 throw new ConcurrentModificationException (); 1199 } 1200 return new SubList(first + f, f + l); 1201 } 1202 1203 1204 private class SubListIter implements ListIterator { 1205 1206 private List expected; 1207 private ListIterator iter; 1208 private int lastReturnedIndex = -1; 1209 1210 1211 public SubListIter(int i) { 1212 this.expected = list; 1213 this.iter = SubList.this.get(expected).listIterator(i); 1214 } 1215 1216 private void checkMod() { 1217 if (list != expected) { 1218 throw new ConcurrentModificationException (); 1219 } 1220 } 1221 1222 List get() { 1223 return SubList.this.get(expected); 1224 } 1225 1226 public boolean hasNext() { 1227 checkMod(); 1228 return iter.hasNext(); 1229 } 1230 1231 public Object next() { 1232 checkMod(); 1233 lastReturnedIndex = iter.nextIndex(); 1234 return iter.next(); 1235 } 1236 1237 public boolean hasPrevious() { 1238 checkMod(); 1239 return iter.hasPrevious(); 1240 } 1241 1242 public Object previous() { 1243 checkMod(); 1244 lastReturnedIndex = iter.previousIndex(); 1245 return iter.previous(); 1246 } 1247 1248 public int previousIndex() { 1249 checkMod(); 1250 return iter.previousIndex(); 1251 } 1252 1253 public int nextIndex() { 1254 checkMod(); 1255 return iter.nextIndex(); 1256 } 1257 1258 public void remove() { 1259 checkMod(); 1260 if (lastReturnedIndex < 0) { 1261 throw new IllegalStateException (); 1262 } 1263 get().remove(lastReturnedIndex); 1264 last--; 1265 expected = list; 1266 iter = get().listIterator(previousIndex()); 1267 lastReturnedIndex = -1; 1268 } 1269 1270 public void set(Object o) { 1271 checkMod(); 1272 if (lastReturnedIndex < 0) { 1273 throw new IllegalStateException (); 1274 } 1275 get().set(lastReturnedIndex, o); 1276 expected = list; 1277 iter = get().listIterator(previousIndex() + 1); 1278 } 1279 1280 public void add(Object o) { 1281 checkMod(); 1282 int i = nextIndex(); 1283 get().add(i, o); 1284 last++; 1285 iter = get().listIterator(i + 1); 1286 lastReturnedIndex = 1; 1287 } 1288 1289 } 1290 1291 1292 } 1293 1294 1295 private class ListIter implements ListIterator { 1296 1297 private List expected; 1298 private ListIterator iter; 1299 private int lastReturnedIndex = -1; 1300 1301 1302 public ListIter(int i) { 1303 this.expected = list; 1304 this.iter = get().listIterator(i); 1305 } 1306 1307 private void checkMod() { 1308 if (list != expected) { 1309 throw new ConcurrentModificationException (); 1310 } 1311 } 1312 1313 List get() { 1314 return expected; 1315 } 1316 1317 public boolean hasNext() { 1318 checkMod(); 1319 return iter.hasNext(); 1320 } 1321 1322 public Object next() { 1323 checkMod(); 1324 lastReturnedIndex = iter.nextIndex(); 1325 return iter.next(); 1326 } 1327 1328 public boolean hasPrevious() { 1329 checkMod(); 1330 return iter.hasPrevious(); 1331 } 1332 1333 public Object previous() { 1334 checkMod(); 1335 lastReturnedIndex = iter.previousIndex(); 1336 return iter.previous(); 1337 } 1338 1339 public int previousIndex() { 1340 checkMod(); 1341 return iter.previousIndex(); 1342 } 1343 1344 public int nextIndex() { 1345 checkMod(); 1346 return iter.nextIndex(); 1347 } 1348 1349 public void remove() { 1350 checkMod(); 1351 if (lastReturnedIndex < 0) { 1352 throw new IllegalStateException (); 1353 } 1354 get().remove(lastReturnedIndex); 1355 expected = list; 1356 iter = get().listIterator(previousIndex()); 1357 lastReturnedIndex = -1; 1358 } 1359 1360 public void set(Object o) { 1361 checkMod(); 1362 if (lastReturnedIndex < 0) { 1363 throw new IllegalStateException (); 1364 } 1365 get().set(lastReturnedIndex, o); 1366 expected = list; 1367 iter = get().listIterator(previousIndex() + 1); 1368 } 1369 1370 public void add(Object o) { 1371 checkMod(); 1372 int i = nextIndex(); 1373 get().add(i, o); 1374 iter = get().listIterator(i + 1); 1375 lastReturnedIndex = 1; 1376 } 1377 1378 } 1379} 1380 1381 1408 | Popular Tags |