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.37 by jsr166, Wed Nov 30 03:31:47 2016 UTC vs.
Revision 1.48 by jsr166, Wed Mar 28 02:50:41 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 1994, 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 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 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 1022 | Line 1027 | public class Vector<E>
1027                      setBit(deathRow, i - beg);
1028              if (modCount != expectedModCount)
1029                  throw new ConcurrentModificationException();
1025            expectedModCount++;
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 {
# 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 1408 | Line 1425 | public class Vector<E>
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 */
1428 >        /** Creates new spliterator covering the given range. */
1429          VectorSpliterator(Object[] array, int origin, int fence,
1430                            int expectedModCount) {
1431              this.array = array;
# Line 1437 | Line 1454 | public class Vector<E>
1454  
1455          @SuppressWarnings("unchecked")
1456          public boolean tryAdvance(Consumer<? super E> action) {
1457 +            Objects.requireNonNull(action);
1458              int i;
1441            if (action == null)
1442                throw new NullPointerException();
1459              if (getFence() > (i = index)) {
1460                  index = i + 1;
1461                  action.accept((E)array[i]);
# Line 1452 | 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 <            Object[] a;
1473 <            if (action == null)
1474 <                throw new NullPointerException();
1475 <            if ((hi = fence) < 0) {
1476 <                synchronized (Vector.this) {
1477 <                    expectedModCount = modCount;
1478 <                    a = array = elementData;
1463 <                    hi = fence = elementCount;
1464 <                }
1465 <            }
1466 <            else
1467 <                a = array;
1468 <            if (a != null && (i = index) >= 0 && (index = hi) <= a.length) {
1469 <                while (i < hi)
1470 <                    action.accept((E) a[i++]);
1471 <                if (modCount == expectedModCount)
1472 <                    return;
1473 <            }
1474 <            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() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines