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

Comparing jsr166/src/main/java/util/Vector.java (file contents):
Revision 1.36 by jsr166, Mon Nov 14 22:46:22 2016 UTC vs.
Revision 1.42 by jsr166, Wed Dec 21 05:14:01 2016 UTC

# Line 306 | Line 306 | public class Vector<E>
306          modCount++;
307          if (newSize > elementData.length)
308              grow(newSize);
309 <        for (int i = newSize; i < elementCount; i++)
310 <            elementData[i] = null;
311 <        elementCount = newSize;
309 >        final Object[] es = elementData;
310 >        for (int to = elementCount, i = elementCount = newSize; i < to; i++)
311 >            es[i] = null;
312      }
313  
314      /**
# Line 676 | Line 676 | public class Vector<E>
676       * method (which is part of the {@link List} interface).
677       */
678      public synchronized void removeAllElements() {
679 <        Arrays.fill(elementData, 0, elementCount, null);
679 >        final Object[] es = elementData;
680 >        for (int to = elementCount, i = elementCount = 0; i < to; i++)
681 >            es[i] = null;
682          modCount++;
681        elementCount = 0;
683      }
684  
685      /**
# Line 984 | Line 985 | public class Vector<E>
985          return bulkRemove(e -> !c.contains(e));
986      }
987  
988 +    /**
989 +     * @throws NullPointerException {@inheritDoc}
990 +     */
991      @Override
992      public boolean removeIf(Predicate<? super E> filter) {
993          Objects.requireNonNull(filter);
# Line 1028 | Line 1032 | public class Vector<E>
1032              for (i = beg; i < end; i++)
1033                  if (isClear(deathRow, i - beg))
1034                      es[w++] = es[i];
1035 <            Arrays.fill(es, elementCount = w, end, null);
1035 >            for (i = elementCount = w; i < end; i++)
1036 >                es[i] = null;
1037              // checkInvariants();
1038              return true;
1039          } else {
# Line 1159 | Line 1164 | public class Vector<E>
1164       * (If {@code toIndex==fromIndex}, this operation has no effect.)
1165       */
1166      protected synchronized void removeRange(int fromIndex, int toIndex) {
1162        final Object[] es = elementData;
1163        final int oldSize = elementCount;
1164        System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex);
1165
1167          modCount++;
1168 <        Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null);
1168 >        shiftTailOverGap(elementData, fromIndex, toIndex);
1169          // checkInvariants();
1170      }
1171  
1172 +    /** Erases the gap from lo to hi, by sliding down following elements. */
1173 +    private void shiftTailOverGap(Object[] es, int lo, int hi) {
1174 +        System.arraycopy(es, hi, es, lo, elementCount - hi);
1175 +        for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++)
1176 +            es[i] = null;
1177 +    }
1178 +
1179      /**
1180 <     * Save the state of the {@code Vector} instance to a stream (that
1181 <     * is, serialize it).
1180 >     * Saves the state of the {@code Vector} instance to a stream
1181 >     * (that is, serializes it).
1182       * This method performs synchronization to ensure the consistency
1183       * of the serialized data.
1184 +     *
1185 +     * @param s the stream
1186 +     * @throws java.io.IOException if an I/O error occurs
1187       */
1188      private void writeObject(java.io.ObjectOutputStream s)
1189              throws java.io.IOException {
# Line 1345 | Line 1356 | public class Vector<E>
1356          }
1357      }
1358  
1359 +    /**
1360 +     * @throws NullPointerException {@inheritDoc}
1361 +     */
1362      @Override
1363      public synchronized void forEach(Consumer<? super E> action) {
1364          Objects.requireNonNull(action);
# Line 1358 | Line 1372 | public class Vector<E>
1372          // checkInvariants();
1373      }
1374  
1375 +    /**
1376 +     * @throws NullPointerException {@inheritDoc}
1377 +     */
1378      @Override
1379      public synchronized void replaceAll(UnaryOperator<E> operator) {
1380          Objects.requireNonNull(operator);
# Line 1398 | Line 1415 | public class Vector<E>
1415       */
1416      @Override
1417      public Spliterator<E> spliterator() {
1418 <        return new VectorSpliterator<>(this, null, 0, -1, 0);
1418 >        return new VectorSpliterator(null, 0, -1, 0);
1419      }
1420  
1421      /** Similar to ArrayList Spliterator */
1422 <    static final class VectorSpliterator<E> implements Spliterator<E> {
1406 <        private final Vector<E> list;
1422 >    final class VectorSpliterator implements Spliterator<E> {
1423          private Object[] array;
1424          private int index; // current index, modified on advance/split
1425          private int fence; // -1 until used; then one past last index
1426          private int expectedModCount; // initialized when fence set
1427  
1428 <        /** Create new spliterator covering the given range */
1429 <        VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1428 >        /** Creates new spliterator covering the given range */
1429 >        VectorSpliterator(Object[] array, int origin, int fence,
1430                            int expectedModCount) {
1415            this.list = list;
1431              this.array = array;
1432              this.index = origin;
1433              this.fence = fence;
# Line 1422 | Line 1437 | public class Vector<E>
1437          private int getFence() { // initialize on first use
1438              int hi;
1439              if ((hi = fence) < 0) {
1440 <                synchronized (list) {
1441 <                    array = list.elementData;
1442 <                    expectedModCount = list.modCount;
1443 <                    hi = fence = list.elementCount;
1440 >                synchronized (Vector.this) {
1441 >                    array = elementData;
1442 >                    expectedModCount = modCount;
1443 >                    hi = fence = elementCount;
1444                  }
1445              }
1446              return hi;
# Line 1434 | Line 1449 | public class Vector<E>
1449          public Spliterator<E> trySplit() {
1450              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1451              return (lo >= mid) ? null :
1452 <                new VectorSpliterator<>(list, array, lo, index = mid,
1438 <                                        expectedModCount);
1452 >                new VectorSpliterator(array, lo, index = mid, expectedModCount);
1453          }
1454  
1455          @SuppressWarnings("unchecked")
# Line 1446 | Line 1460 | public class Vector<E>
1460              if (getFence() > (i = index)) {
1461                  index = i + 1;
1462                  action.accept((E)array[i]);
1463 <                if (list.modCount != expectedModCount)
1463 >                if (modCount != expectedModCount)
1464                      throw new ConcurrentModificationException();
1465                  return true;
1466              }
# Line 1455 | Line 1469 | public class Vector<E>
1469  
1470          @SuppressWarnings("unchecked")
1471          public void forEachRemaining(Consumer<? super E> action) {
1458            int i, hi; // hoist accesses and checks from loop
1459            Vector<E> lst; Object[] a;
1472              if (action == null)
1473                  throw new NullPointerException();
1474 <            if ((lst = list) != null) {
1475 <                if ((hi = fence) < 0) {
1476 <                    synchronized (lst) {
1477 <                        expectedModCount = lst.modCount;
1478 <                        a = array = lst.elementData;
1479 <                        hi = fence = lst.elementCount;
1480 <                    }
1469 <                }
1470 <                else
1471 <                    a = array;
1472 <                if (a != null && (i = index) >= 0 && (index = hi) <= a.length) {
1473 <                    while (i < hi)
1474 <                        action.accept((E) a[i++]);
1475 <                    if (lst.modCount == expectedModCount)
1476 <                        return;
1477 <                }
1478 <            }
1479 <            throw new ConcurrentModificationException();
1474 >            final int hi = getFence();
1475 >            final Object[] a = array;
1476 >            int i;
1477 >            for (i = index, index = hi; i < hi; i++)
1478 >                action.accept((E) a[i]);
1479 >            if (modCount != expectedModCount)
1480 >                throw new ConcurrentModificationException();
1481          }
1482  
1483          public long estimateSize() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines