1 9 package javolution.util; 10 11 import java.io.IOException ; 12 13 import j2me.io.ObjectInputStream; 14 import j2me.io.ObjectOutputStream; 15 import j2me.io.Serializable; 16 import j2me.lang.IllegalStateException; 17 import j2me.util.Collection; 18 import j2me.util.Iterator; 19 import j2me.util.List; 20 import j2me.util.ListIterator; 21 import j2mex.realtime.MemoryArea; 22 import java.util.NoSuchElementException ; 23 import javolution.context.PersistentContext; 24 import javolution.context.RealtimeObject; 25 import javolution.lang.Reusable; 26 27 54 public class FastListextends FastCollectionimplements Reusable, 55 List { 56 57 60 private static final Factory FACTORY = new Factory() { 61 62 public Object create() { 63 return new FastList(); 64 } 65 66 public void cleanup(Object obj) { 67 ((FastList) obj).reset(); 68 } 69 }; 70 71 74 private transient Node_head = new Node(); 75 76 79 private transient Node_tail = new Node(); 80 81 84 private transient FastComparator _valueComparator = FastComparator.DEFAULT; 85 86 89 private transient int _size; 90 91 94 public FastList() { 95 this(4); 96 } 97 98 106 public FastList(String id) { 107 this(); 108 new PersistentContext.Reference(id, this) { 109 protected void notifyChange() { 110 FastList.this.clear(); 111 FastList.this.addAll((FastList) this.get()); 112 } 113 }; 114 } 115 116 123 public FastList(int capacity) { 124 _head._next = _tail; 125 _tail._previous = _head; 126 Nodeprevious = _tail; 127 for (int i = 0; i++ < capacity;) { 128 NodenewNode = new Node(); 129 newNode._previous = previous; 130 previous._next = newNode; 131 previous = newNode; 132 } 133 } 134 135 141 public FastList(Collectionvalues) { 142 this(values.size()); 143 addAll(values); 144 } 145 146 153 public staticFastListnewInstance() { 154 return (FastList) FACTORY.object(); 155 } 156 157 162 public static void recycle(FastList instance) { 163 FACTORY.recycle(instance); 164 } 165 166 174 public final boolean add(Object value) { 175 addLast(value); 176 return true; 177 } 178 179 191 public int hashCode() { 192 final FastComparator comp = this.getValueComparator(); 193 int h = 1; 194 for (Node n = _head, end = _tail; (n = n._next) != end;) { 195 h = 31 * h + comp.hashCodeOf(n._value); 196 } 197 return h; 198 } 199 200 208 public final Object get(int index) { 209 if ((index < 0) || (index >= _size)) 210 throw new IndexOutOfBoundsException ("index: " + index); 211 return nodeAt(index)._value; 212 } 213 214 224 public final Object set(int index, Object value) { 225 if ((index < 0) || (index >= _size)) 226 throw new IndexOutOfBoundsException ("index: " + index); 227 final Nodenode = nodeAt(index); 228 Object previousValue = node._value; 229 node._value = value; 230 return previousValue; 231 } 232 233 244 public final void add(int index, Object value) { 245 if ((index < 0) || (index > _size)) 246 throw new IndexOutOfBoundsException ("index: " + index); 247 addBefore(nodeAt(index), value); 248 } 249 250 264 public final boolean addAll(int index, Collectionvalues) { 265 if ((index < 0) || (index > _size)) 266 throw new IndexOutOfBoundsException ("index: " + index); 267 final Node indexNode = nodeAt(index); 268 if (values instanceof FastList) { 269 FastListlist = (FastList) values; 270 for (Noden = list._head, end = list._tail; (n = n._next) != end;) { 271 addBefore(indexNode, n._value); 272 } 273 } else { 274 Iteratori = values.iterator(); 275 while (i.hasNext()) { 276 addBefore(indexNode, i.next()); 277 } 278 } 279 return values.size() != 0; 280 } 281 282 293 public final Object remove(int index) { 294 if ((index < 0) || (index >= _size)) 295 throw new IndexOutOfBoundsException ("index: " + index); 296 final Nodenode = nodeAt(index); 297 Object previousValue = node._value; 298 delete(node); 299 return previousValue; 300 } 301 302 310 public final int indexOf(Object value) { 311 final FastComparator comp = this.getValueComparator(); 312 int index = 0; 313 for (Node n = _head, end = _tail; (n = n._next) != end; index++) { 314 if (comp.areEqual(value, n._value)) 315 return index; 316 } 317 return -1; 318 } 319 320 328 public final int lastIndexOf(Object value) { 329 final FastComparator comp = this.getValueComparator(); 330 int index = size() - 1; 331 for (Node n = _tail, end = _head; (n = n._previous) != end; index--) { 332 if (comp.areEqual(value, n._value)) { 333 return index; 334 } 335 } 336 return -1; 337 } 338 339 346 public Iteratoriterator() { 347 FastListIterator i = (FastListIterator) FastListIterator.FACTORY 348 .object(); 349 i._list = this; 350 i._length = this._size; 351 i._nextNode = _head._next; 352 i._nextIndex = 0; 353 return i; 354 } 355 356 363 public ListIteratorlistIterator() { 364 FastListIterator i = (FastListIterator) FastListIterator.FACTORY 365 .object(); 366 i._list = this; 367 i._length = this._size; 368 i._nextNode = _head._next; 369 i._nextIndex = 0; 370 return i; 371 } 372 373 390 public ListIteratorlistIterator(int index) { 391 if ((index >= 0) && (index <= _size)) { 392 FastListIterator i = (FastListIterator) FastListIterator.FACTORY 393 .object(); 394 i._list = this; 395 i._length = this._size; 396 i._nextNode = nodeAt(index); 397 i._nextIndex = index; 398 return i; 399 } else { 400 throw new IndexOutOfBoundsException ("index: " + index 401 + " for list of size: " + _size); 402 } 403 } 404 405 436 public final ListsubList(int fromIndex, int toIndex) { 437 if ((fromIndex < 0) || (toIndex > _size) || (fromIndex > toIndex)) 438 throw new IndexOutOfBoundsException ("fromIndex: " + fromIndex 439 + ", toIndex: " + toIndex + " for list of size: " + _size); 440 SubList subList = (SubList) SubList.FACTORY.object(); 441 subList._list = this; 442 subList._head = nodeAt(fromIndex)._previous; 443 subList._tail = nodeAt(toIndex); 444 subList._size = toIndex - fromIndex; 445 return subList; 446 } 447 448 454 public final Object getFirst() { 455 final Nodenode = _head._next; 456 if (node == _tail) 457 throw new NoSuchElementException (); 458 return node._value; 459 } 460 461 467 public final Object getLast() { 468 final Nodenode = _tail._previous; 469 if (node == _head) 470 throw new NoSuchElementException (); 471 return node._value; 472 } 473 474 479 public final void addFirst(Object value) { 480 addBefore(_head._next, value); 481 } 482 483 488 public void addLast(Object value) { if (_tail._next == null) { 490 increaseCapacity(); 491 } 492 _tail._value = value; 493 _tail = _tail._next; 494 _size++; 495 } 496 497 503 public final Object removeFirst() { 504 final Nodefirst = _head._next; 505 if (first == _tail) 506 throw new NoSuchElementException (); 507 Object previousValue = first._value; 508 delete(first); 509 return previousValue; 510 } 511 512 518 public final Object removeLast() { 519 if (_size == 0) 520 throw new NoSuchElementException (); 521 _size--; 522 final Nodelast = _tail._previous; 523 final Object previousValue = last._value; 524 _tail = last; 525 last._value = null; 526 return previousValue; 527 } 528 529 533 539 public final void addBefore(Nodenext, Object value) { 540 if (_tail._next == null) { 541 increaseCapacity(); } 543 _size++; 544 final Node newNode = _tail._next; 545 final Node tailNext = _tail._next = newNode._next; 547 if (tailNext != null) { 548 tailNext._previous = _tail; 549 } 550 final Node previous = next._previous; 552 previous._next = newNode; 553 next._previous = newNode; 554 newNode._next = next; 555 newNode._previous = previous; 556 557 newNode._value = value; 558 } 559 560 567 private final NodenodeAt(int index) { 568 final int size = _size; 569 if (index <= (size >> 1)) { Nodenode = _head; 571 for (int i = index; i-- >= 0;) { 572 node = node._next; 573 } 574 return node; 575 } else { Nodenode = _tail; 577 for (int i = size - index; i-- > 0;) { 578 node = node._previous; 579 } 580 return node; 581 } 582 } 583 584 public final Record head() { 586 return _head; 587 } 588 589 public final Record tail() { 591 return _tail; 592 } 593 594 public final Object valueOf(Record record) { 596 return ((Node) record)._value; 597 } 598 599 public final void delete(Record record) { 601 Nodenode = (Node) record; 602 _size--; 603 node._value = null; 604 node._previous._next = node._next; 606 node._next._previous = node._previous; 607 final Nodenext = _tail._next; 609 node._previous = _tail; 610 node._next = next; 611 _tail._next = node; 612 if (next != null) { 613 next._previous = node; 614 } 615 } 616 617 621 public final int size() { 623 return _size; 624 } 625 626 public final void clear() { 628 for (Noden = _head, end = _tail; (n = n._next) != end;) { 629 n._value = null; 630 } 631 _tail = _head._next; 632 _size = 0; 633 } 634 635 641 public FastList setValueComparator(FastComparator comparator) { 642 _valueComparator = comparator; 643 return this; 644 } 645 646 public FastComparator getValueComparator() { 648 return _valueComparator; 649 } 650 651 public Collectionunmodifiable() { 653 return (Collection) super.unmodifiable(); 654 } 655 656 private void readObject(ObjectInputStream stream) throws IOException , 658 ClassNotFoundException { 659 660 _head = new Node(); 662 _tail = new Node(); 663 _head._next = _tail; 664 _tail._previous = _head; 665 666 setValueComparator((FastComparator) stream.readObject()); 667 final int size = stream.readInt(); 668 for (int i = size; i-- != 0;) { 669 addLast((Object ) stream.readObject()); 670 } 671 } 672 673 private void writeObject(ObjectOutputStream stream) throws IOException { 675 stream.writeObject(getValueComparator()); 676 stream.writeInt(_size); 677 Node node = _head; 678 for (int i = _size; i-- != 0;) { 679 node = node._next; 680 stream.writeObject(node._value); 681 } 682 } 683 684 private void increaseCapacity() { 686 MemoryArea.getMemoryArea(this).executeInArea(new Runnable () { 687 public void run() { 688 Node newNode0 = new Node(); 689 _tail._next = newNode0; 690 newNode0._previous = _tail; 691 692 Node newNode1 = new Node(); 693 newNode0._next = newNode1; 694 newNode1._previous = newNode0; 695 696 Node newNode2 = new Node(); 697 newNode1._next = newNode2; 698 newNode2._previous = newNode1; 699 700 Node newNode3 = new Node(); 701 newNode2._next = newNode3; 702 newNode3._previous = newNode2; 703 } 704 }); 705 } 706 707 711 public static final class Nodeimplements Record, 712 Serializable { 713 714 717 private Node_next; 718 719 722 private Node_previous; 723 724 727 private Object _value; 728 729 732 private Node() { 733 } 734 735 740 public final Object getValue() { 741 return _value; 742 } 743 744 public final RecordgetNext() { 746 return _next; 747 } 748 749 public final RecordgetPrevious() { 751 return _previous; 752 } 753 754 } 755 756 759 private static final class FastListIterator extends RealtimeObject 760 implements ListIterator { 761 762 private static final Factory FACTORY = new Factory() { 763 protected Object create() { 764 return new FastListIterator(); 765 } 766 767 protected void cleanup(Object obj) { 768 FastListIterator i = (FastListIterator) obj; 769 i._list = null; 770 i._currentNode = null; 771 i._nextNode = null; 772 } 773 }; 774 775 private FastList _list; 776 777 private Node _nextNode; 778 779 private Node _currentNode; 780 781 private int _length; 782 783 private int _nextIndex; 784 785 public boolean hasNext() { 786 return (_nextIndex != _length); 787 } 788 789 public Object next() { 790 if (_nextIndex == _length) 791 throw new NoSuchElementException (); 792 _nextIndex++; 793 _currentNode = _nextNode; 794 _nextNode = _nextNode._next; 795 return _currentNode._value; 796 } 797 798 public int nextIndex() { 799 return _nextIndex; 800 } 801 802 public boolean hasPrevious() { 803 return _nextIndex != 0; 804 } 805 806 public Object previous() { 807 if (_nextIndex == 0) 808 throw new NoSuchElementException (); 809 _nextIndex--; 810 _currentNode = _nextNode = _nextNode._previous; 811 return _currentNode._value; 812 } 813 814 public int previousIndex() { 815 return _nextIndex - 1; 816 } 817 818 public void add(Object o) { 819 _list.addBefore(_nextNode, o); 820 _currentNode = null; 821 _length++; 822 _nextIndex++; 823 } 824 825 public void set(Object o) { 826 if (_currentNode != null) { 827 _currentNode._value = o; 828 } else { 829 throw new IllegalStateException (); 830 } 831 } 832 833 public void remove() { 834 if (_currentNode != null) { 835 if (_nextNode == _currentNode) { _nextNode = _nextNode._next; 837 } else { 838 _nextIndex--; 839 } 840 _list.delete(_currentNode); 841 _currentNode = null; 842 _length--; 843 } else { 844 throw new IllegalStateException (); 845 } 846 } 847 } 848 849 852 private static final class SubList extends FastCollection 853 implements List, Serializable { 854 855 private static final Factory FACTORY = new Factory() { 856 protected Object create() { 857 return new SubList(); 858 } 859 860 protected void cleanup(Object obj) { 861 SubList sl = (SubList) obj; 862 sl._list = null; 863 sl._head = null; 864 sl._tail = null; 865 } 866 }; 867 868 private FastList _list; 869 870 private Node _head; 871 872 private Node _tail; 873 874 private int _size; 875 876 public int size() { 877 return _size; 878 } 879 880 public Record head() { 881 return _head; 882 } 883 884 public Record tail() { 885 return _tail; 886 } 887 888 public Object valueOf(Record record) { 889 return _list.valueOf(record); 890 } 891 892 public void delete(Record record) { 893 _list.delete(record); 894 } 895 896 public boolean addAll(int index, Collection values) { 897 if ((index < 0) || (index > _size)) 898 throw new IndexOutOfBoundsException ("index: " + index); 899 final Node indexNode = nodeAt(index); 900 Iterator i = values.iterator(); 901 while (i.hasNext()) { 902 _list.addBefore(indexNode, i.next()); 903 } 904 return values.size() != 0; 905 } 906 907 public Object get(int index) { 908 if ((index < 0) || (index >= _size)) 909 throw new IndexOutOfBoundsException ("index: " + index); 910 return nodeAt(index)._value; 911 } 912 913 public Object set(int index, Object value) { 914 if ((index < 0) || (index >= _size)) 915 throw new IndexOutOfBoundsException ("index: " + index); 916 final Node node = nodeAt(index); 917 Object previousValue = node._value; 918 node._value = value; 919 return previousValue; 920 } 921 922 public void add(int index, Object element) { 923 if ((index < 0) || (index > _size)) 924 throw new IndexOutOfBoundsException ("index: " + index); 925 _list.addBefore(nodeAt(index), element); 926 } 927 928 public Object remove(int index) { 929 if ((index < 0) || (index >= _size)) 930 throw new IndexOutOfBoundsException ("index: " + index); 931 final Node node = nodeAt(index); 932 Object previousValue = node._value; 933 _list.delete(node); 934 return previousValue; 935 } 936 937 public int indexOf(Object value) { 938 final FastComparator comp = _list.getValueComparator(); 939 int index = 0; 940 for (Node n = _head, end = _tail; (n = n._next) != end; index++) { 941 if (comp.areEqual(value, n._value)) 942 return index; 943 } 944 return -1; 945 } 946 947 public int lastIndexOf(Object value) { 948 final FastComparator comp = this.getValueComparator(); 949 int index = size() - 1; 950 for (Node n = _tail, end = _head; (n = n._previous) != end; index--) { 951 if (comp.areEqual(value, n._value)) { 952 return index; 953 } 954 } 955 return -1; 956 } 957 958 public ListIterator listIterator() { 959 return listIterator(0); 960 } 961 962 public ListIterator listIterator(int index) { 963 if ((index >= 0) && (index <= _size)) { 964 FastListIterator i = (FastListIterator) FastListIterator.FACTORY 965 .object(); 966 i._list = _list; 967 i._length = _size; 968 i._nextNode = nodeAt(index); 969 i._nextIndex = index; 970 return i; 971 } else { 972 throw new IndexOutOfBoundsException ("index: " + index 973 + " for list of size: " + _size); 974 } 975 } 976 977 public List subList(int fromIndex, int toIndex) { 978 if ((fromIndex < 0) || (toIndex > _size) || (fromIndex > toIndex)) 979 throw new IndexOutOfBoundsException ("fromIndex: " + fromIndex 980 + ", toIndex: " + toIndex + " for list of size: " 981 + _size); 982 SubList subList = (SubList) SubList.FACTORY.object(); 983 subList._list = _list; 984 subList._head = nodeAt(fromIndex)._previous; 985 subList._tail = nodeAt(toIndex); 986 subList._size = toIndex - fromIndex; 987 return subList; 988 } 989 990 private final Node nodeAt(int index) { 991 if (index <= (_size >> 1)) { Node node = _head; 993 for (int i = index; i-- >= 0;) { 994 node = node._next; 995 } 996 return node; 997 } else { Node node = _tail; 999 for (int i = _size - index; i-- > 0;) { 1000 node = node._previous; 1001 } 1002 return node; 1003 } 1004 } 1005 } 1006 1007 private static final long serialVersionUID = 1L; 1008} | Popular Tags |