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.30 by jsr166, Thu Nov 3 20:49:07 2016 UTC vs.
Revision 1.31 by jsr166, Fri Nov 4 02:56:17 2016 UTC

# Line 513 | Line 513 | public class Vector<E>
513       * Returns the last component of the vector.
514       *
515       * @return  the last component of the vector, i.e., the component at index
516 <     *          <code>size()&nbsp;-&nbsp;1</code>.
516 >     *          {@code size() - 1}
517       * @throws NoSuchElementException if this vector is empty
518       */
519      public synchronized E lastElement() {
# Line 855 | Line 855 | public class Vector<E>
855       * Shifts any subsequent elements to the left (subtracts one from their
856       * indices).  Returns the element that was removed from the Vector.
857       *
858     * @throws ArrayIndexOutOfBoundsException if the index is out of range
859     *         ({@code index < 0 || index >= size()})
858       * @param index the index of the element to be removed
859       * @return element that was removed
860 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
861 +     *         ({@code index < 0 || index >= size()})
862       * @since 1.2
863       */
864      public synchronized E remove(int index) {
# Line 949 | Line 949 | public class Vector<E>
949       *         or if the specified collection is null
950       * @since 1.2
951       */
952 <    public synchronized boolean removeAll(Collection<?> c) {
953 <        return super.removeAll(c);
952 >    public boolean removeAll(Collection<?> c) {
953 >        Objects.requireNonNull(c);
954 >        return bulkRemove(e -> c.contains(e));
955      }
956  
957      /**
# Line 972 | Line 973 | public class Vector<E>
973       *         or if the specified collection is null
974       * @since 1.2
975       */
976 <    public synchronized boolean retainAll(Collection<?> c) {
977 <        return super.retainAll(c);
976 >    public boolean retainAll(Collection<?> c) {
977 >        Objects.requireNonNull(c);
978 >        return bulkRemove(e -> !c.contains(e));
979 >    }
980 >
981 >    @Override
982 >    public boolean removeIf(Predicate<? super E> filter) {
983 >        Objects.requireNonNull(filter);
984 >        return bulkRemove(filter);
985 >    }
986 >
987 >    @SuppressWarnings("unchecked")
988 >    private synchronized boolean bulkRemove(Predicate<? super E> filter) {
989 >        int expectedModCount = modCount;
990 >        final Object[] es = elementData;
991 >        final int size = elementCount;
992 >        final boolean modified;
993 >        int r;
994 >        // Optimize for initial run of survivors
995 >        for (r = 0; r < size; r++)
996 >            if (filter.test((E) es[r]))
997 >                break;
998 >        if (modified = (r < size)) {
999 >            expectedModCount++;
1000 >            modCount++;
1001 >            int w = r++;
1002 >            try {
1003 >                for (E e; r < size; r++)
1004 >                    if (!filter.test(e = (E) es[r]))
1005 >                        es[w++] = e;
1006 >            } catch (Throwable ex) {
1007 >                // copy remaining elements
1008 >                System.arraycopy(es, r, es, w, size - r);
1009 >                w += size - r;
1010 >                throw ex;
1011 >            } finally {
1012 >                Arrays.fill(es, elementCount = w, size, null);
1013 >            }
1014 >        }
1015 >        if (modCount != expectedModCount)
1016 >            throw new ConcurrentModificationException();
1017 >        return modified;
1018      }
1019  
1020      /**
# Line 1303 | Line 1344 | public class Vector<E>
1344  
1345      @Override
1346      @SuppressWarnings("unchecked")
1306    public synchronized boolean removeIf(Predicate<? super E> filter) {
1307        Objects.requireNonNull(filter);
1308        // figure out which elements are to be removed
1309        // any exception thrown from the filter predicate at this stage
1310        // will leave the collection unmodified
1311        int removeCount = 0;
1312        final int size = elementCount;
1313        final BitSet removeSet = new BitSet(size);
1314        final int expectedModCount = modCount;
1315        for (int i=0; modCount == expectedModCount && i < size; i++) {
1316            @SuppressWarnings("unchecked")
1317            final E element = (E) elementData[i];
1318            if (filter.test(element)) {
1319                removeSet.set(i);
1320                removeCount++;
1321            }
1322        }
1323        if (modCount != expectedModCount) {
1324            throw new ConcurrentModificationException();
1325        }
1326
1327        // shift surviving elements left over the spaces left by removed elements
1328        final boolean anyToRemove = removeCount > 0;
1329        if (anyToRemove) {
1330            final int newSize = size - removeCount;
1331            for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
1332                i = removeSet.nextClearBit(i);
1333                elementData[j] = elementData[i];
1334            }
1335            for (int k=newSize; k < size; k++) {
1336                elementData[k] = null;  // Let gc do its work
1337            }
1338            elementCount = newSize;
1339            if (modCount != expectedModCount) {
1340                throw new ConcurrentModificationException();
1341            }
1342            modCount++;
1343        }
1344
1345        return anyToRemove;
1346    }
1347
1348    @Override
1349    @SuppressWarnings("unchecked")
1347      public synchronized void replaceAll(UnaryOperator<E> operator) {
1348          Objects.requireNonNull(operator);
1349          final int expectedModCount = modCount;
# Line 1410 | Line 1407 | public class Vector<E>
1407          private int getFence() { // initialize on first use
1408              int hi;
1409              if ((hi = fence) < 0) {
1410 <                synchronized(list) {
1410 >                synchronized (list) {
1411                      array = list.elementData;
1412                      expectedModCount = list.modCount;
1413                      hi = fence = list.elementCount;
# Line 1449 | Line 1446 | public class Vector<E>
1446                  throw new NullPointerException();
1447              if ((lst = list) != null) {
1448                  if ((hi = fence) < 0) {
1449 <                    synchronized(lst) {
1449 >                    synchronized (lst) {
1450                          expectedModCount = lst.modCount;
1451                          a = array = lst.elementData;
1452                          hi = fence = lst.elementCount;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines