1 17 package org.eclipse.emf.common.util; 18 19 20 import java.io.Serializable ; 21 import java.util.AbstractList ; 22 import java.util.Collection ; 23 import java.util.ConcurrentModificationException ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ListIterator ; 27 import java.util.NoSuchElementException ; 28 29 30 33 public abstract class DelegatingEList extends AbstractList implements EList, Cloneable , Serializable 34 { 35 38 public DelegatingEList() 39 { 40 } 41 42 46 public DelegatingEList(Collection collection) 47 { 48 addAll(collection); 49 } 50 51 57 protected boolean useEquals() 58 { 59 return true; 60 } 61 62 66 protected boolean equalObjects(Object firstObject, Object secondObject) 67 { 68 return 69 useEquals() && firstObject != null ? 70 firstObject.equals(secondObject) : 71 firstObject == secondObject; 72 } 73 74 79 protected boolean canContainNull() 80 { 81 return true; 82 } 83 84 90 protected boolean isUnique() 91 { 92 return false; 93 } 94 95 105 protected Object validate(int index, Object object) 106 { 107 if (!canContainNull() && object == null) 108 { 109 throw new IllegalArgumentException ("The 'no null' constraint is violated"); 110 } 111 112 return object; 113 } 114 115 123 protected Object resolve(int index, Object object) 124 { 125 return object; 126 } 127 128 136 protected void didSet(int index, Object newObject, Object oldObject) 137 { 138 } 139 140 147 protected void didAdd(int index, Object newObject) 148 { 149 } 150 151 158 protected void didRemove(int index, Object oldObject) 159 { 160 } 161 162 170 protected void didClear(int size, Object [] oldObjects) 171 { 172 if (oldObjects != null) 173 { 174 for (int i = 0; i < size; ++i) 175 { 176 didRemove(i, oldObjects[i]); 177 } 178 } 179 } 180 181 189 protected void didMove(int index, Object movedObject, int oldIndex) 190 { 191 } 192 193 198 protected void didChange() 199 { 200 } 201 202 206 protected abstract List delegateList(); 207 208 212 public int size() 213 { 214 return delegateSize(); 215 } 216 217 221 protected int delegateSize() 222 { 223 return delegateList().size(); 224 } 225 226 230 public boolean isEmpty() 231 { 232 return delegateIsEmpty(); 233 } 234 235 239 protected boolean delegateIsEmpty() 240 { 241 return delegateList().isEmpty(); 242 } 243 244 249 public boolean contains(Object object) 250 { 251 return delegateContains(object); 252 } 253 254 259 protected boolean delegateContains(Object object) 260 { 261 return delegateList().contains(object); 262 } 263 264 270 public boolean containsAll(Collection collection) 271 { 272 return delegateContainsAll(collection); 273 } 274 275 281 protected boolean delegateContainsAll(Collection collection) 282 { 283 return delegateList().containsAll(collection); 284 } 285 286 291 public int indexOf(Object object) 292 { 293 return delegateIndexOf(object); 294 } 295 296 301 protected int delegateIndexOf(Object object) 302 { 303 return delegateList().indexOf(object); 304 } 305 306 311 public int lastIndexOf(Object object) 312 { 313 return delegateLastIndexOf(object); 314 } 315 316 321 protected int delegateLastIndexOf(Object object) 322 { 323 return delegateList().lastIndexOf(object); 324 } 325 326 330 public Object [] toArray() 331 { 332 return delegateToArray(); 333 } 334 335 339 protected Object [] delegateToArray() 340 { 341 return delegateList().toArray(); 342 } 343 344 350 public Object [] toArray(Object array[]) 351 { 352 return delegateToArray(array); 353 } 354 355 361 protected Object [] delegateToArray(Object array[]) 362 { 363 return delegateList().toArray(array); 364 } 365 366 376 public Object get(int index) 377 { 378 return resolve(index, delegateGet(index)); 379 } 380 381 387 protected Object delegateGet(int index) 388 { 389 return delegateList().get(index); 390 } 391 392 400 protected Object basicGet(int index) 401 { 402 return delegateGet(index); 403 } 404 405 417 public Object set(int index, Object object) 418 { 419 if (isUnique()) 420 { 421 int currentIndex = indexOf(object); 422 if (currentIndex >= 0 && currentIndex != index) 423 { 424 throw new IllegalArgumentException ("The 'no duplicates' constraint is violated"); 425 } 426 } 427 428 return setUnique(index, object); 429 } 430 431 441 public Object setUnique(int index, Object object) 442 { 443 Object oldObject = delegateSet(index, validate(index, object)); 444 didSet(index, object, oldObject); 445 didChange(); 446 return oldObject; 447 } 448 449 455 protected Object delegateSet(int index, Object object) 456 { 457 return delegateList().set(index, object); 458 } 459 460 471 public boolean add(Object object) 472 { 473 if (isUnique() && contains(object)) 474 { 475 return false; 476 } 477 else 478 { 479 addUnique(object); 480 return true; 481 } 482 } 483 484 492 public void addUnique(Object object) 493 { 494 ++modCount; 495 496 int size = size(); 497 delegateAdd(validate(size, object)); 498 didAdd(size, object); 499 didChange(); 500 } 501 502 506 protected void delegateAdd(Object object) 507 { 508 delegateList().add(object); 509 } 510 511 522 public void add(int index, Object object) 523 { 524 if (isUnique() && contains(object)) 525 { 526 throw new IllegalArgumentException ("The 'no duplicates' constraint is violated"); 527 } 528 529 addUnique(index, object); 530 } 531 532 539 public void addUnique(int index, Object object) 540 { 541 ++modCount; 542 543 delegateAdd(index, validate(index, object)); 544 didAdd(index, object); 545 didChange(); 546 } 547 548 552 protected void delegateAdd(int index, Object object) 553 { 554 delegateList().add(index, object); 555 } 556 557 567 public boolean addAll(Collection collection) 568 { 569 if (isUnique()) 570 { 571 collection = getNonDuplicates(collection); 572 } 573 return addAllUnique(collection); 574 } 575 576 583 public boolean addAllUnique(Collection collection) 584 { 585 ++modCount; 586 587 if (collection.isEmpty()) 588 { 589 return false; 590 } 591 else 592 { 593 int i = size(); 594 for (Iterator objects = collection.iterator(); objects.hasNext(); ++i) 595 { 596 Object object = objects.next(); 597 delegateAdd(validate(i, object)); 598 didAdd(i, object); 599 didChange(); 600 } 601 602 return true; 603 } 604 } 605 606 619 public boolean addAll(int index, Collection collection) 620 { 621 if (isUnique()) 622 { 623 collection = getNonDuplicates(collection); 624 } 625 return addAllUnique(index, collection); 626 } 627 628 638 public boolean addAllUnique(int index, Collection collection) 639 { 640 ++modCount; 641 642 if (collection.isEmpty()) 643 { 644 return false; 645 } 646 else 647 { 648 for (Iterator objects = collection.iterator(); objects.hasNext(); ++index) 649 { 650 Object object = objects.next(); 651 delegateAdd(index, validate(index, object)); 652 didAdd(index, object); 653 didChange(); 654 } 655 656 return true; 657 } 658 } 659 660 668 public boolean remove(Object object) 669 { 670 int index = indexOf(object); 671 if (index >= 0) 672 { 673 remove(index); 674 return true; 675 } 676 else 677 { 678 return false; 679 } 680 } 681 682 687 public boolean removeAll(Collection collection) 688 { 689 boolean modified = false; 690 for (ListIterator i = delegateListIterator(); i.hasNext(); ) 691 { 692 if (collection.contains(i.next())) 693 { 694 remove(i.previousIndex()); 695 modified = true; 696 } 697 } 698 699 return modified; 700 } 701 702 709 public Object remove(int index) 710 { 711 ++modCount; 712 713 Object oldObject = delegateRemove(index); 714 didRemove(index, oldObject); 715 didChange(); 716 717 return oldObject; 718 } 719 720 725 protected Object delegateRemove(int index) 726 { 727 return delegateList().remove(index); 728 } 729 730 738 public boolean retainAll(Collection collection) 739 { 740 boolean modified = false; 741 for (ListIterator i = delegateListIterator(); i.hasNext(); ) 742 { 743 if (!collection.contains(i.next())) 744 { 745 remove(i.previousIndex()); 746 modified = true; 747 } 748 } 749 return modified; 750 } 751 752 755 public void clear() 756 { 757 doClear(size(), delegateToArray()); 758 } 759 760 765 protected void doClear(int oldSize, Object [] oldData) 766 { 767 ++modCount; 768 769 delegateClear(); 770 771 didClear(oldSize, oldData); 772 didChange(); 773 } 774 775 778 protected void delegateClear() 779 { 780 delegateList().clear(); 781 } 782 783 791 public void move(int index, Object object) 792 { 793 move(index, indexOf(object)); 794 } 795 796 805 public Object move(int targetIndex, int sourceIndex) 806 { 807 ++modCount; 808 int size = size(); 809 if (targetIndex >= size || targetIndex < 0) 810 throw new IndexOutOfBoundsException ("targetIndex=" + targetIndex + ", size=" + size); 811 812 if (sourceIndex >= size || sourceIndex < 0) 813 throw new IndexOutOfBoundsException ("sourceIndex=" + sourceIndex + ", size=" + size); 814 815 Object object = delegateGet(sourceIndex); 816 if (targetIndex != sourceIndex) 817 { 818 delegateAdd(targetIndex, delegateRemove(sourceIndex)); 819 didMove(targetIndex, object, sourceIndex); 820 didChange(); 821 } 822 return object; 823 } 824 825 831 public boolean equals(Object object) 832 { 833 return delegateEquals(object); 834 } 835 836 840 protected boolean delegateEquals(Object object) 841 { 842 return delegateList().equals(object); 843 } 844 845 849 public int hashCode() 850 { 851 return delegateHashCode(); 852 } 853 854 858 protected int delegateHashCode() 859 { 860 return delegateList().hashCode(); 861 } 862 863 867 public String toString() 868 { 869 return delegateToString(); 870 } 871 872 876 protected String delegateToString() 877 { 878 return delegateList().toString(); 879 } 880 881 887 public Iterator iterator() 888 { 889 return new EIterator(); 890 } 891 892 896 protected Iterator delegateIterator() 897 { 898 return delegateList().iterator(); 899 } 900 901 904 protected class EIterator implements Iterator 905 { 906 909 protected int cursor = 0; 910 911 914 protected int lastCursor = -1; 915 916 919 protected int expectedModCount = modCount; 920 921 925 public boolean hasNext() 926 { 927 return cursor != size(); 928 } 929 930 936 public Object next() 937 { 938 try 939 { 940 Object next = DelegatingEList.this.get(cursor); 941 checkModCount(); 942 lastCursor = cursor++; 943 return next; 944 } 945 catch (IndexOutOfBoundsException exception) 946 { 947 checkModCount(); 948 throw new NoSuchElementException (); 949 } 950 } 951 952 961 public void remove() 962 { 963 if (lastCursor == -1) 964 { 965 throw new IllegalStateException (); 966 } 967 checkModCount(); 968 969 try 970 { 971 DelegatingEList.this.remove(lastCursor); 972 expectedModCount = modCount; 973 if (lastCursor < cursor) 974 { 975 --cursor; 976 } 977 lastCursor = -1; 978 } 979 catch (IndexOutOfBoundsException exception) 980 { 981 throw new ConcurrentModificationException (); 982 } 983 } 984 985 989 protected void checkModCount() 990 { 991 if (modCount != expectedModCount) 992 { 993 throw new ConcurrentModificationException (); 994 } 995 } 996 } 997 998 1003 protected Iterator basicIterator() 1004 { 1005 return new NonResolvingEIterator(); 1006 } 1007 1008 1011 protected class NonResolvingEIterator extends EIterator 1012 { 1013 1019 public Object next() 1020 { 1021 try 1022 { 1023 Object next = delegateGet(cursor); 1024 checkModCount(); 1025 lastCursor = cursor++; 1026 return next; 1027 } 1028 catch (IndexOutOfBoundsException exception) 1029 { 1030 checkModCount(); 1031 throw new NoSuchElementException (); 1032 } 1033 } 1034 1035 1039 public void remove() 1040 { 1041 throw new UnsupportedOperationException (); 1042 } 1043 } 1044 1045 1051 public ListIterator listIterator() 1052 { 1053 return new EListIterator(); 1054 } 1055 1056 1060 protected ListIterator delegateListIterator() 1061 { 1062 return delegateList().listIterator(); 1063 } 1064 1065 1073 public ListIterator listIterator(int index) 1074 { 1075 int size = size(); 1076 if (index < 0 || index > size()) 1077 throw new IndexOutOfBoundsException ("index=" + index + ", size=" + size); 1078 1079 return new EListIterator(index); 1080 } 1081 1082 1085 protected class EListIterator extends EIterator implements ListIterator 1086 { 1087 1090 public EListIterator() 1091 { 1092 } 1093 1094 1098 public EListIterator(int index) 1099 { 1100 cursor = index; 1101 } 1102 1103 1107 public boolean hasPrevious() 1108 { 1109 return cursor != 0; 1110 } 1111 1112 1118 public Object previous() 1119 { 1120 try 1121 { 1122 Object previous = DelegatingEList.this.get(--cursor); 1123 checkModCount(); 1124 lastCursor = cursor; 1125 return previous; 1126 } 1127 catch (IndexOutOfBoundsException exception) 1128 { 1129 checkModCount(); 1130 throw new NoSuchElementException (); 1131 } 1132 } 1133 1134 1138 public int nextIndex() 1139 { 1140 return cursor; 1141 } 1142 1143 1147 public int previousIndex() 1148 { 1149 return cursor - 1; 1150 } 1151 1152 1161 public void set(Object object) 1162 { 1163 if (lastCursor == -1) 1164 { 1165 throw new IllegalStateException (); 1166 } 1167 checkModCount(); 1168 1169 try 1170 { 1171 DelegatingEList.this.set(lastCursor, object); 1172 } 1173 catch (IndexOutOfBoundsException exception) 1174 { 1175 throw new ConcurrentModificationException (); 1176 } 1177 } 1178 1179 1184 public void add(Object object) 1185 { 1186 checkModCount(); 1187 1188 try 1189 { 1190 DelegatingEList.this.add(cursor++, object); 1191 expectedModCount = modCount; 1192 lastCursor = -1; 1193 } 1194 catch (IndexOutOfBoundsException exception) 1195 { 1196 throw new ConcurrentModificationException (); 1197 } 1198 } 1199 } 1200 1201 1206 protected ListIterator basicListIterator() 1207 { 1208 return new NonResolvingEListIterator(); 1209 } 1210 1211 1218 protected ListIterator basicListIterator(int index) 1219 { 1220 int size = size(); 1221 if (index < 0 || index > size()) 1222 throw new IndexOutOfBoundsException ("index=" + index + ", size=" + size); 1223 1224 return new NonResolvingEListIterator(index); 1225 } 1226 1227 1230 protected class NonResolvingEListIterator extends EListIterator 1231 { 1232 1235 public NonResolvingEListIterator() 1236 { 1237 } 1238 1239 1243 public NonResolvingEListIterator(int index) 1244 { 1245 super(index); 1246 } 1247 1248 1254 public Object next() 1255 { 1256 try 1257 { 1258 Object next = delegateGet(cursor); 1259 checkModCount(); 1260 lastCursor = cursor++; 1261 return next; 1262 } 1263 catch (IndexOutOfBoundsException exception) 1264 { 1265 checkModCount(); 1266 throw new NoSuchElementException (); 1267 } 1268 } 1269 1270 1276 public Object previous() 1277 { 1278 try 1279 { 1280 Object previous = delegateGet(--cursor); 1281 checkModCount(); 1282 lastCursor = cursor; 1283 return previous; 1284 } 1285 catch (IndexOutOfBoundsException exception) 1286 { 1287 checkModCount(); 1288 throw new NoSuchElementException (); 1289 } 1290 } 1291 1292 1296 public void remove() 1297 { 1298 throw new UnsupportedOperationException (); 1299 } 1300 1301 1305 public void set(Object object) 1306 { 1307 throw new UnsupportedOperationException (); 1308 } 1309 1310 1314 public void add(Object object) 1315 { 1316 throw new UnsupportedOperationException (); 1317 } 1318 } 1319 1320 1323 public static class UnmodifiableEList extends DelegatingEList 1324 { 1325 protected List underlyingList; 1326 1327 1331 public UnmodifiableEList(List underlyingList) 1332 { 1333 this.underlyingList = underlyingList; 1334 } 1335 1336 protected List delegateList() 1337 { 1338 return underlyingList; 1339 } 1340 1341 1345 public Object set(int index, Object object) 1346 { 1347 throw new UnsupportedOperationException (); 1348 } 1349 1350 1354 public boolean add(Object object) 1355 { 1356 throw new UnsupportedOperationException (); 1357 } 1358 1359 1363 public void add(int index, Object object) 1364 { 1365 throw new UnsupportedOperationException (); 1366 } 1367 1368 1372 public boolean addAll(Collection collection) 1373 { 1374 throw new UnsupportedOperationException (); 1375 } 1376 1377 1381 public boolean addAll(int index, Collection collection) 1382 { 1383 throw new UnsupportedOperationException (); 1384 } 1385 1386 1390 public boolean remove(Object object) 1391 { 1392 throw new UnsupportedOperationException (); 1393 } 1394 1395 1399 public Object remove(int index) 1400 { 1401 throw new UnsupportedOperationException (); 1402 } 1403 1404 1408 public boolean removeAll(Collection collection) 1409 { 1410 throw new UnsupportedOperationException (); 1411 } 1412 1413 1417 public boolean retainAll(Collection collection) 1418 { 1419 throw new UnsupportedOperationException (); 1420 } 1421 1422 1426 public void clear() 1427 { 1428 throw new UnsupportedOperationException (); 1429 } 1430 1431 1435 public void move(int index, Object object) 1436 { 1437 throw new UnsupportedOperationException (); 1438 } 1439 1440 1444 public Object move(int targetIndex, int sourceIndex) 1445 { 1446 throw new UnsupportedOperationException (); 1447 } 1448 1449 1453 public Iterator iterator() 1454 { 1455 return basicIterator(); 1456 } 1457 1458 1462 public ListIterator listIterator() 1463 { 1464 return basicListIterator(); 1465 } 1466 1467 1472 public ListIterator listIterator(int index) 1473 { 1474 return basicListIterator(index); 1475 } 1476 } 1477 1478 1482 protected List basicList() 1483 { 1484 return delegateBasicList(); 1485 } 1486 1487 1491 protected List delegateBasicList() 1492 { 1493 return delegateList(); 1494 } 1495 1496 1501 protected Collection getDuplicates(Collection collection) 1502 { 1503 Collection result = collection; 1504 Collection filteredResult = null; 1505 for (Iterator i = collection.iterator(); i.hasNext(); ) 1506 { 1507 Object object = i.next(); 1508 if (!contains(object)) 1509 { 1510 if (filteredResult == null) 1511 { 1512 result = filteredResult = new BasicEList(collection); 1513 } 1514 filteredResult.remove(object); 1515 } 1516 } 1517 return result; 1518 } 1519 1520 1525 protected Collection getNonDuplicates(Collection collection) 1526 { 1527 Collection result = new UniqueEList(collection.size()); 1528 for (Iterator i = collection.iterator(); i.hasNext(); ) 1529 { 1530 Object object = i.next(); 1531 if (!contains(object)) 1532 { 1533 result.add(object); 1534 } 1535 } 1536 return result; 1537 } 1538} 1539 | Popular Tags |