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.34 by jsr166, Sun Nov 13 19:58:47 2016 UTC vs.
Revision 1.47 by jsr166, Mon Jan 8 20:57:44 2018 UTC

# Line 70 | Line 70 | import java.util.function.UnaryOperator;
70   *
71   * <p>As of the Java 2 platform v1.2, this class was retrofitted to
72   * implement the {@link List} interface, making it a member of the
73 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
73 > * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
74   * Java Collections Framework</a>.  Unlike the new collection
75   * implementations, {@code Vector} is synchronized.  If a thread-safe
76   * implementation is not needed, it is recommended to use {@link
# 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 585 | Line 586 | public class Vector<E>
586          modCount++;
587          elementCount--;
588          elementData[elementCount] = null; /* to let gc do its work */
589 +        // checkInvariants();
590      }
591  
592      /**
# Line 675 | Line 677 | public class Vector<E>
677       * method (which is part of the {@link List} interface).
678       */
679      public synchronized void removeAllElements() {
680 <        // Let gc do its work
681 <        for (int i = 0; i < elementCount; i++)
682 <            elementData[i] = null;
681 <
680 >        final Object[] es = elementData;
681 >        for (int to = elementCount, i = elementCount = 0; i < to; i++)
682 >            es[i] = null;
683          modCount++;
683        elementCount = 0;
684      }
685  
686      /**
# Line 810 | Line 810 | public class Vector<E>
810              elementData = grow();
811          elementData[s] = e;
812          elementCount = s + 1;
813 +        // checkInvariants();
814      }
815  
816      /**
# Line 878 | Line 879 | public class Vector<E>
879                               numMoved);
880          elementData[--elementCount] = null; // Let gc do its work
881  
882 +        // checkInvariants();
883          return oldValue;
884      }
885  
# Line 933 | Line 935 | public class Vector<E>
935                  elementData = grow(s + numNew);
936              System.arraycopy(a, 0, elementData, s, numNew);
937              elementCount = s + numNew;
938 +            // checkInvariants();
939              return true;
940          }
941      }
# Line 983 | 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 1005 | Line 1011 | public class Vector<E>
1011          int expectedModCount = modCount;
1012          final Object[] es = elementData;
1013          final int end = elementCount;
1008        final boolean modified;
1014          int i;
1015          // Optimize for initial run of survivors
1016          for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
# Line 1013 | Line 1018 | public class Vector<E>
1018          // Tolerate predicates that reentrantly access the collection for
1019          // read (but writers still get CME), so traverse once to find
1020          // elements to delete, a second pass to physically expunge.
1021 <        if (modified = (i < end)) {
1017 <            expectedModCount++;
1018 <            modCount++;
1021 >        if (i < end) {
1022              final int beg = i;
1023              final long[] deathRow = nBits(end - beg);
1024              deathRow[0] = 1L;   // set bit 0
1025              for (i = beg + 1; i < end; i++)
1026                  if (filter.test(elementAt(es, i)))
1027                      setBit(deathRow, i - beg);
1028 +            if (modCount != expectedModCount)
1029 +                throw new ConcurrentModificationException();
1030 +            modCount++;
1031              int w = beg;
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 {
1040 >            if (modCount != expectedModCount)
1041 >                throw new ConcurrentModificationException();
1042 >            // checkInvariants();
1043 >            return false;
1044          }
1031        if (modCount != expectedModCount)
1032            throw new ConcurrentModificationException();
1033        return modified;
1045      }
1046  
1047      /**
# Line 1071 | Line 1082 | public class Vector<E>
1082                               numMoved);
1083          System.arraycopy(a, 0, elementData, index, numNew);
1084          elementCount = s + numNew;
1085 +        // checkInvariants();
1086          return true;
1087      }
1088  
# Line 1152 | 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) {
1155        int numMoved = elementCount - toIndex;
1156        System.arraycopy(elementData, toIndex, elementData, fromIndex,
1157                         numMoved);
1158
1159        // Let gc do its work
1167          modCount++;
1168 <        int newElementCount = elementCount - (toIndex-fromIndex);
1169 <        while (elementCount != newElementCount)
1170 <            elementData[--elementCount] = 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 1340 | 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 1350 | Line 1369 | public class Vector<E>
1369              action.accept(elementAt(es, i));
1370          if (modCount != expectedModCount)
1371              throw new ConcurrentModificationException();
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 1363 | Line 1386 | public class Vector<E>
1386          if (modCount != expectedModCount)
1387              throw new ConcurrentModificationException();
1388          modCount++;
1389 +        // checkInvariants();
1390      }
1391  
1392      @SuppressWarnings("unchecked")
# Line 1370 | Line 1394 | public class Vector<E>
1394      public synchronized void sort(Comparator<? super E> c) {
1395          final int expectedModCount = modCount;
1396          Arrays.sort((E[]) elementData, 0, elementCount, c);
1397 <        if (modCount != expectedModCount) {
1397 >        if (modCount != expectedModCount)
1398              throw new ConcurrentModificationException();
1375        }
1399          modCount++;
1400 +        // checkInvariants();
1401      }
1402  
1403      /**
# Line 1391 | 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> {
1399 <        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) {
1408            this.list = list;
1431              this.array = array;
1432              this.index = origin;
1433              this.fence = fence;
# Line 1415 | 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 1427 | 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,
1431 <                                        expectedModCount);
1452 >                new VectorSpliterator(array, lo, index = mid, expectedModCount);
1453          }
1454  
1455          @SuppressWarnings("unchecked")
1456          public boolean tryAdvance(Consumer<? super E> action) {
1457 +            Objects.requireNonNull(action);
1458              int i;
1437            if (action == null)
1438                throw new NullPointerException();
1459              if (getFence() > (i = index)) {
1460                  index = i + 1;
1461                  action.accept((E)array[i]);
1462 <                if (list.modCount != expectedModCount)
1462 >                if (modCount != expectedModCount)
1463                      throw new ConcurrentModificationException();
1464                  return true;
1465              }
# Line 1448 | Line 1468 | public class Vector<E>
1468  
1469          @SuppressWarnings("unchecked")
1470          public void forEachRemaining(Consumer<? super E> action) {
1471 <            int i, hi; // hoist accesses and checks from loop
1472 <            Vector<E> lst; Object[] a;
1473 <            if (action == null)
1474 <                throw new NullPointerException();
1475 <            if ((lst = list) != null) {
1476 <                if ((hi = fence) < 0) {
1477 <                    synchronized (lst) {
1478 <                        expectedModCount = lst.modCount;
1459 <                        a = array = lst.elementData;
1460 <                        hi = fence = lst.elementCount;
1461 <                    }
1462 <                }
1463 <                else
1464 <                    a = array;
1465 <                if (a != null && (i = index) >= 0 && (index = hi) <= a.length) {
1466 <                    while (i < hi)
1467 <                        action.accept((E) a[i++]);
1468 <                    if (lst.modCount == expectedModCount)
1469 <                        return;
1470 <                }
1471 <            }
1472 <            throw new ConcurrentModificationException();
1471 >            Objects.requireNonNull(action);
1472 >            final int hi = getFence();
1473 >            final Object[] a = array;
1474 >            int i;
1475 >            for (i = index, index = hi; i < hi; i++)
1476 >                action.accept((E) a[i]);
1477 >            if (modCount != expectedModCount)
1478 >                throw new ConcurrentModificationException();
1479          }
1480  
1481          public long estimateSize() {
# Line 1480 | Line 1486 | public class Vector<E>
1486              return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1487          }
1488      }
1489 +
1490 +    void checkInvariants() {
1491 +        // assert elementCount >= 0;
1492 +        // assert elementCount == elementData.length || elementData[elementCount] == null;
1493 +    }
1494   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines