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.5 by dl, Sat Jul 16 16:05:32 2011 UTC vs.
Revision 1.15 by jsr166, Mon Dec 19 19:18:35 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 57 | Line 58 | public class ReadMostlyVector<E> impleme
58      private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
59  
60      // fields are non-private to simpify nested class access
61 <    Object[] array;
61 >    volatile Object[] array;
62      final SequenceLock lock;
63 <    int count;
63 >    volatile int count;
64      final int capacityIncrement;
65  
66      /**
# Line 129 | Line 130 | public class ReadMostlyVector<E> impleme
130      }
131  
132      // For explanation, see CopyOnWriteArrayList
133 <    final void grow(int minCapacity) {
133 >    final Object[] grow(int minCapacity) {
134          int oldCapacity = array.length;
135          int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
136                                           capacityIncrement : oldCapacity);
# Line 137 | Line 138 | public class ReadMostlyVector<E> impleme
138              newCapacity = minCapacity;
139          if (newCapacity - MAX_ARRAY_SIZE > 0)
140              newCapacity = hugeCapacity(minCapacity);
141 <        array = Arrays.copyOf(array, newCapacity);
141 >        return array = Arrays.copyOf(array, newCapacity);
142      }
143  
144      static int hugeCapacity(int minCapacity) {
# Line 161 | Line 162 | public class ReadMostlyVector<E> impleme
162              Object e = items[i];
163              if (lock.getSequence() != seq)
164                  break;
165 <            if (x == null? e == null : x.equals(e))
165 >            if ((x == null) ? e == null : x.equals(e))
166                  return i;
167          }
168          return -1;
# Line 171 | Line 172 | public class ReadMostlyVector<E> impleme
172          Object[] items = array;
173          for (int i = index; i < fence; ++i) {
174              Object e = items[i];
175 <            if (x == null? e == null : x.equals(e))
175 >            if ((x == null) ? e == null : x.equals(e))
176                  return i;
177          }
178          return -1;
# Line 183 | Line 184 | public class ReadMostlyVector<E> impleme
184              Object e = items[i];
185              if (lock.getSequence() != seq)
186                  break;
187 <            if (x == null? e == null : x.equals(e))
187 >            if ((x == null) ? e == null : x.equals(e))
188                  return i;
189          }
190          return -1;
# Line 193 | Line 194 | public class ReadMostlyVector<E> impleme
194          Object[] items = array;
195          for (int i = index; i >= origin; --i) {
196              Object e = items[i];
197 <            if (x == null? e == null : x.equals(e))
197 >            if ((x == null) ? e == null : x.equals(e))
198                  return i;
199          }
200          return -1;
201      }
202  
203 <    final void rawAdd(Object e) {
203 >    final void rawAdd(E e) {
204          int n = count;
205 <        if (n >= array.length)
206 <            grow(n + 1);
207 <        array[n] = e;
205 >        Object[] items = array;
206 >        if (n >= items.length)
207 >            items = grow(n + 1);
208 >        items[n] = e;
209          count = n + 1;
210      }
211  
212 <    final void rawAddAt(int index, Object e) {
212 >    final void rawAddAt(int index, E e) {
213          int n = count;
214 +        Object[] items = array;
215          if (index > n)
216              throw new ArrayIndexOutOfBoundsException(index);
217 <        if (n >= array.length)
218 <            grow(n + 1);
217 >        if (n >= items.length)
218 >            items = grow(n + 1);
219          if (index < n)
220 <            System.arraycopy(array, index, array, index + 1, n - index);
221 <        array[index] = e;
220 >            System.arraycopy(items, index, items, index + 1, n - index);
221 >        items[index] = e;
222          count = n + 1;
223      }
224  
225      final boolean rawAddAllAt(int index, Object[] elements) {
226          int n = count;
227 +        Object[] items = array;
228          if (index < 0 || index > n)
229              throw new ArrayIndexOutOfBoundsException(index);
230          int len = elements.length;
231          if (len == 0)
232              return false;
233          int newCount = n + len;
234 <        if (newCount >= array.length)
235 <            grow(newCount);
236 <        int mv = count - index;
234 >        if (newCount >= items.length)
235 >            items = grow(newCount);
236 >        int mv = n - index;
237          if (mv > 0)
238 <            System.arraycopy(array, index, array, index + len, mv);
239 <        System.arraycopy(elements, 0, array, index, len);
238 >            System.arraycopy(items, index, items, index + len, mv);
239 >        System.arraycopy(elements, 0, items, index, len);
240          count = newCount;
241          return true;
242      }
243  
244      final boolean rawRemoveAt(int index) {
245          int n = count - 1;
246 +        Object[] items = array;
247          if (index < 0 || index > n)
248              return false;
249          int mv = n - index;
250          if (mv > 0)
251 <            System.arraycopy(array, index + 1, array, index, mv);
252 <        array[n] = null;
251 >            System.arraycopy(items, index + 1, items, index, mv);
252 >        items[n] = null;
253          count = n;
254          return true;
255      }
# Line 257 | Line 262 | public class ReadMostlyVector<E> impleme
262       * field under lock.
263       */
264      final boolean internalRemoveAll(Collection<?> c, int origin, int bound) {
265 <        SequenceLock lock = this.lock;
265 >        final SequenceLock lock = this.lock;
266          boolean removed = false;
267          lock.lock();
268          try {
# Line 276 | Line 281 | public class ReadMostlyVector<E> impleme
281      }
282  
283      final boolean internalRetainAll(Collection<?> c, int origin, int bound) {
284 <        SequenceLock lock = this.lock;
284 >        final SequenceLock lock = this.lock;
285          boolean removed = false;
286          if (c != this) {
287              lock.lock();
288              try {
289 +                Object[] items = array;
290                  int i = origin;
291                  int n = count;
292                  int fence = bound < 0 || bound > n ? n : bound;
293                  while (i >= 0 && i < fence) {
294 <                    if (c.contains(array[i]))
294 >                    if (c.contains(items[i]))
295                          ++i;
296                      else {
297                          --fence;
298 <                        int mv = --count - i;
298 >                        int mv = --n - i;
299                          if (mv > 0)
300 <                            System.arraycopy(array, i + 1, array, i, mv);
295 <                        removed = true;
300 >                            System.arraycopy(items, i + 1, items, i, mv);
301                      }
302                  }
303 +                if (count != n) {
304 +                    count = n;
305 +                    removed = true;
306 +                }
307              } finally {
308                  lock.unlock();
309              }
# Line 306 | Line 315 | public class ReadMostlyVector<E> impleme
315          int n = count;
316          int fence = bound < 0 || bound > n ? n : bound;
317          if (origin >= 0 && origin < fence) {
318 +            Object[] items = array;
319              int removed = fence - origin;
320              int newCount = n - removed;
321              int mv = n - (origin + removed);
322              if (mv > 0)
323 <                System.arraycopy(array, origin + removed, array, origin, mv);
323 >                System.arraycopy(items, origin + removed, items, origin, mv);
324              for (int i = n; i < newCount; ++i)
325 <                array[i] = null;
325 >                items[i] = null;
326              count = newCount;
327          }
328      }
329  
330      final boolean internalContainsAll(Collection<?> c, int origin, int bound) {
331 <        SequenceLock lock = this.lock;
331 >        final SequenceLock lock = this.lock;
332          boolean contained;
333          boolean locked = false;
334          try {
335              for (;;) {
336                  long seq = lock.awaitAvailability();
337 +                int n = count;
338                  Object[] items = array;
339                  int len = items.length;
329                int n = count;
340                  if (n > len)
341                      continue;
342                  int fence = bound < 0 || bound > n ? n : bound;
# Line 335 | Line 345 | public class ReadMostlyVector<E> impleme
345                  else {
346                      contained = true;
347                      for (Object e : c) {
348 <                        int idx = (locked?
348 >                        int idx = (locked ?
349                                     rawIndexOf(e, origin, fence) :
350                                     validatedIndexOf(e, items, origin,
351                                                      fence, seq));
# Line 358 | Line 368 | public class ReadMostlyVector<E> impleme
368      }
369  
370      final boolean internalEquals(List<?> list, int origin, int bound) {
371 <        SequenceLock lock = this.lock;
371 >        final SequenceLock lock = this.lock;
372          boolean locked = false;
373          boolean equal;
374          try {
# Line 399 | Line 409 | public class ReadMostlyVector<E> impleme
409      }
410  
411      final int internalHashCode(int origin, int bound) {
412 <        SequenceLock lock = this.lock;
412 >        final SequenceLock lock = this.lock;
413          int hash;
414          boolean locked = false;
415          try {
# Line 407 | Line 417 | public class ReadMostlyVector<E> impleme
417                  hash = 1;
418                  long seq = lock.awaitAvailability();
419                  Object[] items = array;
410                int len = items.length;
420                  int n = count;
421 +                int len = items.length;
422                  if (n > len)
423                      continue;
424                  int fence = bound < 0 || bound > n ? n : bound;
# Line 431 | Line 441 | public class ReadMostlyVector<E> impleme
441      }
442  
443      final String internalToString(int origin, int bound) {
444 <        SequenceLock lock = this.lock;
444 >        final SequenceLock lock = this.lock;
445          String ret;
446          boolean locked = false;
447          try {
448              outer:for (;;) {
449                  long seq = lock.awaitAvailability();
450                  Object[] items = array;
441                int len = items.length;
451                  int n = count;
452 +                int len = items.length;
453                  if (n > len)
454                      continue;
455                  int fence = bound < 0 || bound > n ? n : bound;
# Line 478 | Line 488 | public class ReadMostlyVector<E> impleme
488  
489      final Object[] internalToArray(int origin, int bound) {
490          Object[] result;
491 <        SequenceLock lock = this.lock;
491 >        final SequenceLock lock = this.lock;
492          boolean locked = false;
493          try {
494              for (;;) {
495                  result = null;
496                  long seq = lock.awaitAvailability();
497                  Object[] items = array;
488                int len = items.length;
498                  int n = count;
499 +                int len = items.length;
500                  if (n > len)
501                      continue;
502                  int fence = bound < 0 || bound > n ? n : bound;
# Line 505 | Line 515 | public class ReadMostlyVector<E> impleme
515          return result;
516      }
517  
518 +    @SuppressWarnings("unchecked")
519      final <T> T[] internalToArray(T[] a, int origin, int bound) {
520          int alen = a.length;
521          T[] result;
522 <        SequenceLock lock = this.lock;
522 >        final SequenceLock lock = this.lock;
523          boolean locked = false;
524          try {
525              for (;;) {
526                  long seq = lock.awaitAvailability();
527                  Object[] items = array;
517                int len = items.length;
528                  int n = count;
529 +                int len = items.length;
530                  if (n > len)
531                      continue;
532                  int fence = bound < 0 || bound > n ? n : bound;
# Line 524 | Line 535 | public class ReadMostlyVector<E> impleme
535                      rlen = 0;
536                  if (origin < 0 || alen >= rlen) {
537                      if (rlen > 0)
538 <                        System.arraycopy(array, 0, a, origin, rlen);
538 >                        System.arraycopy(items, 0, a, origin, rlen);
539                      if (alen > rlen)
540                          a[rlen] = null;
541                      result = a;
542                  }
543                  else
544 <                    result = (T[]) Arrays.copyOfRange(array, origin,
544 >                    result = (T[]) Arrays.copyOfRange(items, origin,
545                                                        fence, a.getClass());
546                  if (lock.getSequence() == seq)
547                      break;
# Line 547 | Line 558 | public class ReadMostlyVector<E> impleme
558      // public List methods
559  
560      public boolean add(E e) {
561 <        SequenceLock lock = this.lock;
561 >        final SequenceLock lock = this.lock;
562          lock.lock();
563          try {
564              rawAdd(e);
# Line 558 | Line 569 | public class ReadMostlyVector<E> impleme
569      }
570  
571      public void add(int index, E element) {
572 <        SequenceLock lock = this.lock;
572 >        final SequenceLock lock = this.lock;
573          lock.lock();
574          try {
575              rawAddAt(index, element);
# Line 572 | Line 583 | public class ReadMostlyVector<E> impleme
583          int len = elements.length;
584          if (len == 0)
585              return false;
586 <        SequenceLock lock = this.lock;
586 >        final SequenceLock lock = this.lock;
587          lock.lock();
588          try {
589 <            int newCount = count + len;
590 <            if (newCount >= array.length)
591 <                grow(newCount);
592 <            System.arraycopy(elements, 0, array, count, len);
589 >            Object[] items = array;
590 >            int n = count;
591 >            int newCount = n + len;
592 >            if (newCount >= items.length)
593 >                items = grow(newCount);
594 >            System.arraycopy(elements, 0, items, n, len);
595              count = newCount;
596          } finally {
597              lock.unlock();
# Line 587 | Line 600 | public class ReadMostlyVector<E> impleme
600      }
601  
602      public boolean addAll(int index, Collection<? extends E> c) {
603 <        SequenceLock lock = this.lock;
603 >        final SequenceLock lock = this.lock;
604          boolean ret;
605          Object[] elements = c.toArray();
606          lock.lock();
# Line 600 | Line 613 | public class ReadMostlyVector<E> impleme
613      }
614  
615      public void clear() {
616 <        SequenceLock lock = this.lock;
616 >        final SequenceLock lock = this.lock;
617          lock.lock();
618          try {
619 <            for (int i = 0; i < count; i++)
620 <                array[i] = null;
619 >            int n = count;
620 >            Object[] items = array;
621 >            for (int i = 0; i < n; i++)
622 >                items[i] = null;
623              count = 0;
624          } finally {
625              lock.unlock();
# Line 628 | Line 643 | public class ReadMostlyVector<E> impleme
643      }
644  
645      public E get(int index) {
646 <        SequenceLock lock = this.lock;
646 >        final SequenceLock lock = this.lock;
647          for (;;) {
648              long seq = lock.awaitAvailability();
634            Object[] items = array;
649              int n = count;
650 +            Object[] items = array;
651              if (n > items.length)
652                  continue;
653 <            Object e; boolean ex;
654 <            if (index < 0 || index >= n) {
655 <                e = null;
641 <                ex = true;
642 <            }
643 <            else {
644 <                e = items[index];
645 <                ex = false;
646 <            }
653 >            boolean outOfBounds = (index < 0 || index >= n);
654 >            @SuppressWarnings("unchecked")
655 >            E e = outOfBounds ? null : (E) items[index];
656              if (lock.getSequence() == seq) {
657 <                if (ex)
657 >                if (outOfBounds)
658                      throw new ArrayIndexOutOfBoundsException(index);
659                  else
660 <                    return (E)e;
660 >                    return e;
661              }
662          }
663      }
# Line 658 | Line 667 | public class ReadMostlyVector<E> impleme
667      }
668  
669      public int indexOf(Object o) {
670 <        SequenceLock lock = this.lock;
671 <        long seq = lock.awaitAvailability();
672 <        Object[] items = array;
673 <        int n = count;
674 <        if (n <= items.length) {
675 <            boolean valid = true;
676 <            for (int i = 0; i < n; ++i) {
677 <                Object e = items[i];
678 <                if (lock.getSequence() == seq) {
679 <                    if (o == null? e == null : o.equals(e))
670 >        final SequenceLock lock = this.lock;
671 >        for (;;) {
672 >            long seq = lock.awaitAvailability();
673 >            Object[] items = array;
674 >            int n = count;
675 >            if (n <= items.length) {
676 >                for (int i = 0; i < n; ++i) {
677 >                    Object e = items[i];
678 >                    if (lock.getSequence() != seq) {
679 >                        lock.lock();
680 >                        try {
681 >                            return rawIndexOf(o, 0, count);
682 >                        } finally {
683 >                            lock.unlock();
684 >                        }
685 >                    }
686 >                    else if ((o == null) ? e == null : o.equals(e))
687                          return i;
688                  }
673                else {
674                    valid = false;
675                    break;
676                }
677            }
678            if (valid)
689                  return -1;
690 <        }
681 <        lock.lock();
682 <        try {
683 <            return rawIndexOf(o, 0, count);
684 <        } finally {
685 <            lock.unlock();
690 >            }
691          }
692      }
693  
694      public boolean isEmpty() {
690        long ignore = lock.getSequence();
695          return count == 0;
696      }
697  
698      public Iterator<E> iterator() {
699 <        return new Itr(this, 0);
699 >        return new Itr<E>(this, 0);
700      }
701  
702      public int lastIndexOf(Object o) {
703 <        SequenceLock lock = this.lock;
704 <        long seq = lock.awaitAvailability();
705 <        Object[] items = array;
706 <        int n = count;
707 <        if (n <= items.length) {
708 <            int idx = validatedLastIndexOf(o, items, n - 1, 0, seq);
709 <            if (lock.getSequence() == seq)
710 <                return idx;
711 <        }
712 <        lock.lock();
713 <        try {
714 <            return rawLastIndexOf(o, count - 1, 0);
715 <        } finally {
716 <            lock.unlock();
703 >        final SequenceLock lock = this.lock;
704 >        for (;;) {
705 >            long seq = lock.awaitAvailability();
706 >            Object[] items = array;
707 >            int n = count;
708 >            if (n <= items.length) {
709 >                for (int i = n - 1; i >= 0; --i) {
710 >                    Object e = items[i];
711 >                    if (lock.getSequence() != seq) {
712 >                        lock.lock();
713 >                        try {
714 >                            return rawLastIndexOf(o, 0, count);
715 >                        } finally {
716 >                            lock.unlock();
717 >                        }
718 >                    }
719 >                    else if ((o == null) ? e == null : o.equals(e))
720 >                        return i;
721 >                }
722 >                return -1;
723 >            }
724          }
725      }
726  
727      public ListIterator<E> listIterator() {
728 <        return new Itr(this, 0);
728 >        return new Itr<E>(this, 0);
729      }
730  
731      public ListIterator<E> listIterator(int index) {
732 <        return new Itr(this, index);
732 >        return new Itr<E>(this, index);
733      }
734  
735      public E remove(int index) {
736 <        SequenceLock lock = this.lock;
726 <        Object oldValue;
736 >        final SequenceLock lock = this.lock;
737          lock.lock();
738          try {
739              if (index < 0 || index >= count)
740                  throw new ArrayIndexOutOfBoundsException(index);
741 <            oldValue = array[index];
741 >            @SuppressWarnings("unchecked")
742 >            E oldValue = (E) array[index];
743              rawRemoveAt(index);
744 +            return oldValue;
745          } finally {
746              lock.unlock();
747          }
736        return (E)oldValue;
748      }
749  
750      public boolean remove(Object o) {
751 <        SequenceLock lock = this.lock;
741 <        boolean removed;
751 >        final SequenceLock lock = this.lock;
752          lock.lock();
753          try {
754 <            removed = rawRemoveAt(rawIndexOf(o, 0, count));
754 >            return rawRemoveAt(rawIndexOf(o, 0, count));
755          } finally {
756              lock.unlock();
757          }
748        return removed;
758      }
759  
760      public boolean removeAll(Collection<?> c) {
# Line 757 | Line 766 | public class ReadMostlyVector<E> impleme
766      }
767  
768      public E set(int index, E element) {
769 <        Object oldValue;
761 <        SequenceLock lock = this.lock;
769 >        final SequenceLock lock = this.lock;
770          lock.lock();
771          try {
772 +            Object[] items = array;
773              if (index < 0 || index >= count)
774                  throw new ArrayIndexOutOfBoundsException(index);
775 <            oldValue = array[index];
776 <            array[index] = element;
775 >            @SuppressWarnings("unchecked")
776 >            E oldValue = (E) items[index];
777 >            items[index] = element;
778 >            return oldValue;
779          } finally {
780              lock.unlock();
781          }
771        return (E)oldValue;
782      }
783  
784      public int size() {
775        long ignore = lock.getSequence();
785          return count;
786      }
787  
# Line 781 | Line 790 | public class ReadMostlyVector<E> impleme
790          int ssize = toIndex - fromIndex;
791          if (fromIndex < 0 || toIndex > c || ssize < 0)
792              throw new IndexOutOfBoundsException();
793 <        return new ReadMostlyVectorSublist(this, fromIndex, ssize);
793 >        return new ReadMostlyVectorSublist<E>(this, fromIndex, ssize);
794      }
795  
796      public Object[] toArray() {
# Line 802 | Line 811 | public class ReadMostlyVector<E> impleme
811       * Append the element if not present.
812       *
813       * @param e element to be added to this list, if absent
814 <     * @return <tt>true</tt> if the element was added
814 >     * @return {@code true} if the element was added
815       */
816      public boolean addIfAbsent(E e) {
817 <        boolean added;
809 <        SequenceLock lock = this.lock;
817 >        final SequenceLock lock = this.lock;
818          lock.lock();
819          try {
820              if (rawIndexOf(e, 0, count) < 0) {
821                  rawAdd(e);
822 <                added = true;
822 >                return true;
823              }
824              else
825 <                added = false;
825 >                return false;
826          } finally {
827              lock.unlock();
828          }
821        return added;
829      }
830  
831      /**
# Line 840 | Line 847 | public class ReadMostlyVector<E> impleme
847              lock.lock();
848              try {
849                  for (int i = 0; i < clen; ++i) {
850 <                    Object e = cs[i];
850 >                    @SuppressWarnings("unchecked")
851 >                    E e = (E) cs[i];
852                      if (rawIndexOf(e, 0, count) < 0) {
853                          rawAdd(e);
854                          ++added;
# Line 857 | Line 865 | public class ReadMostlyVector<E> impleme
865       * Returns an iterator operating over a snapshot copy of the
866       * elements of this collection created upon construction of the
867       * iterator. The iterator does <em>NOT</em> support the
868 <     * <tt>remove</tt> method.
868 >     * {@code remove} method.
869       *
870       * @return an iterator over the elements in this list in proper sequence
871       */
# Line 866 | Line 874 | public class ReadMostlyVector<E> impleme
874      }
875  
876      static final class SnapshotIterator<E> implements Iterator<E> {
877 <        final Object[] items;
878 <        int cursor;
877 >        private final Object[] items;
878 >        private int cursor;
879          SnapshotIterator(ReadMostlyVector<E> v) { items = v.toArray(); }
880          public boolean hasNext() { return cursor < items.length; }
881 +        @SuppressWarnings("unchecked")
882          public E next() {
883              if (cursor < items.length)
884                  return (E) items[cursor++];
# Line 882 | Line 891 | public class ReadMostlyVector<E> impleme
891  
892      /** See {@link Vector#firstElement} */
893      public E firstElement() {
894 <        SequenceLock lock = this.lock;
894 >        final SequenceLock lock = this.lock;
895          for (;;) {
896              long seq = lock.awaitAvailability();
897              Object[] items = array;
# Line 890 | Line 899 | public class ReadMostlyVector<E> impleme
899              int n = count;
900              if (n > len)
901                  continue;
902 <            Object e; boolean ex;
903 <            if (n > 0) {
904 <                e = items[0];
896 <                ex = false;
897 <            }
898 <            else {
899 <                e = null;
900 <                ex = true;
901 <            }
902 >            boolean empty = (n == 0);
903 >            @SuppressWarnings("unchecked")
904 >            E e = empty ? null : (E) items[0];
905              if (lock.getSequence() == seq) {
906 <                if (ex)
906 >                if (empty)
907                      throw new NoSuchElementException();
908                  else
909 <                    return (E)e;
909 >                    return e;
910              }
911          }
912      }
913  
914      /** See {@link Vector#lastElement} */
915      public E lastElement() {
916 <        SequenceLock lock = this.lock;
916 >        final SequenceLock lock = this.lock;
917          for (;;) {
918              long seq = lock.awaitAvailability();
919              Object[] items = array;
# Line 918 | Line 921 | public class ReadMostlyVector<E> impleme
921              int n = count;
922              if (n > len)
923                  continue;
924 <            Object e; boolean ex;
925 <            if (n > 0) {
926 <                e = items[n - 1];
924 <                ex = false;
925 <            }
926 <            else {
927 <                e = null;
928 <                ex = true;
929 <            }
924 >            boolean empty = (n == 0);
925 >            @SuppressWarnings("unchecked")
926 >            E e = empty ? null : (E) items[n - 1];
927              if (lock.getSequence() == seq) {
928 <                if (ex)
928 >                if (empty)
929                      throw new NoSuchElementException();
930                  else
931 <                    return (E)e;
931 >                    return e;
932              }
933          }
934      }
935  
936      /** See {@link Vector#indexOf(Object, int)} */
937      public int indexOf(Object o, int index) {
938 <        SequenceLock lock = this.lock;
938 >        final SequenceLock lock = this.lock;
939          int idx = 0;
940          boolean ex = false;
941          long seq = lock.awaitAvailability();
# Line 969 | Line 966 | public class ReadMostlyVector<E> impleme
966  
967      /** See {@link Vector#lastIndexOf(Object, int)} */
968      public int lastIndexOf(Object o, int index) {
969 <        SequenceLock lock = this.lock;
969 >        final SequenceLock lock = this.lock;
970          int idx = 0;
971          boolean ex = false;
972          long seq = lock.awaitAvailability();
# Line 1002 | Line 999 | public class ReadMostlyVector<E> impleme
999      public void setSize(int newSize) {
1000          if (newSize < 0)
1001              throw new ArrayIndexOutOfBoundsException(newSize);
1002 <        SequenceLock lock = this.lock;
1002 >        final SequenceLock lock = this.lock;
1003          lock.lock();
1004          try {
1005              int n = count;
1006              if (newSize > n)
1007                  grow(newSize);
1008              else {
1009 +                Object[] items = array;
1010                  for (int i = newSize ; i < n ; i++)
1011 <                    array[i] = null;
1011 >                    items[i] = null;
1012              }
1013              count = newSize;
1014          } finally {
# Line 1020 | Line 1018 | public class ReadMostlyVector<E> impleme
1018  
1019      /** See {@link Vector#copyInto} */
1020      public void copyInto(Object[] anArray) {
1021 <        SequenceLock lock = this.lock;
1021 >        final SequenceLock lock = this.lock;
1022          lock.lock();
1023          try {
1024              System.arraycopy(array, 0, anArray, 0, count);
# Line 1031 | Line 1029 | public class ReadMostlyVector<E> impleme
1029  
1030      /** See {@link Vector#trimToSize} */
1031      public void trimToSize() {
1032 <        SequenceLock lock = this.lock;
1032 >        final SequenceLock lock = this.lock;
1033          lock.lock();
1034          try {
1035 <            if (count < array.length)
1036 <                array = Arrays.copyOf(array, count);
1035 >            Object[] items = array;
1036 >            int n = count;
1037 >            if (n < items.length)
1038 >                array = Arrays.copyOf(items, n);
1039          } finally {
1040              lock.unlock();
1041          }
# Line 1044 | Line 1044 | public class ReadMostlyVector<E> impleme
1044      /** See {@link Vector#ensureCapacity} */
1045      public void ensureCapacity(int minCapacity) {
1046          if (minCapacity > 0) {
1047 <            SequenceLock lock = this.lock;
1047 >            final SequenceLock lock = this.lock;
1048              lock.lock();
1049              try {
1050                  if (minCapacity - array.length > 0)
# Line 1057 | Line 1057 | public class ReadMostlyVector<E> impleme
1057  
1058      /** See {@link Vector#elements} */
1059      public Enumeration<E> elements() {
1060 <        return new Itr(this, 0);
1060 >        return new Itr<E>(this, 0);
1061      }
1062  
1063      /** See {@link Vector#capacity} */
1064      public int capacity() {
1065        long ignore = lock.getSequence();
1065          return array.length;
1066      }
1067  
# Line 1103 | Line 1102 | public class ReadMostlyVector<E> impleme
1102  
1103      // other methods
1104  
1105 <    public Object clone() {
1106 <        SequenceLock lock = this.lock;
1105 >    public ReadMostlyVector<E> clone() {
1106 >        final SequenceLock lock = this.lock;
1107          Object[] a = null;
1108          boolean retry = false;
1109          long seq = lock.awaitAvailability();
# Line 1123 | Line 1122 | public class ReadMostlyVector<E> impleme
1122                  lock.unlock();
1123              }
1124          }
1125 <        return new ReadMostlyVector(a, n, capacityIncrement);
1125 >        return new ReadMostlyVector<E>(a, n, capacityIncrement);
1126      }
1127  
1128      private void writeObject(java.io.ObjectOutputStream s)
1129              throws java.io.IOException {
1130 <        SequenceLock lock = this.lock;
1130 >        final SequenceLock lock = this.lock;
1131          lock.lock();
1132          try {
1133              s.defaultWriteObject();
# Line 1137 | Line 1136 | public class ReadMostlyVector<E> impleme
1136          }
1137      }
1138  
1139 <    static final class Itr<E> implements ListIterator<E>, Enumeration<E>  {
1139 >    static final class Itr<E> implements ListIterator<E>, Enumeration<E> {
1140          final ReadMostlyVector<E> list;
1141          final SequenceLock lock;
1142          Object[] items;
1143 <        Object next, prev;
1143 >        E next, prev;
1144          long seq;
1145          int cursor;
1146          int fence;
# Line 1167 | Line 1166 | public class ReadMostlyVector<E> impleme
1166                       lock.getSequence() != seq);
1167          }
1168  
1169 +        @SuppressWarnings("unchecked")
1170          public boolean hasNext() {
1171              boolean valid;
1172              int i = cursor;
# Line 1175 | Line 1175 | public class ReadMostlyVector<E> impleme
1175                      valid = false;
1176                      break;
1177                  }
1178 <                next = items[i];
1178 >                next = (E) items[i];
1179                  if (lock.getSequence() == seq) {
1180                      valid = true;
1181                      break;
# Line 1185 | Line 1185 | public class ReadMostlyVector<E> impleme
1185              return validNext = valid;
1186          }
1187  
1188 +        @SuppressWarnings("unchecked")
1189          public boolean hasPrevious() {
1190              boolean valid;
1191              int i = cursor - 1;
# Line 1193 | Line 1194 | public class ReadMostlyVector<E> impleme
1194                      valid = false;
1195                      break;
1196                  }
1197 <                prev = items[i];
1197 >                prev = (E) items[i];
1198                  if (lock.getSequence() == seq) {
1199                      valid = true;
1200                      break;
# Line 1207 | Line 1208 | public class ReadMostlyVector<E> impleme
1208              if (validNext || hasNext()) {
1209                  validNext = false;
1210                  lastRet = cursor++;
1211 <                return (E) next;
1211 >                return next;
1212              }
1213              throw new NoSuchElementException();
1214          }
# Line 1216 | Line 1217 | public class ReadMostlyVector<E> impleme
1217              if (validPrev || hasPrevious()) {
1218                  validPrev = false;
1219                  lastRet = cursor--;
1220 <                return (E) prev;
1220 >                return prev;
1221              }
1222              throw new NoSuchElementException();
1223          }
# Line 1273 | Line 1274 | public class ReadMostlyVector<E> impleme
1274          public int previousIndex() { return cursor - 1; }
1275      }
1276  
1277 <    static final class ReadMostlyVectorSublist<E> implements List<E>, RandomAccess, java.io.Serializable {
1277 >    static final class ReadMostlyVectorSublist<E>
1278 >            implements List<E>, RandomAccess, java.io.Serializable {
1279 >        static final long serialVersionUID = 3041673470172026059L;
1280 >
1281          final ReadMostlyVector<E> list;
1282          final int offset;
1283          volatile int size;
1284  
1285 <        ReadMostlyVectorSublist(ReadMostlyVector<E> list, int offset, int size) {
1285 >        ReadMostlyVectorSublist(ReadMostlyVector<E> list,
1286 >                                int offset, int size) {
1287              this.list = list;
1288              this.offset = offset;
1289              this.size = size;
# Line 1290 | Line 1295 | public class ReadMostlyVector<E> impleme
1295          }
1296  
1297          public boolean add(E element) {
1298 <            SequenceLock lock = list.lock;
1298 >            final SequenceLock lock = list.lock;
1299              lock.lock();
1300              try {
1301                  int c = size;
# Line 1303 | Line 1308 | public class ReadMostlyVector<E> impleme
1308          }
1309  
1310          public void add(int index, E element) {
1311 <            SequenceLock lock = list.lock;
1311 >            final SequenceLock lock = list.lock;
1312              lock.lock();
1313              try {
1314                  if (index < 0 || index > size)
# Line 1317 | Line 1322 | public class ReadMostlyVector<E> impleme
1322  
1323          public boolean addAll(Collection<? extends E> c) {
1324              Object[] elements = c.toArray();
1325 <            int added;
1321 <            SequenceLock lock = list.lock;
1325 >            final SequenceLock lock = list.lock;
1326              lock.lock();
1327              try {
1328                  int s = size;
1329                  int pc = list.count;
1330                  list.rawAddAllAt(offset + s, elements);
1331 <                added = list.count - pc;
1331 >                int added = list.count - pc;
1332                  size = s + added;
1333 +                return added != 0;
1334              } finally {
1335                  lock.unlock();
1336              }
1332            return added != 0;
1337          }
1338  
1339          public boolean addAll(int index, Collection<? extends E> c) {
1340              Object[] elements = c.toArray();
1341 <            int added;
1338 <            SequenceLock lock = list.lock;
1341 >            final SequenceLock lock = list.lock;
1342              lock.lock();
1343              try {
1344                  int s = size;
# Line 1343 | Line 1346 | public class ReadMostlyVector<E> impleme
1346                      throw new ArrayIndexOutOfBoundsException(index);
1347                  int pc = list.count;
1348                  list.rawAddAllAt(index + offset, elements);
1349 <                added = list.count - pc;
1349 >                int added = list.count - pc;
1350                  size = s + added;
1351 +                return added != 0;
1352              } finally {
1353                  lock.unlock();
1354              }
1351            return added != 0;
1355          }
1356  
1357          public void clear() {
1358 <            SequenceLock lock = list.lock;
1358 >            final SequenceLock lock = list.lock;
1359              lock.lock();
1360              try {
1361                  list.internalClear(offset, offset + size);
# Line 1389 | Line 1392 | public class ReadMostlyVector<E> impleme
1392          }
1393  
1394          public int indexOf(Object o) {
1395 <            SequenceLock lock = list.lock;
1395 >            final SequenceLock lock = list.lock;
1396              long seq = lock.awaitAvailability();
1397              Object[] items = list.array;
1398              int c = list.count;
# Line 1413 | Line 1416 | public class ReadMostlyVector<E> impleme
1416          }
1417  
1418          public Iterator<E> iterator() {
1419 <            return new SubItr(this, offset);
1419 >            return new SubItr<E>(this, offset);
1420          }
1421  
1422          public int lastIndexOf(Object o) {
1423 <            SequenceLock lock = list.lock;
1423 >            final SequenceLock lock = list.lock;
1424              long seq = lock.awaitAvailability();
1425              Object[] items = list.array;
1426              int c = list.count;
# Line 1437 | Line 1440 | public class ReadMostlyVector<E> impleme
1440          }
1441  
1442          public ListIterator<E> listIterator() {
1443 <            return new SubItr(this, offset);
1443 >            return new SubItr<E>(this, offset);
1444          }
1445  
1446          public ListIterator<E> listIterator(int index) {
1447 <            return new SubItr(this, index + offset);
1447 >            return new SubItr<E>(this, index + offset);
1448          }
1449  
1450          public E remove(int index) {
1451 <            Object result;
1449 <            SequenceLock lock = list.lock;
1451 >            final SequenceLock lock = list.lock;
1452              lock.lock();
1453              try {
1454                  Object[] items = list.array;
1455                  int i = index + offset;
1456                  if (index < 0 || index >= size || i >= items.length)
1457                      throw new ArrayIndexOutOfBoundsException(index);
1458 <                result = items[i];
1458 >                @SuppressWarnings("unchecked")
1459 >                E result = (E) items[i];
1460                  list.rawRemoveAt(i);
1461                  size--;
1462 +                return result;
1463              } finally {
1464                  lock.unlock();
1465              }
1462            return (E)result;
1466          }
1467  
1468          public boolean remove(Object o) {
1469 <            boolean removed = false;
1467 <            SequenceLock lock = list.lock;
1469 >            final SequenceLock lock = list.lock;
1470              lock.lock();
1471              try {
1472                  if (list.rawRemoveAt(list.rawIndexOf(o, offset,
1473                                                       offset + size))) {
1472                    removed = true;
1474                      --size;
1475 +                    return true;
1476                  }
1477 +                else
1478 +                    return false;
1479              } finally {
1480                  lock.unlock();
1481              }
1478            return removed;
1482          }
1483  
1484          public boolean removeAll(Collection<?> c) {
# Line 1501 | Line 1504 | public class ReadMostlyVector<E> impleme
1504              int ssize = toIndex - fromIndex;
1505              if (fromIndex < 0 || toIndex > c || ssize < 0)
1506                  throw new IndexOutOfBoundsException();
1507 <            return new ReadMostlyVectorSublist(list, offset+fromIndex, ssize);
1507 >            return new ReadMostlyVectorSublist<E>(list, offset+fromIndex, ssize);
1508          }
1509  
1510          public Object[] toArray() {
# Line 1523 | Line 1526 | public class ReadMostlyVector<E> impleme
1526          final ReadMostlyVector<E> list;
1527          final SequenceLock lock;
1528          Object[] items;
1529 <        Object next, prev;
1529 >        E next, prev;
1530          long seq;
1531          int cursor;
1532          int fence;
# Line 1554 | Line 1557 | public class ReadMostlyVector<E> impleme
1557              } while (lock.getSequence() != seq);
1558          }
1559  
1560 +        @SuppressWarnings("unchecked")
1561          public boolean hasNext() {
1562              boolean valid;
1563              int i = cursor;
# Line 1562 | Line 1566 | public class ReadMostlyVector<E> impleme
1566                      valid = false;
1567                      break;
1568                  }
1569 <                next = items[i];
1569 >                next = (E) items[i];
1570                  if (lock.getSequence() == seq) {
1571                      valid = true;
1572                      break;
# Line 1572 | Line 1576 | public class ReadMostlyVector<E> impleme
1576              return validNext = valid;
1577          }
1578  
1579 +        @SuppressWarnings("unchecked")
1580          public boolean hasPrevious() {
1581              boolean valid;
1582              int i = cursor - 1;
# Line 1580 | Line 1585 | public class ReadMostlyVector<E> impleme
1585                      valid = false;
1586                      break;
1587                  }
1588 <                prev = items[i];
1588 >                prev = (E) items[i];
1589                  if (lock.getSequence() == seq) {
1590                      valid = true;
1591                      break;
# Line 1594 | Line 1599 | public class ReadMostlyVector<E> impleme
1599              if (validNext || hasNext()) {
1600                  validNext = false;
1601                  lastRet = cursor++;
1602 <                return (E) next;
1602 >                return next;
1603              }
1604              throw new NoSuchElementException();
1605          }
# Line 1603 | Line 1608 | public class ReadMostlyVector<E> impleme
1608              if (validPrev || hasPrevious()) {
1609                  validPrev = false;
1610                  lastRet = cursor--;
1611 <                return (E) prev;
1611 >                return prev;
1612              }
1613              throw new NoSuchElementException();
1614          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines