ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayList.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayList.java (file contents):
Revision 1.36 by jsr166, Mon Oct 31 23:02:42 2016 UTC vs.
Revision 1.38 by jsr166, Sat Nov 12 20:51:59 2016 UTC

# Line 716 | Line 716 | public class ArrayList<E> extends Abstra
716       * @see Collection#contains(Object)
717       */
718      public boolean removeAll(Collection<?> c) {
719        Objects.requireNonNull(c);
719          return batchRemove(c, false);
720      }
721  
# Line 737 | Line 736 | public class ArrayList<E> extends Abstra
736       * @see Collection#contains(Object)
737       */
738      public boolean retainAll(Collection<?> c) {
740        Objects.requireNonNull(c);
739          return batchRemove(c, true);
740      }
741  
742      private boolean batchRemove(Collection<?> c, boolean complement) {
743 <        final Object[] elementData = this.elementData;
744 <        int r = 0, w = 0;
745 <        boolean modified = false;
746 <        try {
747 <            for (; r < size; r++)
748 <                if (c.contains(elementData[r]) == complement)
749 <                    elementData[w++] = elementData[r];
750 <        } finally {
751 <            // Preserve behavioral compatibility with AbstractCollection,
752 <            // even if c.contains() throws.
753 <            if (r != size) {
754 <                System.arraycopy(elementData, r,
755 <                                 elementData, w,
756 <                                 size - r);
743 >        Objects.requireNonNull(c);
744 >        final Object[] es = elementData;
745 >        final int size = this.size;
746 >        final boolean modified;
747 >        int r;
748 >        // Optimize for initial run of survivors
749 >        for (r = 0; r < size && c.contains(es[r]) == complement; r++)
750 >            ;
751 >        if (modified = (r < size)) {
752 >            int w = r++;
753 >            try {
754 >                for (Object e; r < size; r++)
755 >                    if (c.contains(e = es[r]) == complement)
756 >                        es[w++] = e;
757 >            } catch (Throwable ex) {
758 >                // Preserve behavioral compatibility with AbstractCollection,
759 >                // even if c.contains() throws.
760 >                System.arraycopy(es, r, es, w, size - r);
761                  w += size - r;
762 <            }
763 <            if (w != size) {
762 <                // clear to let GC do its work
763 <                for (int i = w; i < size; i++)
764 <                    elementData[i] = null;
762 >                throw ex;
763 >            } finally {
764                  modCount += size - w;
765 <                size = w;
767 <                modified = true;
765 >                Arrays.fill(es, (this.size = w), size, null);
766              }
767          }
768          return modified;
# Line 1503 | Line 1501 | public class ArrayList<E> extends Abstra
1501          final int size = this.size;
1502          final boolean modified;
1503          int r;
1504 <        for (r = 0; r < size; r++)
1505 <            if (filter.test((E) es[r]))
1506 <                break;
1504 >        // Optimize for initial run of survivors
1505 >        for (r = 0; r < size && !filter.test((E) es[r]); r++)
1506 >            ;
1507          if (modified = (r < size)) {
1508              expectedModCount++;
1509              modCount++;
# Line 1514 | Line 1512 | public class ArrayList<E> extends Abstra
1512                  for (E e; r < size; r++)
1513                      if (!filter.test(e = (E) es[r]))
1514                          es[w++] = e;
1517                Arrays.fill(es, (this.size = w), size, null);
1515              } catch (Throwable ex) {
1516                  // copy remaining elements
1517                  System.arraycopy(es, r, es, w, size - r);
1518 <                Arrays.fill(es, (this.size = w + size - r), size, null);
1518 >                w += size - r;
1519                  throw ex;
1520 +            } finally {
1521 +                Arrays.fill(es, (this.size = w), size, null);
1522              }
1523          }
1524          if (modCount != expectedModCount)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines