ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/extra/ReadMostlyVector.java
(Generate patch)

Comparing jsr166/src/jsr166e/extra/ReadMostlyVector.java (file contents):
Revision 1.11 by dl, Wed Jul 20 20:29:33 2011 UTC vs.
Revision 1.22 by jsr166, Sat Dec 31 21:16:14 2011 UTC

# Line 19 | Line 19 | import java.util.*;
19   * best-effort in the presence of concurrent modifications, and do
20   * <em>NOT</em> throw {@link ConcurrentModificationException}.  An
21   * iterator's {@code next()} method returns consecutive elements as
22 < * they appear in the underlying array upon each access. Alternatvely,
22 > * they appear in the underlying array upon each access. Alternatively,
23   * method {@link #snapshotIterator} may be used for deterministic
24   * traversals, at the expense of making a copy, and unavailability of
25   * method {@code Iterator.remove}.
# Line 32 | Line 32 | import java.util.*;
32   *
33   * @author Doug Lea
34   */
35 < public class ReadMostlyVector<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
35 > public class ReadMostlyVector<E>
36 >        implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
37      private static final long serialVersionUID = 8673264195747942595L;
38  
39      /*
# Line 48 | Line 49 | public class ReadMostlyVector<E> impleme
49       * read-only mode, and then lock. When in read-only mode, they
50       * validate only at the end of an array scan unless the element is
51       * actually used (for example, as an argument of method equals).
52 +     *
53 +     * We rely on some invariants that are always true, even for field
54 +     * reads in read-only mode that have not yet been validated:
55 +     * - array != null
56 +     * - count >= 0
57       */
58  
59      /**
# Line 129 | Line 135 | public class ReadMostlyVector<E> impleme
135      }
136  
137      // For explanation, see CopyOnWriteArrayList
138 <    final void grow(int minCapacity) {
138 >    final Object[] grow(int minCapacity) {
139          int oldCapacity = array.length;
140          int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
141                                           capacityIncrement : oldCapacity);
# Line 137 | Line 143 | public class ReadMostlyVector<E> impleme
143              newCapacity = minCapacity;
144          if (newCapacity - MAX_ARRAY_SIZE > 0)
145              newCapacity = hugeCapacity(minCapacity);
146 <        array = Arrays.copyOf(array, newCapacity);
146 >        return array = Arrays.copyOf(array, newCapacity);
147      }
148  
149      static int hugeCapacity(int minCapacity) {
# Line 199 | Line 205 | public class ReadMostlyVector<E> impleme
205          return -1;
206      }
207  
208 <    final void rawAdd(Object e) {
208 >    final void rawAdd(E e) {
209          int n = count;
210          Object[] items = array;
211 <        if (n < items.length)
212 <            items[n] = e;
213 <        else {
208 <            grow(n + 1);
209 <            array[n] = e;
210 <        }
211 >        if (n >= items.length)
212 >            items = grow(n + 1);
213 >        items[n] = e;
214          count = n + 1;
215      }
216  
217 <    final void rawAddAt(int index, Object e) {
217 >    final void rawAddAt(int index, E e) {
218          int n = count;
219 +        Object[] items = array;
220          if (index > n)
221              throw new ArrayIndexOutOfBoundsException(index);
222 <        if (n >= array.length)
223 <            grow(n + 1);
220 <        Object[] items = array;
222 >        if (n >= items.length)
223 >            items = grow(n + 1);
224          if (index < n)
225              System.arraycopy(items, index, items, index + 1, n - index);
226          items[index] = e;
# Line 226 | Line 229 | public class ReadMostlyVector<E> impleme
229  
230      final boolean rawAddAllAt(int index, Object[] elements) {
231          int n = count;
232 +        Object[] items = array;
233          if (index < 0 || index > n)
234              throw new ArrayIndexOutOfBoundsException(index);
235          int len = elements.length;
236          if (len == 0)
237              return false;
238          int newCount = n + len;
239 <        if (newCount >= array.length)
240 <            grow(newCount);
237 <        Object[] items = array;
239 >        if (newCount >= items.length)
240 >            items = grow(newCount);
241          int mv = n - index;
242          if (mv > 0)
243              System.arraycopy(items, index, items, index + len, mv);
# Line 244 | Line 247 | public class ReadMostlyVector<E> impleme
247      }
248  
249      final boolean rawRemoveAt(int index) {
247        Object[] items = array;
250          int n = count - 1;
251 +        Object[] items = array;
252          if (index < 0 || index > n)
253              return false;
254          int mv = n - index;
# Line 264 | Line 267 | public class ReadMostlyVector<E> impleme
267       * field under lock.
268       */
269      final boolean internalRemoveAll(Collection<?> c, int origin, int bound) {
270 <        SequenceLock lock = this.lock;
270 >        final SequenceLock lock = this.lock;
271          boolean removed = false;
272          lock.lock();
273          try {
# Line 283 | Line 286 | public class ReadMostlyVector<E> impleme
286      }
287  
288      final boolean internalRetainAll(Collection<?> c, int origin, int bound) {
289 <        SequenceLock lock = this.lock;
289 >        final SequenceLock lock = this.lock;
290          boolean removed = false;
291          if (c != this) {
292              lock.lock();
# Line 297 | Line 300 | public class ReadMostlyVector<E> impleme
300                          ++i;
301                      else {
302                          --fence;
303 <                        int mv = --count - i;
303 >                        int mv = --n - i;
304                          if (mv > 0)
305                              System.arraycopy(items, i + 1, items, i, mv);
303                        removed = true;
306                      }
307                  }
308 +                if (count != n) {
309 +                    count = n;
310 +                    removed = true;
311 +                }
312              } finally {
313                  lock.unlock();
314              }
# Line 311 | Line 317 | public class ReadMostlyVector<E> impleme
317      }
318  
319      final void internalClear(int origin, int bound) {
314        Object[] items = array;
320          int n = count;
321          int fence = bound < 0 || bound > n ? n : bound;
322          if (origin >= 0 && origin < fence) {
323 +            Object[] items = array;
324              int removed = fence - origin;
325              int newCount = n - removed;
326              int mv = n - (origin + removed);
# Line 327 | Line 333 | public class ReadMostlyVector<E> impleme
333      }
334  
335      final boolean internalContainsAll(Collection<?> c, int origin, int bound) {
336 <        SequenceLock lock = this.lock;
336 >        final SequenceLock lock = this.lock;
337          boolean contained;
338          boolean locked = false;
339          try {
340              for (;;) {
341                  long seq = lock.awaitAvailability();
342 +                int n = count;
343                  Object[] items = array;
344                  int len = items.length;
338                int n = count;
345                  if (n > len)
346                      continue;
347                  int fence = bound < 0 || bound > n ? n : bound;
# Line 367 | Line 373 | public class ReadMostlyVector<E> impleme
373      }
374  
375      final boolean internalEquals(List<?> list, int origin, int bound) {
376 <        SequenceLock lock = this.lock;
376 >        final SequenceLock lock = this.lock;
377          boolean locked = false;
378          boolean equal;
379          try {
# Line 408 | Line 414 | public class ReadMostlyVector<E> impleme
414      }
415  
416      final int internalHashCode(int origin, int bound) {
417 <        SequenceLock lock = this.lock;
417 >        final SequenceLock lock = this.lock;
418          int hash;
419          boolean locked = false;
420          try {
# Line 416 | Line 422 | public class ReadMostlyVector<E> impleme
422                  hash = 1;
423                  long seq = lock.awaitAvailability();
424                  Object[] items = array;
419                int len = items.length;
425                  int n = count;
426 +                int len = items.length;
427                  if (n > len)
428                      continue;
429                  int fence = bound < 0 || bound > n ? n : bound;
# Line 440 | Line 446 | public class ReadMostlyVector<E> impleme
446      }
447  
448      final String internalToString(int origin, int bound) {
449 <        SequenceLock lock = this.lock;
449 >        final SequenceLock lock = this.lock;
450          String ret;
451          boolean locked = false;
452          try {
453              outer:for (;;) {
454                  long seq = lock.awaitAvailability();
455                  Object[] items = array;
450                int len = items.length;
456                  int n = count;
457 +                int len = items.length;
458                  if (n > len)
459                      continue;
460                  int fence = bound < 0 || bound > n ? n : bound;
# Line 487 | Line 493 | public class ReadMostlyVector<E> impleme
493  
494      final Object[] internalToArray(int origin, int bound) {
495          Object[] result;
496 <        SequenceLock lock = this.lock;
496 >        final SequenceLock lock = this.lock;
497          boolean locked = false;
498          try {
499              for (;;) {
500                  result = null;
501                  long seq = lock.awaitAvailability();
502                  Object[] items = array;
497                int len = items.length;
503                  int n = count;
504 +                int len = items.length;
505                  if (n > len)
506                      continue;
507                  int fence = bound < 0 || bound > n ? n : bound;
# Line 514 | Line 520 | public class ReadMostlyVector<E> impleme
520          return result;
521      }
522  
523 +    @SuppressWarnings("unchecked")
524      final <T> T[] internalToArray(T[] a, int origin, int bound) {
525          int alen = a.length;
526          T[] result;
527 <        SequenceLock lock = this.lock;
527 >        final SequenceLock lock = this.lock;
528          boolean locked = false;
529          try {
530              for (;;) {
531                  long seq = lock.awaitAvailability();
532                  Object[] items = array;
526                int len = items.length;
533                  int n = count;
534 +                int len = items.length;
535                  if (n > len)
536                      continue;
537                  int fence = bound < 0 || bound > n ? n : bound;
# Line 556 | Line 563 | public class ReadMostlyVector<E> impleme
563      // public List methods
564  
565      public boolean add(E e) {
566 <        SequenceLock lock = this.lock;
566 >        final SequenceLock lock = this.lock;
567          lock.lock();
568          try {
569              rawAdd(e);
# Line 567 | Line 574 | public class ReadMostlyVector<E> impleme
574      }
575  
576      public void add(int index, E element) {
577 <        SequenceLock lock = this.lock;
577 >        final SequenceLock lock = this.lock;
578          lock.lock();
579          try {
580              rawAddAt(index, element);
# Line 581 | Line 588 | public class ReadMostlyVector<E> impleme
588          int len = elements.length;
589          if (len == 0)
590              return false;
591 <        SequenceLock lock = this.lock;
591 >        final SequenceLock lock = this.lock;
592          lock.lock();
593          try {
594 <            int newCount = count + len;
595 <            if (newCount >= array.length)
596 <                grow(newCount);
597 <            System.arraycopy(elements, 0, array, count, len);
594 >            Object[] items = array;
595 >            int n = count;
596 >            int newCount = n + len;
597 >            if (newCount >= items.length)
598 >                items = grow(newCount);
599 >            System.arraycopy(elements, 0, items, n, len);
600              count = newCount;
601          } finally {
602              lock.unlock();
# Line 596 | Line 605 | public class ReadMostlyVector<E> impleme
605      }
606  
607      public boolean addAll(int index, Collection<? extends E> c) {
608 <        SequenceLock lock = this.lock;
608 >        final SequenceLock lock = this.lock;
609          boolean ret;
610          Object[] elements = c.toArray();
611          lock.lock();
# Line 609 | Line 618 | public class ReadMostlyVector<E> impleme
618      }
619  
620      public void clear() {
621 <        SequenceLock lock = this.lock;
621 >        final SequenceLock lock = this.lock;
622          lock.lock();
623          try {
624 +            int n = count;
625              Object[] items = array;
626 <            for (int i = 0; i < count; i++)
626 >            for (int i = 0; i < n; i++)
627                  items[i] = null;
628              count = 0;
629          } finally {
# Line 638 | Line 648 | public class ReadMostlyVector<E> impleme
648      }
649  
650      public E get(int index) {
651 <        SequenceLock lock = this.lock;
651 >        final SequenceLock lock = this.lock;
652          for (;;) {
653              long seq = lock.awaitAvailability();
644            Object[] items = array;
654              int n = count;
655 <            if (n > items.length)
656 <                continue;
657 <            Object e; boolean ex;
649 <            if (index < 0 || index >= n) {
650 <                e = null;
651 <                ex = true;
652 <            }
653 <            else {
654 <                e = items[index];
655 <                ex = false;
656 <            }
655 >            Object[] items = array;
656 >            @SuppressWarnings("unchecked")
657 >            E e = (index < items.length) ? (E) items[index] : null;
658              if (lock.getSequence() == seq) {
659 <                if (ex)
659 >                if (index >= n)
660                      throw new ArrayIndexOutOfBoundsException(index);
661 <                else
661 <                    return (E)e;
661 >                return e;
662              }
663          }
664      }
# Line 668 | Line 668 | public class ReadMostlyVector<E> impleme
668      }
669  
670      public int indexOf(Object o) {
671 <        SequenceLock lock = this.lock;
672 <        for (;;) {
673 <            long seq = lock.awaitAvailability();
674 <            Object[] items = array;
675 <            int n = count;
676 <            if (n <= items.length) {
677 <                for (int i = 0; i < n; ++i) {
678 <                    Object e = items[i];
679 <                    if (lock.getSequence() != seq) {
680 <                        lock.lock();
681 <                        try {
682 <                            return rawIndexOf(o, 0, count);
683 <                        } finally {
684 <                            lock.unlock();
685 <                        }
686 <                    }
687 <                    else if ((o == null) ? e == null : o.equals(e))
688 <                        return i;
689 <                }
690 <                return -1;
691 <            }
692 <        }
671 >        return indexOf(o, 0);
672      }
673  
674      public boolean isEmpty() {
# Line 701 | Line 680 | public class ReadMostlyVector<E> impleme
680      }
681  
682      public int lastIndexOf(Object o) {
683 <        SequenceLock lock = this.lock;
683 >        final SequenceLock lock = this.lock;
684          for (;;) {
685              long seq = lock.awaitAvailability();
686              Object[] items = array;
# Line 712 | Line 691 | public class ReadMostlyVector<E> impleme
691                      if (lock.getSequence() != seq) {
692                          lock.lock();
693                          try {
694 <                            return rawLastIndexOf(o, 0, count);
694 >                            return rawLastIndexOf(o, count - 1, 0);
695                          } finally {
696                              lock.unlock();
697                          }
# Line 734 | Line 713 | public class ReadMostlyVector<E> impleme
713      }
714  
715      public E remove(int index) {
716 <        SequenceLock lock = this.lock;
738 <        Object oldValue;
716 >        final SequenceLock lock = this.lock;
717          lock.lock();
718          try {
719              if (index < 0 || index >= count)
720                  throw new ArrayIndexOutOfBoundsException(index);
721 <            oldValue = array[index];
721 >            @SuppressWarnings("unchecked")
722 >            E oldValue = (E) array[index];
723              rawRemoveAt(index);
724 +            return oldValue;
725          } finally {
726              lock.unlock();
727          }
748        return (E)oldValue;
728      }
729  
730      public boolean remove(Object o) {
731 <        SequenceLock lock = this.lock;
753 <        boolean removed;
731 >        final SequenceLock lock = this.lock;
732          lock.lock();
733          try {
734 <            removed = rawRemoveAt(rawIndexOf(o, 0, count));
734 >            return rawRemoveAt(rawIndexOf(o, 0, count));
735          } finally {
736              lock.unlock();
737          }
760        return removed;
738      }
739  
740      public boolean removeAll(Collection<?> c) {
# Line 769 | Line 746 | public class ReadMostlyVector<E> impleme
746      }
747  
748      public E set(int index, E element) {
749 <        Object oldValue;
773 <        SequenceLock lock = this.lock;
749 >        final SequenceLock lock = this.lock;
750          lock.lock();
751          try {
752              Object[] items = array;
753              if (index < 0 || index >= count)
754                  throw new ArrayIndexOutOfBoundsException(index);
755 <            oldValue = items[index];
755 >            @SuppressWarnings("unchecked")
756 >            E oldValue = (E) items[index];
757              items[index] = element;
758 +            return oldValue;
759          } finally {
760              lock.unlock();
761          }
784        return (E)oldValue;
762      }
763  
764      public int size() {
# Line 817 | Line 794 | public class ReadMostlyVector<E> impleme
794       * @return {@code true} if the element was added
795       */
796      public boolean addIfAbsent(E e) {
797 <        boolean added;
821 <        SequenceLock lock = this.lock;
797 >        final SequenceLock lock = this.lock;
798          lock.lock();
799          try {
800              if (rawIndexOf(e, 0, count) < 0) {
801                  rawAdd(e);
802 <                added = true;
802 >                return true;
803              }
804              else
805 <                added = false;
805 >                return false;
806          } finally {
807              lock.unlock();
808          }
833        return added;
809      }
810  
811      /**
# Line 852 | Line 827 | public class ReadMostlyVector<E> impleme
827              lock.lock();
828              try {
829                  for (int i = 0; i < clen; ++i) {
830 <                    Object e = cs[i];
830 >                    @SuppressWarnings("unchecked")
831 >                    E e = (E) cs[i];
832                      if (rawIndexOf(e, 0, count) < 0) {
833                          rawAdd(e);
834                          ++added;
# Line 878 | Line 854 | public class ReadMostlyVector<E> impleme
854      }
855  
856      static final class SnapshotIterator<E> implements Iterator<E> {
857 <        final Object[] items;
858 <        int cursor;
857 >        private final Object[] items;
858 >        private int cursor;
859          SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); }
860          public boolean hasNext() { return cursor < items.length; }
861 +        @SuppressWarnings("unchecked")
862          public E next() {
863              if (cursor < items.length)
864                  return (E) items[cursor++];
# Line 894 | Line 871 | public class ReadMostlyVector<E> impleme
871  
872      /** See {@link Vector#firstElement} */
873      public E firstElement() {
874 <        SequenceLock lock = this.lock;
874 >        final SequenceLock lock = this.lock;
875          for (;;) {
876              long seq = lock.awaitAvailability();
877              Object[] items = array;
901            int len = items.length;
878              int n = count;
879 <            if (n > len)
880 <                continue;
905 <            Object e; boolean ex;
906 <            if (n > 0) {
907 <                e = items[0];
908 <                ex = false;
909 <            }
910 <            else {
911 <                e = null;
912 <                ex = true;
913 <            }
879 >            @SuppressWarnings("unchecked")
880 >            E e = (items.length > 0) ? (E) items[0] : null;
881              if (lock.getSequence() == seq) {
882 <                if (ex)
882 >                if (n <= 0)
883                      throw new NoSuchElementException();
884 <                else
918 <                    return (E)e;
884 >                return e;
885              }
886          }
887      }
888  
889      /** See {@link Vector#lastElement} */
890      public E lastElement() {
891 <        SequenceLock lock = this.lock;
891 >        final SequenceLock lock = this.lock;
892          for (;;) {
893              long seq = lock.awaitAvailability();
894              Object[] items = array;
929            int len = items.length;
895              int n = count;
896 <            if (n > len)
897 <                continue;
933 <            Object e; boolean ex;
934 <            if (n > 0) {
935 <                e = items[n - 1];
936 <                ex = false;
937 <            }
938 <            else {
939 <                e = null;
940 <                ex = true;
941 <            }
896 >            @SuppressWarnings("unchecked")
897 >            E e = (n > 0 && items.length >= n) ? (E) items[n - 1] : null;
898              if (lock.getSequence() == seq) {
899 <                if (ex)
899 >                if (n <= 0)
900                      throw new NoSuchElementException();
901 <                else
946 <                    return (E)e;
901 >                return e;
902              }
903          }
904      }
905  
906      /** See {@link Vector#indexOf(Object, int)} */
907      public int indexOf(Object o, int index) {
908 <        SequenceLock lock = this.lock;
908 >        final SequenceLock lock = this.lock;
909          int idx = 0;
910          boolean ex = false;
911          long seq = lock.awaitAvailability();
# Line 969 | Line 924 | public class ReadMostlyVector<E> impleme
924                  if (index < 0)
925                      ex = true;
926                  else
927 <                    idx = rawIndexOf(o, 0, count);
927 >                    idx = rawIndexOf(o, index, count);
928              } finally {
929                  lock.unlock();
930              }
# Line 981 | Line 936 | public class ReadMostlyVector<E> impleme
936  
937      /** See {@link Vector#lastIndexOf(Object, int)} */
938      public int lastIndexOf(Object o, int index) {
939 <        SequenceLock lock = this.lock;
939 >        final SequenceLock lock = this.lock;
940          int idx = 0;
941          boolean ex = false;
942          long seq = lock.awaitAvailability();
# Line 1014 | Line 969 | public class ReadMostlyVector<E> impleme
969      public void setSize(int newSize) {
970          if (newSize < 0)
971              throw new ArrayIndexOutOfBoundsException(newSize);
972 <        SequenceLock lock = this.lock;
972 >        final SequenceLock lock = this.lock;
973          lock.lock();
974          try {
975              int n = count;
# Line 1033 | Line 988 | public class ReadMostlyVector<E> impleme
988  
989      /** See {@link Vector#copyInto} */
990      public void copyInto(Object[] anArray) {
991 <        SequenceLock lock = this.lock;
991 >        final SequenceLock lock = this.lock;
992          lock.lock();
993          try {
994              System.arraycopy(array, 0, anArray, 0, count);
# Line 1044 | Line 999 | public class ReadMostlyVector<E> impleme
999  
1000      /** See {@link Vector#trimToSize} */
1001      public void trimToSize() {
1002 <        SequenceLock lock = this.lock;
1002 >        final SequenceLock lock = this.lock;
1003          lock.lock();
1004          try {
1005              Object[] items = array;
1006 <            if (count < items.length)
1007 <                array = Arrays.copyOf(items, count);
1006 >            int n = count;
1007 >            if (n < items.length)
1008 >                array = Arrays.copyOf(items, n);
1009          } finally {
1010              lock.unlock();
1011          }
# Line 1058 | Line 1014 | public class ReadMostlyVector<E> impleme
1014      /** See {@link Vector#ensureCapacity} */
1015      public void ensureCapacity(int minCapacity) {
1016          if (minCapacity > 0) {
1017 <            SequenceLock lock = this.lock;
1017 >            final SequenceLock lock = this.lock;
1018              lock.lock();
1019              try {
1020                  if (minCapacity - array.length > 0)
# Line 1117 | Line 1073 | public class ReadMostlyVector<E> impleme
1073      // other methods
1074  
1075      public ReadMostlyVector<E> clone() {
1076 <        SequenceLock lock = this.lock;
1076 >        final SequenceLock lock = this.lock;
1077          Object[] a = null;
1078          boolean retry = false;
1079          long seq = lock.awaitAvailability();
# Line 1141 | Line 1097 | public class ReadMostlyVector<E> impleme
1097  
1098      private void writeObject(java.io.ObjectOutputStream s)
1099              throws java.io.IOException {
1100 <        SequenceLock lock = this.lock;
1100 >        final SequenceLock lock = this.lock;
1101          lock.lock();
1102          try {
1103              s.defaultWriteObject();
# Line 1150 | Line 1106 | public class ReadMostlyVector<E> impleme
1106          }
1107      }
1108  
1109 <    static final class Itr<E> implements ListIterator<E>, Enumeration<E>  {
1109 >    static final class Itr<E> implements ListIterator<E>, Enumeration<E> {
1110          final ReadMostlyVector<E> list;
1111          final SequenceLock lock;
1112          Object[] items;
1113 <        Object next, prev;
1113 >        E next, prev;
1114          long seq;
1115          int cursor;
1116          int fence;
# Line 1180 | Line 1136 | public class ReadMostlyVector<E> impleme
1136                       lock.getSequence() != seq);
1137          }
1138  
1139 +        @SuppressWarnings("unchecked")
1140          public boolean hasNext() {
1141              boolean valid;
1142              int i = cursor;
# Line 1188 | Line 1145 | public class ReadMostlyVector<E> impleme
1145                      valid = false;
1146                      break;
1147                  }
1148 <                next = items[i];
1148 >                next = (E) items[i];
1149                  if (lock.getSequence() == seq) {
1150                      valid = true;
1151                      break;
# Line 1198 | Line 1155 | public class ReadMostlyVector<E> impleme
1155              return validNext = valid;
1156          }
1157  
1158 +        @SuppressWarnings("unchecked")
1159          public boolean hasPrevious() {
1160              boolean valid;
1161              int i = cursor - 1;
# Line 1206 | Line 1164 | public class ReadMostlyVector<E> impleme
1164                      valid = false;
1165                      break;
1166                  }
1167 <                prev = items[i];
1167 >                prev = (E) items[i];
1168                  if (lock.getSequence() == seq) {
1169                      valid = true;
1170                      break;
# Line 1220 | Line 1178 | public class ReadMostlyVector<E> impleme
1178              if (validNext || hasNext()) {
1179                  validNext = false;
1180                  lastRet = cursor++;
1181 <                return (E) next;
1181 >                return next;
1182              }
1183              throw new NoSuchElementException();
1184          }
# Line 1229 | Line 1187 | public class ReadMostlyVector<E> impleme
1187              if (validPrev || hasPrevious()) {
1188                  validPrev = false;
1189                  lastRet = cursor--;
1190 <                return (E) prev;
1190 >                return prev;
1191              }
1192              throw new NoSuchElementException();
1193          }
# Line 1286 | Line 1244 | public class ReadMostlyVector<E> impleme
1244          public int previousIndex() { return cursor - 1; }
1245      }
1246  
1247 <    static final class ReadMostlyVectorSublist<E> implements List<E>, RandomAccess, java.io.Serializable {
1247 >    static final class ReadMostlyVectorSublist<E>
1248 >            implements List<E>, RandomAccess, java.io.Serializable {
1249 >        static final long serialVersionUID = 3041673470172026059L;
1250 >
1251          final ReadMostlyVector<E> list;
1252          final int offset;
1253          volatile int size;
1254  
1255 <        ReadMostlyVectorSublist(ReadMostlyVector<E> list, int offset, int size) {
1255 >        ReadMostlyVectorSublist(ReadMostlyVector<E> list,
1256 >                                int offset, int size) {
1257              this.list = list;
1258              this.offset = offset;
1259              this.size = size;
# Line 1303 | Line 1265 | public class ReadMostlyVector<E> impleme
1265          }
1266  
1267          public boolean add(E element) {
1268 <            SequenceLock lock = list.lock;
1268 >            final SequenceLock lock = list.lock;
1269              lock.lock();
1270              try {
1271                  int c = size;
# Line 1316 | Line 1278 | public class ReadMostlyVector<E> impleme
1278          }
1279  
1280          public void add(int index, E element) {
1281 <            SequenceLock lock = list.lock;
1281 >            final SequenceLock lock = list.lock;
1282              lock.lock();
1283              try {
1284                  if (index < 0 || index > size)
# Line 1330 | Line 1292 | public class ReadMostlyVector<E> impleme
1292  
1293          public boolean addAll(Collection<? extends E> c) {
1294              Object[] elements = c.toArray();
1295 <            int added;
1334 <            SequenceLock lock = list.lock;
1295 >            final SequenceLock lock = list.lock;
1296              lock.lock();
1297              try {
1298                  int s = size;
1299                  int pc = list.count;
1300                  list.rawAddAllAt(offset + s, elements);
1301 <                added = list.count - pc;
1301 >                int added = list.count - pc;
1302                  size = s + added;
1303 +                return added != 0;
1304              } finally {
1305                  lock.unlock();
1306              }
1345            return added != 0;
1307          }
1308  
1309          public boolean addAll(int index, Collection<? extends E> c) {
1310              Object[] elements = c.toArray();
1311 <            int added;
1351 <            SequenceLock lock = list.lock;
1311 >            final SequenceLock lock = list.lock;
1312              lock.lock();
1313              try {
1314                  int s = size;
# Line 1356 | Line 1316 | public class ReadMostlyVector<E> impleme
1316                      throw new ArrayIndexOutOfBoundsException(index);
1317                  int pc = list.count;
1318                  list.rawAddAllAt(index + offset, elements);
1319 <                added = list.count - pc;
1319 >                int added = list.count - pc;
1320                  size = s + added;
1321 +                return added != 0;
1322              } finally {
1323                  lock.unlock();
1324              }
1364            return added != 0;
1325          }
1326  
1327          public void clear() {
1328 <            SequenceLock lock = list.lock;
1328 >            final SequenceLock lock = list.lock;
1329              lock.lock();
1330              try {
1331                  list.internalClear(offset, offset + size);
# Line 1402 | Line 1362 | public class ReadMostlyVector<E> impleme
1362          }
1363  
1364          public int indexOf(Object o) {
1365 <            SequenceLock lock = list.lock;
1365 >            final SequenceLock lock = list.lock;
1366              long seq = lock.awaitAvailability();
1367              Object[] items = list.array;
1368              int c = list.count;
# Line 1430 | Line 1390 | public class ReadMostlyVector<E> impleme
1390          }
1391  
1392          public int lastIndexOf(Object o) {
1393 <            SequenceLock lock = list.lock;
1393 >            final SequenceLock lock = list.lock;
1394              long seq = lock.awaitAvailability();
1395              Object[] items = list.array;
1396              int c = list.count;
# Line 1458 | Line 1418 | public class ReadMostlyVector<E> impleme
1418          }
1419  
1420          public E remove(int index) {
1421 <            Object result;
1462 <            SequenceLock lock = list.lock;
1421 >            final SequenceLock lock = list.lock;
1422              lock.lock();
1423              try {
1424                  Object[] items = list.array;
1425                  int i = index + offset;
1426                  if (index < 0 || index >= size || i >= items.length)
1427                      throw new ArrayIndexOutOfBoundsException(index);
1428 <                result = items[i];
1428 >                @SuppressWarnings("unchecked")
1429 >                E result = (E) items[i];
1430                  list.rawRemoveAt(i);
1431                  size--;
1432 +                return result;
1433              } finally {
1434                  lock.unlock();
1435              }
1475            return (E)result;
1436          }
1437  
1438          public boolean remove(Object o) {
1439 <            boolean removed = false;
1480 <            SequenceLock lock = list.lock;
1439 >            final SequenceLock lock = list.lock;
1440              lock.lock();
1441              try {
1442                  if (list.rawRemoveAt(list.rawIndexOf(o, offset,
1443                                                       offset + size))) {
1485                    removed = true;
1444                      --size;
1445 +                    return true;
1446                  }
1447 +                else
1448 +                    return false;
1449              } finally {
1450                  lock.unlock();
1451              }
1491            return removed;
1452          }
1453  
1454          public boolean removeAll(Collection<?> c) {
# Line 1536 | Line 1496 | public class ReadMostlyVector<E> impleme
1496          final ReadMostlyVector<E> list;
1497          final SequenceLock lock;
1498          Object[] items;
1499 <        Object next, prev;
1499 >        E next, prev;
1500          long seq;
1501          int cursor;
1502          int fence;
# Line 1567 | Line 1527 | public class ReadMostlyVector<E> impleme
1527              } while (lock.getSequence() != seq);
1528          }
1529  
1530 +        @SuppressWarnings("unchecked")
1531          public boolean hasNext() {
1532              boolean valid;
1533              int i = cursor;
# Line 1575 | Line 1536 | public class ReadMostlyVector<E> impleme
1536                      valid = false;
1537                      break;
1538                  }
1539 <                next = items[i];
1539 >                next = (E) items[i];
1540                  if (lock.getSequence() == seq) {
1541                      valid = true;
1542                      break;
# Line 1585 | Line 1546 | public class ReadMostlyVector<E> impleme
1546              return validNext = valid;
1547          }
1548  
1549 +        @SuppressWarnings("unchecked")
1550          public boolean hasPrevious() {
1551              boolean valid;
1552              int i = cursor - 1;
# Line 1593 | Line 1555 | public class ReadMostlyVector<E> impleme
1555                      valid = false;
1556                      break;
1557                  }
1558 <                prev = items[i];
1558 >                prev = (E) items[i];
1559                  if (lock.getSequence() == seq) {
1560                      valid = true;
1561                      break;
# Line 1607 | Line 1569 | public class ReadMostlyVector<E> impleme
1569              if (validNext || hasNext()) {
1570                  validNext = false;
1571                  lastRet = cursor++;
1572 <                return (E) next;
1572 >                return next;
1573              }
1574              throw new NoSuchElementException();
1575          }
# Line 1616 | Line 1578 | public class ReadMostlyVector<E> impleme
1578              if (validPrev || hasPrevious()) {
1579                  validPrev = false;
1580                  lastRet = cursor--;
1581 <                return (E) prev;
1581 >                return prev;
1582              }
1583              throw new NoSuchElementException();
1584          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines