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.47 by jsr166, Mon Dec 5 00:08:01 2016 UTC vs.
Revision 1.57 by jsr166, Wed Mar 28 02:50:41 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5   * This code is free software; you can redistribute it and/or modify it
# Line 28 | Line 28 | package java.util;
28   import java.util.function.Consumer;
29   import java.util.function.Predicate;
30   import java.util.function.UnaryOperator;
31 + import jdk.internal.misc.SharedSecrets;
32  
33   /**
34   * Resizable-array implementation of the {@code List} interface.  Implements
# Line 91 | Line 92 | import java.util.function.UnaryOperator;
92   * should be used only to detect bugs.</i>
93   *
94   * <p>This class is a member of the
95 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
95 > * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
96   * Java Collections Framework</a>.
97   *
98   * @param <E> the type of elements in this list
# Line 515 | Line 516 | public class ArrayList<E> extends Abstra
516       */
517      public E remove(int index) {
518          Objects.checkIndex(index, size);
519 +        final Object[] es = elementData;
520  
521 <        modCount++;
522 <        E oldValue = elementData(index);
521 <
522 <        int numMoved = size - index - 1;
523 <        if (numMoved > 0)
524 <            System.arraycopy(elementData, index+1, elementData, index,
525 <                             numMoved);
526 <        elementData[--size] = null; // clear to let GC do its work
521 >        @SuppressWarnings("unchecked") E oldValue = (E) es[index];
522 >        fastRemove(es, index);
523  
524          // checkInvariants();
525          return oldValue;
# Line 543 | Line 539 | public class ArrayList<E> extends Abstra
539       * @return {@code true} if this list contained the specified element
540       */
541      public boolean remove(Object o) {
542 <        if (o == null) {
543 <            for (int index = 0; index < size; index++)
544 <                if (elementData[index] == null) {
545 <                    fastRemove(index);
546 <                    return true;
547 <                }
548 <        } else {
549 <            for (int index = 0; index < size; index++)
550 <                if (o.equals(elementData[index])) {
551 <                    fastRemove(index);
552 <                    return true;
553 <                }
542 >        final Object[] es = elementData;
543 >        final int size = this.size;
544 >        int i = 0;
545 >        found: {
546 >            if (o == null) {
547 >                for (; i < size; i++)
548 >                    if (es[i] == null)
549 >                        break found;
550 >            } else {
551 >                for (; i < size; i++)
552 >                    if (o.equals(es[i]))
553 >                        break found;
554 >            }
555 >            return false;
556          }
557 <        return false;
557 >        fastRemove(es, i);
558 >        return true;
559      }
560  
561      /**
562       * Private remove method that skips bounds checking and does not
563       * return the value removed.
564       */
565 <    private void fastRemove(int index) {
565 >    private void fastRemove(Object[] es, int i) {
566          modCount++;
567 <        int numMoved = size - index - 1;
568 <        if (numMoved > 0)
569 <            System.arraycopy(elementData, index+1, elementData, index,
570 <                             numMoved);
572 <        elementData[--size] = null; // clear to let GC do its work
567 >        final int newSize;
568 >        if ((newSize = size - 1) > i)
569 >            System.arraycopy(es, i + 1, es, i, newSize - i);
570 >        es[size = newSize] = null;
571      }
572  
573      /**
# Line 748 | Line 746 | public class ArrayList<E> extends Abstra
746                          final int from, final int end) {
747          Objects.requireNonNull(c);
748          final Object[] es = elementData;
751        final boolean modified;
749          int r;
750          // Optimize for initial run of survivors
751 <        for (r = from; r < end && c.contains(es[r]) == complement; r++)
752 <            ;
753 <        if (modified = (r < end)) {
754 <            int w = r++;
755 <            try {
756 <                for (Object e; r < end; r++)
757 <                    if (c.contains(e = es[r]) == complement)
758 <                        es[w++] = e;
759 <            } catch (Throwable ex) {
760 <                // Preserve behavioral compatibility with AbstractCollection,
761 <                // even if c.contains() throws.
762 <                System.arraycopy(es, r, es, w, end - r);
763 <                w += end - r;
764 <                throw ex;
765 <            } finally {
766 <                modCount += end - w;
767 <                shiftTailOverGap(es, w, end);
768 <            }
751 >        for (r = from;; r++) {
752 >            if (r == end)
753 >                return false;
754 >            if (c.contains(es[r]) != complement)
755 >                break;
756 >        }
757 >        int w = r++;
758 >        try {
759 >            for (Object e; r < end; r++)
760 >                if (c.contains(e = es[r]) == complement)
761 >                    es[w++] = e;
762 >        } catch (Throwable ex) {
763 >            // Preserve behavioral compatibility with AbstractCollection,
764 >            // even if c.contains() throws.
765 >            System.arraycopy(es, r, es, w, end - r);
766 >            w += end - r;
767 >            throw ex;
768 >        } finally {
769 >            modCount += end - w;
770 >            shiftTailOverGap(es, w, end);
771          }
772          // checkInvariants();
773 <        return modified;
773 >        return true;
774      }
775  
776      /**
# Line 790 | Line 789 | public class ArrayList<E> extends Abstra
789          int expectedModCount = modCount;
790          s.defaultWriteObject();
791  
792 <        // Write out size as capacity for behavioural compatibility with clone()
792 >        // Write out size as capacity for behavioral compatibility with clone()
793          s.writeInt(size);
794  
795          // Write out all elements in the proper order.
# Line 822 | Line 821 | public class ArrayList<E> extends Abstra
821  
822          if (size > 0) {
823              // like clone(), allocate array based upon size not capacity
824 +            SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
825              Object[] elements = new Object[size];
826  
827              // Read in all elements in the proper order.
# Line 1149 | Line 1149 | public class ArrayList<E> extends Abstra
1149              return modified;
1150          }
1151  
1152 +        public Object[] toArray() {
1153 +            checkForComodification();
1154 +            return Arrays.copyOfRange(root.elementData, offset, offset + size);
1155 +        }
1156 +
1157 +        @SuppressWarnings("unchecked")
1158 +        public <T> T[] toArray(T[] a) {
1159 +            checkForComodification();
1160 +            if (a.length < size)
1161 +                return (T[]) Arrays.copyOfRange(
1162 +                        root.elementData, offset, offset + size, a.getClass());
1163 +            System.arraycopy(root.elementData, offset, a, 0, size);
1164 +            if (a.length > size)
1165 +                a[size] = null;
1166 +            return a;
1167 +        }
1168 +
1169          public Iterator<E> iterator() {
1170              return listIterator();
1171          }
# Line 1371 | Line 1388 | public class ArrayList<E> extends Abstra
1388          }
1389      }
1390  
1391 +    /**
1392 +     * @throws NullPointerException {@inheritDoc}
1393 +     */
1394      @Override
1395      public void forEach(Consumer<? super E> action) {
1396          Objects.requireNonNull(action);
# Line 1440 | Line 1460 | public class ArrayList<E> extends Abstra
1460          private int fence; // -1 until used; then one past last index
1461          private int expectedModCount; // initialized when fence set
1462  
1463 <        /** Create new spliterator covering the given range */
1463 >        /** Creates new spliterator covering the given range. */
1464          ArrayListSpliterator(int origin, int fence, int expectedModCount) {
1465              this.index = origin;
1466              this.fence = fence;
# Line 1522 | Line 1542 | public class ArrayList<E> extends Abstra
1542          return (bits[i >> 6] & (1L << i)) == 0;
1543      }
1544  
1545 +    /**
1546 +     * @throws NullPointerException {@inheritDoc}
1547 +     */
1548      @Override
1549      public boolean removeIf(Predicate<? super E> filter) {
1550          return removeIf(filter, 0, size);
# Line 1550 | Line 1573 | public class ArrayList<E> extends Abstra
1573                      setBit(deathRow, i - beg);
1574              if (modCount != expectedModCount)
1575                  throw new ConcurrentModificationException();
1553            expectedModCount++;
1576              modCount++;
1577              int w = beg;
1578              for (i = beg; i < end; i++)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines