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.52 by jsr166, Wed May 31 22:37:31 2017 UTC vs.
Revision 1.59 by jsr166, Sun May 6 01:14:25 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 745 | Line 746 | public class ArrayList<E> extends Abstra
746                          final int from, final int end) {
747          Objects.requireNonNull(c);
748          final Object[] es = elementData;
748        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 819 | 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 1119 | Line 1122 | public class ArrayList<E> extends Abstra
1122              return true;
1123          }
1124  
1125 +        public void replaceAll(UnaryOperator<E> operator) {
1126 +            root.replaceAllRange(operator, offset, offset + size);
1127 +        }
1128 +
1129          public boolean removeAll(Collection<?> c) {
1130              return batchRemove(c, false);
1131          }
# Line 1146 | Line 1153 | public class ArrayList<E> extends Abstra
1153              return modified;
1154          }
1155  
1156 +        public Object[] toArray() {
1157 +            checkForComodification();
1158 +            return Arrays.copyOfRange(root.elementData, offset, offset + size);
1159 +        }
1160 +
1161 +        @SuppressWarnings("unchecked")
1162 +        public <T> T[] toArray(T[] a) {
1163 +            checkForComodification();
1164 +            if (a.length < size)
1165 +                return (T[]) Arrays.copyOfRange(
1166 +                        root.elementData, offset, offset + size, a.getClass());
1167 +            System.arraycopy(root.elementData, offset, a, 0, size);
1168 +            if (a.length > size)
1169 +                a[size] = null;
1170 +            return a;
1171 +        }
1172 +
1173          public Iterator<E> iterator() {
1174              return listIterator();
1175          }
# Line 1553 | Line 1577 | public class ArrayList<E> extends Abstra
1577                      setBit(deathRow, i - beg);
1578              if (modCount != expectedModCount)
1579                  throw new ConcurrentModificationException();
1556            expectedModCount++;
1580              modCount++;
1581              int w = beg;
1582              for (i = beg; i < end; i++)
# Line 1572 | Line 1595 | public class ArrayList<E> extends Abstra
1595  
1596      @Override
1597      public void replaceAll(UnaryOperator<E> operator) {
1598 +        replaceAllRange(operator, 0, size);
1599 +    }
1600 +
1601 +    private void replaceAllRange(UnaryOperator<E> operator, int i, int end) {
1602          Objects.requireNonNull(operator);
1603          final int expectedModCount = modCount;
1604          final Object[] es = elementData;
1605 <        final int size = this.size;
1579 <        for (int i = 0; modCount == expectedModCount && i < size; i++)
1605 >        for (; modCount == expectedModCount && i < end; i++)
1606              es[i] = operator.apply(elementAt(es, i));
1607          if (modCount != expectedModCount)
1608              throw new ConcurrentModificationException();
1583        modCount++;
1609          // checkInvariants();
1610      }
1611  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines