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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines