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.44 by jsr166, Wed Nov 16 00:17:25 2016 UTC vs.
Revision 1.48 by jsr166, Thu Dec 8 05:02:32 2016 UTC

# Line 578 | Line 578 | public class ArrayList<E> extends Abstra
578       */
579      public void clear() {
580          modCount++;
581 <        Arrays.fill(elementData, 0, size, null);
582 <        size = 0;
581 >        final Object[] es = elementData;
582 >        for (int to = size, i = size = 0; i < to; i++)
583 >            es[i] = null;
584      }
585  
586      /**
# Line 669 | Line 670 | public class ArrayList<E> extends Abstra
670                      outOfBoundsMsg(fromIndex, toIndex));
671          }
672          modCount++;
673 <        final Object[] es = elementData;
673 <        final int oldSize = size;
674 <        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
675 <        Arrays.fill(es, size -= (toIndex - fromIndex), oldSize, null);
673 >        shiftTailOverGap(elementData, fromIndex, toIndex);
674          // checkInvariants();
675      }
676  
677 +    /** Erases the gap from lo to hi, by sliding down following elements. */
678 +    private void shiftTailOverGap(Object[] es, int lo, int hi) {
679 +        System.arraycopy(es, hi, es, lo, size - hi);
680 +        for (int to = size, i = (size -= hi - lo); i < to; i++)
681 +            es[i] = null;
682 +    }
683 +
684      /**
685       * A version of rangeCheck used by add and addAll.
686       */
# Line 761 | Line 766 | public class ArrayList<E> extends Abstra
766                  w += end - r;
767                  throw ex;
768              } finally {
769 <                final int oldSize = size, deleted = end - w;
770 <                modCount += deleted;
766 <                System.arraycopy(es, end, es, w, oldSize - end);
767 <                Arrays.fill(es, size -= deleted, oldSize, null);
769 >                modCount += end - w;
770 >                shiftTailOverGap(es, w, end);
771              }
772          }
773          // checkInvariants();
# Line 772 | Line 775 | public class ArrayList<E> extends Abstra
775      }
776  
777      /**
778 <     * Save the state of the {@code ArrayList} instance to a stream (that
779 <     * is, serialize it).
778 >     * Saves the state of the {@code ArrayList} instance to a stream
779 >     * (that is, serializes it).
780       *
781 +     * @param s the stream
782 +     * @throws java.io.IOException if an I/O error occurs
783       * @serialData The length of the array backing the {@code ArrayList}
784       *             instance is emitted (int), followed by all of its elements
785       *             (each an {@code Object}) in the proper order.
786       */
787      private void writeObject(java.io.ObjectOutputStream s)
788 <        throws java.io.IOException{
788 >        throws java.io.IOException {
789          // Write out element count, and any hidden stuff
790          int expectedModCount = modCount;
791          s.defaultWriteObject();
# Line 799 | Line 804 | public class ArrayList<E> extends Abstra
804      }
805  
806      /**
807 <     * Reconstitute the {@code ArrayList} instance from a stream (that is,
808 <     * deserialize it).
807 >     * Reconstitutes the {@code ArrayList} instance from a stream (that is,
808 >     * deserializes it).
809 >     * @param s the stream
810 >     * @throws ClassNotFoundException if the class of a serialized object
811 >     *         could not be found
812 >     * @throws java.io.IOException if an I/O error occurs
813       */
814      private void readObject(java.io.ObjectInputStream s)
815          throws java.io.IOException, ClassNotFoundException {
# Line 1291 | Line 1300 | public class ArrayList<E> extends Abstra
1300          public Spliterator<E> spliterator() {
1301              checkForComodification();
1302  
1303 <            // ArrayListSpliterator is not used because late-binding logic
1304 <            // is different here
1296 <            return new Spliterator<>() {
1303 >            // ArrayListSpliterator not used here due to late-binding
1304 >            return new Spliterator<E>() {
1305                  private int index = offset; // current index, modified on advance/split
1306                  private int fence = -1; // -1 until used; then one past last index
1307                  private int expectedModCount; // initialized when fence set
# Line 1307 | Line 1315 | public class ArrayList<E> extends Abstra
1315                      return hi;
1316                  }
1317  
1318 <                public ArrayListSpliterator<E> trySplit() {
1318 >                public ArrayList<E>.ArrayListSpliterator trySplit() {
1319                      int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1320 <                    // ArrayListSpliterator could be used here as the source is already bound
1320 >                    // ArrayListSpliterator can be used here as the source is already bound
1321                      return (lo >= mid) ? null : // divide range in half unless too small
1322 <                        new ArrayListSpliterator<>(root, lo, index = mid,
1315 <                                                   expectedModCount);
1322 >                        root.new ArrayListSpliterator(lo, index = mid, expectedModCount);
1323                  }
1324  
1325                  public boolean tryAdvance(Consumer<? super E> action) {
# Line 1354 | Line 1361 | public class ArrayList<E> extends Abstra
1361                  }
1362  
1363                  public long estimateSize() {
1364 <                    return (long) (getFence() - index);
1364 >                    return getFence() - index;
1365                  }
1366  
1367                  public int characteristics() {
# Line 1364 | Line 1371 | public class ArrayList<E> extends Abstra
1371          }
1372      }
1373  
1374 +    /**
1375 +     * @throws NullPointerException {@inheritDoc}
1376 +     */
1377      @Override
1378      public void forEach(Consumer<? super E> action) {
1379          Objects.requireNonNull(action);
# Line 1391 | Line 1401 | public class ArrayList<E> extends Abstra
1401       */
1402      @Override
1403      public Spliterator<E> spliterator() {
1404 <        return new ArrayListSpliterator<>(this, 0, -1, 0);
1404 >        return new ArrayListSpliterator(0, -1, 0);
1405      }
1406  
1407      /** Index-based split-by-two, lazily initialized Spliterator */
1408 <    static final class ArrayListSpliterator<E> implements Spliterator<E> {
1408 >    final class ArrayListSpliterator implements Spliterator<E> {
1409  
1410          /*
1411           * If ArrayLists were immutable, or structurally immutable (no
# Line 1429 | Line 1439 | public class ArrayList<E> extends Abstra
1439           * these streamlinings.
1440           */
1441  
1432        private final ArrayList<E> list;
1442          private int index; // current index, modified on advance/split
1443          private int fence; // -1 until used; then one past last index
1444          private int expectedModCount; // initialized when fence set
1445  
1446          /** Create new spliterator covering the given range */
1447 <        ArrayListSpliterator(ArrayList<E> list, int origin, int fence,
1439 <                             int expectedModCount) {
1440 <            this.list = list; // OK if null unless traversed
1447 >        ArrayListSpliterator(int origin, int fence, int expectedModCount) {
1448              this.index = origin;
1449              this.fence = fence;
1450              this.expectedModCount = expectedModCount;
# Line 1445 | Line 1452 | public class ArrayList<E> extends Abstra
1452  
1453          private int getFence() { // initialize fence to size on first use
1454              int hi; // (a specialized variant appears in method forEach)
1448            ArrayList<E> lst;
1455              if ((hi = fence) < 0) {
1456 <                if ((lst = list) == null)
1457 <                    hi = fence = 0;
1452 <                else {
1453 <                    expectedModCount = lst.modCount;
1454 <                    hi = fence = lst.size;
1455 <                }
1456 >                expectedModCount = modCount;
1457 >                hi = fence = size;
1458              }
1459              return hi;
1460          }
1461  
1462 <        public ArrayListSpliterator<E> trySplit() {
1462 >        public ArrayListSpliterator trySplit() {
1463              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1464              return (lo >= mid) ? null : // divide range in half unless too small
1465 <                new ArrayListSpliterator<>(list, lo, index = mid,
1464 <                                           expectedModCount);
1465 >                new ArrayListSpliterator(lo, index = mid, expectedModCount);
1466          }
1467  
1468          public boolean tryAdvance(Consumer<? super E> action) {
# Line 1470 | Line 1471 | public class ArrayList<E> extends Abstra
1471              int hi = getFence(), i = index;
1472              if (i < hi) {
1473                  index = i + 1;
1474 <                @SuppressWarnings("unchecked") E e = (E)list.elementData[i];
1474 >                @SuppressWarnings("unchecked") E e = (E)elementData[i];
1475                  action.accept(e);
1476 <                if (list.modCount != expectedModCount)
1476 >                if (modCount != expectedModCount)
1477                      throw new ConcurrentModificationException();
1478                  return true;
1479              }
# Line 1481 | Line 1482 | public class ArrayList<E> extends Abstra
1482  
1483          public void forEachRemaining(Consumer<? super E> action) {
1484              int i, hi, mc; // hoist accesses and checks from loop
1485 <            ArrayList<E> lst; Object[] a;
1485 >            Object[] a;
1486              if (action == null)
1487                  throw new NullPointerException();
1488 <            if ((lst = list) != null && (a = lst.elementData) != null) {
1488 >            if ((a = elementData) != null) {
1489                  if ((hi = fence) < 0) {
1490 <                    mc = lst.modCount;
1491 <                    hi = lst.size;
1490 >                    mc = modCount;
1491 >                    hi = size;
1492                  }
1493                  else
1494                      mc = expectedModCount;
# Line 1496 | Line 1497 | public class ArrayList<E> extends Abstra
1497                          @SuppressWarnings("unchecked") E e = (E) a[i];
1498                          action.accept(e);
1499                      }
1500 <                    if (lst.modCount == mc)
1500 >                    if (modCount == mc)
1501                          return;
1502                  }
1503              }
# Line 1504 | Line 1505 | public class ArrayList<E> extends Abstra
1505          }
1506  
1507          public long estimateSize() {
1508 <            return (long) (getFence() - index);
1508 >            return getFence() - index;
1509          }
1510  
1511          public int characteristics() {
# Line 1524 | Line 1525 | public class ArrayList<E> extends Abstra
1525          return (bits[i >> 6] & (1L << i)) == 0;
1526      }
1527  
1528 +    /**
1529 +     * @throws NullPointerException {@inheritDoc}
1530 +     */
1531      @Override
1532      public boolean removeIf(Predicate<? super E> filter) {
1533          return removeIf(filter, 0, size);
# Line 1558 | Line 1562 | public class ArrayList<E> extends Abstra
1562              for (i = beg; i < end; i++)
1563                  if (isClear(deathRow, i - beg))
1564                      es[w++] = es[i];
1565 <            final int oldSize = size;
1562 <            System.arraycopy(es, end, es, w, oldSize - end);
1563 <            Arrays.fill(es, size -= (end - w), oldSize, null);
1565 >            shiftTailOverGap(es, w, end);
1566              // checkInvariants();
1567              return true;
1568          } else {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines