1 23 24 29 42 43 48 49 package com.sun.enterprise.util.collection; 50 51 import java.util.*; 52 53 61 public class DList 62 implements List, DListNodeFactory 63 { 64 65 protected DListNode first; 66 protected DListNode last; 67 protected int size = 0; 68 protected DListNodeFactory nodeFactory; 69 70 73 public DList() { 74 first = new DListNode(null); 75 last = new DListNode(null); 76 first.next = last; 77 last.prev = first; 78 first.prev = last.next = null; 79 this.nodeFactory = this; 80 } 81 82 85 public DList(DListNode firstNode, DListNode lastNode, DListNodeFactory factory) { 86 initDListWithNodes(firstNode, lastNode, factory); 87 } 88 89 protected void initDListWithNodes(DListNode firstNode, DListNode lastNode, DListNodeFactory factory) { 90 first = firstNode; 91 last = lastNode; 92 first.next = last; 93 last.prev = first; 94 first.prev = last.next = null; 95 this.nodeFactory = (factory == null) ? this : factory; 96 } 97 98 99 102 public DList(DListNodeFactory nodeFactory) { 103 first = new DListNode(null); 104 last = new DListNode(null); 105 first.next = last; 106 last.prev = first; 107 first.prev = last.next = null; 108 this.nodeFactory = nodeFactory; 109 } 110 111 112 private DList(DListNode firstNode, DListNode lastNode, int size, DListNodeFactory nodeFactory) { 113 first = firstNode; 114 last = lastNode; 115 this.size = size; 116 this.nodeFactory = nodeFactory; 117 } 118 119 120 121 127 public void add(int index, Object object) { 128 insertAt(index, object); 129 size++; 130 } 131 132 136 public boolean add(Object object) { 137 last.insertBefore(nodeFactory.createDListNode(object)); 138 size++; 139 return true; 140 } 141 142 148 public boolean addAll(Collection collection) { 149 Iterator iter = collection.iterator(); 150 boolean added = false; 151 while (iter.hasNext()) { 152 add(iter.next()); 153 added = true; 154 } 155 156 size += collection.size(); 157 return added; 158 } 159 160 166 public boolean addAll(int index, Collection collection) { 167 DListNode node = getDListNodeAt(index); 168 Iterator iter = collection.iterator(); 169 boolean added = iter.hasNext(); 170 DListNode head = new DListNode(null); 171 DListNode last = head; 172 while (iter.hasNext()) { 173 last.insertAfter(nodeFactory.createDListNode(iter.next())); 174 last = last.next; 175 } 176 if (head != last) { 177 node.prev.next = head.next; 178 node.prev = last; 179 180 head.next.prev = node; 181 last.next = node; 182 } 183 184 size += collection.size(); 185 return added; 186 } 187 188 public void clear() { 189 first.next = last; 190 last.prev = first; 191 size = 0; 192 } 193 194 public boolean contains(Object o) { 195 return (indexOf(o) != -1); 196 } 197 198 public boolean containsAll(Collection collection) { 199 Iterator iter = collection.iterator(); 200 while (iter.hasNext()) { 201 Object o = iter.next(); 202 if (indexOf(o) == -1) { 203 return false; 204 } 205 } 206 return true; 207 } 208 209 public boolean equals(Object o) { 210 if (o instanceof List) { 211 List list = (List) o; 212 if (list.size() != size()) { 213 return false; 214 } 215 216 Object myObj = null, otherObj = null; 217 DListNode node = first; 218 for (int i=0; i<size; i++) { 219 myObj = node.next.object; 220 otherObj = list.get(i); 221 if (! myObj.equals(otherObj)) { 222 return false; 223 } 224 node = node.next; 225 } 226 return true; 227 } 228 return false; 229 } 230 231 public int hashCode() { 232 int hashCode = 1; 233 DListNode node = first; 234 Object myObj = null; 235 for (int i=0; i<size; i++) { 236 myObj = node.next.object; 237 hashCode = 31*hashCode + (myObj==null ? 0 : myObj.hashCode()); 238 node = node.next; 239 } 240 return hashCode; 241 } 242 243 247 public Object get(int index) { 248 DListNode node = getDListNodeAt(index); 249 return (node == null) ? null : node.object; 250 } 251 252 253 260 public int indexOf(Object o) { 261 int index = 0; 262 for (DListNode node = first.next; node != last; node = node.next) { 263 if (node.object.equals(o)) { 264 return index; 265 } 266 index++; 267 } 268 return -1; 269 } 270 271 275 public boolean isEmpty() { 276 return (this.size > 0); 277 } 278 279 284 public Iterator iterator() { 285 return new DListIterator(first, last, false, 0); 286 } 287 288 296 public int lastIndexOf(Object obj) { 297 int index = size - 1; 298 for (DListNode node = last.prev; node != first; node = node.prev) { 299 if (node.object.equals(obj)) { 300 return index; 301 } 302 index--; 303 } 304 return -1; 305 } 306 307 312 public ListIterator listIterator() { 313 return new DListIterator(first, last, true, 0); 314 } 315 316 325 public ListIterator listIterator(int index) { 326 return new DListIterator(first, last, true, index); 327 } 328 329 334 public Object remove(int index) { 335 DListNode node = getDListNodeAt(index); 336 node.delink(); 337 size--; 338 Object object = node.object; 339 destroyDListNode(node); 340 return object; 341 } 342 343 350 public boolean remove(Object object) { 351 DListNode node = getDListNode(object); 352 if (node == null) { 353 return false; 354 } else { 355 node.delink(); 356 destroyDListNode(node); 357 size--; 358 return true; 359 } 360 } 361 362 public boolean removeAll(Collection collection) { 363 Iterator iter = collection.iterator(); 364 boolean removed = false; 365 while (iter.hasNext()) { 366 if (remove(iter.next())) { 367 size--; 368 removed = true; 369 } 370 } 371 return removed; 372 } 373 374 public boolean retainAll(Collection collection) { 375 376 boolean removed = false; 377 DListNode node = first; 378 DListNode dnode = null; 379 while (node.next != last) { 380 dnode = node.next; 381 if (collection.contains(dnode.object)) { 382 dnode.delink(); 383 destroyDListNode(dnode); 384 size--; 385 removed = true; 386 } else { 387 node = node.next; 388 } 389 } 390 return removed; 391 } 392 393 397 public Object set(int index, Object object) { 398 DListNode node = getDListNodeAt(index); 399 Object oldObject = (node == null) ? null : node.object; 400 node.object = object; 401 return oldObject; 402 } 403 404 405 411 public DListNode insertAt(int index, Object object) { 412 if ((index < 0) || (index >= size)) { 413 return null; 414 } 415 int mid = size >> 1; DListNode node = null; 417 if (index <= mid) { 418 node = first.next; 419 for (int i=0; i<index ; i++) { 420 node = node.next; 421 } 422 } else { 423 index = size - index - 1; 424 node = last.prev; 425 for (int i=0; i<index ; i++) { 426 node = node.prev; 427 } 428 } 429 DListNode newNode = nodeFactory.createDListNode(object); 430 node.insertBefore(newNode); 431 size++; 432 return newNode; 433 } 434 435 439 public int size() { 440 return size; 441 } 442 443 444 465 public List subList(int fromIndex, int toIndex) { 466 System.out.println("subList(" + fromIndex + ", " + toIndex + ")"); 467 DListNode startNode = getDListNodeAt(fromIndex); 468 System.out.println("nodeAt(" + fromIndex + "): " + startNode.object); 469 DListNode toNode = getDListNodeAt(toIndex); 470 System.out.println("nodeAt(" + toIndex + "): " + toNode.object); 471 return new DList(startNode.prev, toNode, toIndex - fromIndex, nodeFactory); 472 } 473 474 475 476 485 public Object [] toArray() { 486 Object [] array = new Object [size]; 487 int index = 0; 488 for (DListNode node = first.next; node != last; node = node.next) { 489 array[index++] = node.object; 490 } 491 return array; 492 } 493 494 514 public Object [] toArray(Object [] array) { 515 516 if (array.length < size) { 517 array = (Object []) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), size); 518 } 519 520 int index = 0; 521 for (DListNode node = first.next; node != last; node = node.next) { 522 array[index++] = node.object; 523 } 524 525 if (array.length > size) { 526 array[size] = null; 527 } 528 529 return array; 530 531 } 532 533 534 535 536 537 538 539 540 public DListNode createDListNode(Object object) { 541 return new DListNode(object); 542 } 543 544 public void destroyDListNode(DListNode node) { 545 } 546 547 public DListNodeFactory getDListNodeFactory() { 548 return this.nodeFactory; 549 } 550 551 public void setDListNodeFactory(DListNodeFactory nodeFactory) { 552 this.nodeFactory = nodeFactory; 553 } 554 555 559 public void addAsFirstNode(DListNode node) { 560 DListNode fNode = first.next; 561 node.next = fNode; 562 node.prev = first; 563 fNode.prev = first.next = node; 564 size++; 565 } 566 567 574 public DListNode addAsFirstObject(Object object) { 575 DListNode node = nodeFactory.createDListNode(object); 576 addAsFirstNode(node); 577 return node; 578 } 579 580 584 public void addAsLastNode(DListNode node) { 585 DListNode lNode = last.prev; 586 node.next = last; 587 node.prev = lNode; 588 lNode.next = last.prev = node; 589 size++; 590 } 591 592 599 public DListNode addAsLastObject(Object obj) { 600 DListNode node= nodeFactory.createDListNode(obj); 601 addAsLastNode(node); 602 return node; 603 } 604 605 609 public DListNode delinkFirstNode() { 610 if (size > 0) { 611 DListNode node = first.next; 612 node.delink(); 613 size--; 614 return node; 615 } 616 return null; 617 } 618 619 623 public Object removeFirstObject() { 624 DListNode node = delinkFirstNode(); 625 if (node == null) { 626 return null; 627 } else { 628 Object object = node.object; 629 destroyDListNode(node); 630 return object; 631 } 632 } 633 634 635 639 public DListNode delinkLastNode() { 640 if (size > 0) { 641 DListNode node = last.prev; 642 node.delink(); 643 size--; 644 return node; 645 } 646 return null; 647 } 648 649 653 public Object removeLastObject() { 654 DListNode node = delinkLastNode(); 655 if (node == null) { 656 return null; 657 } else { 658 Object object = node.object; 659 destroyDListNode(node); 660 return object; 661 } 662 } 663 664 665 672 public DListNode getDListNode(Object o) { 673 for (DListNode node = first.next; node != last; node = node.next) { 674 if (node.object.equals(o)) { 675 return node; 676 } 677 } 678 return null; 679 } 680 681 public void delink(DListNode node) { 682 node.delink(); 683 size--; 684 } 685 686 692 public DListNode getDListNodeAt(int index) { 693 if ((index < 0) || (index >= size)) { 694 throw new ArrayIndexOutOfBoundsException ("DList size: " + size + "; index: " + index); 695 } 696 int mid = size >> 1; DListNode node = null; 698 if (index <= mid) { 699 node = first.next; 700 for (int i=0; i<index ; i++) { 701 node = node.next; 702 } 703 } else { 704 index = size - index - 1; 705 node = last.prev; 706 for (int i=0; i<index ; i++) { 707 node = node.prev; 708 } 709 } 710 return node; 711 } 712 713 public DListNode getFirstDListNode() { 714 return (size == 0) ? null : first.next; 715 } 716 717 public DListNode getLastDListNode() { 718 return (size == 0) ? null : last.prev; 719 } 720 721 public DListNode getNextNode(DListNode node) { 722 DListNode nextNode = node.next; 723 return (nextNode == last) ? null : nextNode; 724 } 725 726 public DListNode getPreviousNode(DListNode node) { 727 DListNode prevNode = node.prev; 728 return (prevNode == first) ? null : prevNode; 729 } 730 731 737 public Iterator nodeIterator() { 738 return new DListIterator( first, last, true, 0); 739 } 740 741 742 743 744 745 746 747 748 749 private class DListIterator 750 implements java.util.ListIterator 751 { 752 DListNode firstNode; 753 DListNode lastNode; 754 DListNode currentNode; 755 boolean toReturnNode; 756 int currentIndex = -1; 757 758 DListIterator(DListNode firstNode, DListNode lastNode, boolean toReturnNode, int skip) { 759 this.firstNode = this.currentNode = firstNode; 760 this.lastNode = lastNode; 761 this.toReturnNode = toReturnNode; 762 763 for (int i=0; i<skip; i++) { 764 currentNode = currentNode.next; 765 } 766 this.currentIndex = skip; 767 768 } 769 770 DListIterator(int startIndex, int endIndex, boolean toReturnNode, int skip) { 771 this.firstNode = this.currentNode = firstNode; 772 this.lastNode = getDListNodeAt(endIndex); 773 this.toReturnNode = toReturnNode; 774 775 for (int i=0; i<skip; i++) { 776 currentNode = currentNode.next; 777 } 778 this.currentIndex = skip; 779 780 } 781 782 public void add(Object obj) { 783 currentNode.insertAfter(nodeFactory.createDListNode(obj)); 784 } 785 786 public boolean hasNext() { 787 return (currentNode.next != lastNode); 788 } 789 790 public boolean hasPrevious() { 791 return (currentNode != firstNode); 792 } 793 794 public Object next() { 795 if (currentNode.next == lastNode) { 796 throw new java.util.NoSuchElementException ("No next after this element"); 797 } 798 currentNode = currentNode.next; 799 currentIndex++; 800 return (toReturnNode ? currentNode : currentNode.object); 801 } 802 803 public int nextIndex() { 804 return (currentIndex+1); 805 } 806 807 public Object previous() { 808 if (currentNode == firstNode) { 809 throw new java.util.NoSuchElementException ("No previous before this element"); 810 } 811 DListNode node = currentNode; 812 currentNode = currentNode.prev; 813 currentIndex--; 814 return (toReturnNode ? node : node.object); 815 } 816 817 public int previousIndex() { 818 return (currentIndex-1); 819 } 820 821 public void remove() { 822 throw new UnsupportedOperationException ("list.remove() not supported by DList iterator...."); 823 } 824 825 public void set(Object o) { 826 throw new UnsupportedOperationException ("list.remove() not supported by DList iterator...."); 827 } 828 } 829 830 831 } | Popular Tags |