1 17 package org.eclipse.emf.common.util; 18 19 20 import java.util.Arrays ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.Comparator ; 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 class ECollections 34 { 35 private ECollections() 37 { 38 } 39 40 43 public static void reverse(EList list) 44 { 45 int last = list.size() - 1; 46 for (int i = 0; i < last; i++) 47 { 48 list.move(i, last); 49 } 50 } 51 52 64 public static int indexOf(List list, Object o, int fromIndex) 65 { 66 if (fromIndex < 0) 67 { 68 fromIndex = 0; 69 } 70 71 int size = list.size(); 72 for (int i = fromIndex; i < size; i++) 73 { 74 Object element = list.get(i); 75 if (o == null) 76 { 77 if (element == null) 78 { 79 return i; 80 } 81 } 82 else if (o == element || o.equals(element)) 83 { 84 return i; 85 } 86 } 87 return -1; 88 } 89 90 96 public static void sort(EList list) 97 { 98 Object [] listAsArray = list.toArray(); 99 Arrays.sort(listAsArray); 100 for (int i=0; i < listAsArray.length; i++) 101 { 102 int oldIndex = indexOf(list, listAsArray[i], i); 103 if (i != oldIndex) 104 { 105 list.move(i, oldIndex); 106 } 107 } 108 } 109 110 117 public static void sort(EList list, Comparator comparator) 118 { 119 Object [] listAsArray = list.toArray(); 120 Arrays.sort(listAsArray, comparator); 121 for (int i=0; i < listAsArray.length; i++) 122 { 123 int oldIndex = indexOf(list, listAsArray[i], i); 124 if (i != oldIndex) 125 { 126 list.move(i, oldIndex); 127 } 128 } 129 } 130 131 140 public static void setEList(EList eList, List prototypeList) 141 { 142 int index = 0; 143 for (Iterator objects = prototypeList.iterator(); objects.hasNext(); ++index) 144 { 145 Object prototypeObject = objects.next(); 146 if (eList.size() <= index) 147 { 148 eList.add(prototypeObject); 149 } 150 else 151 { 152 boolean done; 153 do 154 { 155 done = true; 156 Object targetObject = eList.get(index); 157 if (targetObject == null ? prototypeObject != null : !targetObject.equals(prototypeObject)) 158 { 159 int position = indexOf(eList, prototypeObject, index); 160 if (position != -1) 161 { 162 int targetIndex = indexOf(prototypeList, targetObject, index); 163 if (targetIndex == -1) 164 { 165 eList.remove(index); 166 done = false; 167 } 168 else if (targetIndex > position) 169 { 170 if (eList.size() <= targetIndex) 171 { 172 targetIndex = eList.size() - 1; 173 } 174 eList.move(targetIndex, index); 175 176 done = false; 177 } 178 else 179 { 180 eList.move(index, position); 181 } 182 } 183 else 184 { 185 eList.add(index, prototypeObject); 186 } 187 } 188 } 189 while (!done); 190 } 191 } 192 for (int i = eList.size(); i > index;) 193 { 194 eList.remove(--i); 195 } 196 } 197 198 202 public static EList unmodifiableEList(EList list) 203 { 204 return new UnmodifiableEList(list); 205 } 206 207 210 public static final EList EMPTY_ELIST = new EmptyUnmodifiableEList(); 211 212 private static class UnmodifiableEList implements EList 213 { 214 protected EList list; 215 216 public UnmodifiableEList(EList list) 217 { 218 this.list = list; 219 } 220 221 public int size() 222 { 223 return list.size(); 224 } 225 226 public boolean isEmpty() 227 { 228 return list.isEmpty(); 229 } 230 231 public boolean contains(Object o) 232 { 233 return list.contains(o); 234 } 235 236 public Object [] toArray() 237 { 238 return list.toArray(); 239 } 240 241 public Object [] toArray(Object [] a) 242 { 243 return list.toArray(a); 244 } 245 246 public String toString() 247 { 248 return list.toString(); 249 } 250 251 public Iterator iterator() 252 { 253 return 254 new Iterator () 255 { 256 Iterator i = list.iterator(); 257 258 public boolean hasNext() 259 { 260 return i.hasNext(); 261 } 262 public Object next() 263 { 264 return i.next(); 265 } 266 public void remove() 267 { 268 throw new UnsupportedOperationException (); 269 } 270 }; 271 } 272 273 public boolean add(Object o) 274 { 275 throw new UnsupportedOperationException (); 276 } 277 278 public boolean remove(Object o) 279 { 280 throw new UnsupportedOperationException (); 281 } 282 283 public boolean containsAll(Collection coll) 284 { 285 return list.containsAll(coll); 286 } 287 288 public boolean addAll(Collection coll) 289 { 290 throw new UnsupportedOperationException (); 291 } 292 293 public boolean removeAll(Collection coll) 294 { 295 throw new UnsupportedOperationException (); 296 } 297 298 public boolean retainAll(Collection coll) 299 { 300 throw new UnsupportedOperationException (); 301 } 302 303 public void clear() 304 { 305 throw new UnsupportedOperationException (); 306 } 307 308 public boolean equals(Object o) 309 { 310 return list.equals(o); 311 } 312 313 public int hashCode() 314 { 315 return list.hashCode(); 316 } 317 318 public Object get(int index) 319 { 320 return list.get(index); 321 } 322 323 public Object set(int index, Object element) 324 { 325 throw new UnsupportedOperationException (); 326 } 327 328 public void add(int index, Object element) 329 { 330 throw new UnsupportedOperationException (); 331 } 332 333 public Object remove(int index) 334 { 335 throw new UnsupportedOperationException (); 336 } 337 338 public int indexOf(Object o) 339 { 340 return list.indexOf(o); 341 } 342 343 public int lastIndexOf(Object o) 344 { 345 return list.lastIndexOf(o); 346 } 347 348 public boolean addAll(int index, Collection collection) 349 { 350 throw new UnsupportedOperationException (); 351 } 352 353 public ListIterator listIterator() 354 { 355 return listIterator(0); 356 } 357 358 public ListIterator listIterator(final int index) 359 { 360 return 361 new ListIterator () 362 { 363 ListIterator i = list.listIterator(index); 364 365 public boolean hasNext() 366 { 367 return i.hasNext(); 368 } 369 370 public Object next() 371 { 372 return i.next(); 373 } 374 375 public boolean hasPrevious() 376 { 377 return i.hasPrevious(); 378 } 379 380 public Object previous() 381 { 382 return i.previous(); 383 } 384 385 public int nextIndex() 386 { 387 return i.nextIndex(); 388 } 389 390 public int previousIndex() 391 { 392 return i.previousIndex(); 393 } 394 395 public void remove() 396 { 397 throw new UnsupportedOperationException (); 398 } 399 400 public void set(Object o) 401 { 402 throw new UnsupportedOperationException (); 403 } 404 405 public void add(Object o) 406 { 407 throw new UnsupportedOperationException (); 408 } 409 }; 410 } 411 412 public List subList(int fromIndex, int toIndex) 413 { 414 return new UnmodifiableEList(new BasicEList(list.subList(fromIndex, toIndex))); 415 } 416 417 public void move(int newPosition, Object o) 418 { 419 throw new UnsupportedOperationException (); 420 } 421 422 public Object move(int newPosition, int oldPosition) 423 { 424 throw new UnsupportedOperationException (); 425 } 426 } 427 428 private static class EmptyUnmodifiableEList implements EList 429 { 430 public EmptyUnmodifiableEList() 431 { 432 } 433 434 public int size() 435 { 436 return 0; 437 } 438 439 public boolean isEmpty() 440 { 441 return true; 442 } 443 444 public boolean equals(Object o) 445 { 446 return Collections.EMPTY_LIST.equals(o); 447 } 448 449 public int hashCode() 450 { 451 return Collections.EMPTY_LIST.hashCode(); 452 } 453 454 public Object get(int index) 455 { 456 return Collections.EMPTY_LIST.get(index); 457 } 458 459 public boolean contains(Object o) 460 { 461 return false; 462 } 463 464 public int indexOf(Object o) 465 { 466 return -1; 467 } 468 469 public int lastIndexOf(Object o) 470 { 471 return -1; 472 } 473 474 ListIterator listIterator = 475 new ListIterator () 476 { 477 public boolean hasNext() 478 { 479 return false; 480 } 481 public Object next() 482 { 483 throw new NoSuchElementException (); 484 } 485 public boolean hasPrevious() 486 { 487 return false; 488 } 489 public Object previous() 490 { 491 throw new NoSuchElementException (); 492 } 493 public int nextIndex() 494 { 495 return 0; 496 } 497 public int previousIndex() 498 { 499 return -1; 500 } 501 502 public void remove() 503 { 504 throw new UnsupportedOperationException (); 505 } 506 public void set(Object o) 507 { 508 throw new UnsupportedOperationException (); 509 } 510 public void add(Object o) 511 { 512 throw new UnsupportedOperationException (); 513 } 514 }; 515 516 public Iterator iterator() 517 { 518 return listIterator; 519 } 520 521 public ListIterator listIterator() 522 { 523 return listIterator; 524 } 525 526 public ListIterator listIterator(int index) 527 { 528 return listIterator; 529 } 530 531 public List subList(int fromIndex, int toIndex) 532 { 533 return Collections.EMPTY_LIST.subList(fromIndex, toIndex); 534 } 535 536 537 public Object [] toArray() 538 { 539 return Collections.EMPTY_LIST.toArray(); 540 } 541 542 public Object [] toArray(Object [] a) 543 { 544 return Collections.EMPTY_LIST.toArray(a); 545 } 546 547 public String toString() 548 { 549 return Collections.EMPTY_LIST.toString(); 550 } 551 552 public boolean add(Object o) 553 { 554 throw new UnsupportedOperationException (); 555 } 556 557 public boolean remove(Object o) 558 { 559 throw new UnsupportedOperationException (); 560 } 561 562 public boolean containsAll(Collection coll) 563 { 564 return false; 565 } 566 567 public boolean addAll(Collection coll) 568 { 569 throw new UnsupportedOperationException (); 570 } 571 572 public boolean removeAll(Collection coll) 573 { 574 throw new UnsupportedOperationException (); 575 } 576 577 public boolean retainAll(Collection coll) 578 { 579 throw new UnsupportedOperationException (); 580 } 581 582 public void clear() 583 { 584 throw new UnsupportedOperationException (); 585 } 586 587 public Object set(int index, Object element) 588 { 589 throw new UnsupportedOperationException (); 590 } 591 592 public void add(int index, Object element) 593 { 594 throw new UnsupportedOperationException (); 595 } 596 597 public Object remove(int index) 598 { 599 throw new UnsupportedOperationException (); 600 } 601 602 public boolean addAll(int index, Collection collection) 603 { 604 throw new UnsupportedOperationException (); 605 } 606 607 public void move(int newPosition, Object o) 608 { 609 throw new UnsupportedOperationException (); 610 } 611 612 public Object move(int newPosition, int oldPosition) 613 { 614 throw new UnsupportedOperationException (); 615 } 616 } 617 } 618 | Popular Tags |