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.32 by jsr166, Sat Nov 12 20:51:59 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 && !filter.test((E) es[r]); r++)
996 >            ;
997 >        if (modified = (r < size)) {
998 >            expectedModCount++;
999 >            modCount++;
1000 >            int w = r++;
1001 >            try {
1002 >                for (E e; r < size; r++)
1003 >                    if (!filter.test(e = (E) es[r]))
1004 >                        es[w++] = e;
1005 >            } catch (Throwable ex) {
1006 >                // copy remaining elements
1007 >                System.arraycopy(es, r, es, w, size - r);
1008 >                w += size - r;
1009 >                throw ex;
1010 >            } finally {
1011 >                Arrays.fill(es, elementCount = w, size, null);
1012 >            }
1013 >        }
1014 >        if (modCount != expectedModCount)
1015 >            throw new ConcurrentModificationException();
1016 >        return modified;
1017      }
1018  
1019      /**
# Line 1303 | Line 1343 | public class Vector<E>
1343  
1344      @Override
1345      @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")
1346      public synchronized void replaceAll(UnaryOperator<E> operator) {
1347          Objects.requireNonNull(operator);
1348          final int expectedModCount = modCount;
# Line 1410 | Line 1406 | public class Vector<E>
1406          private int getFence() { // initialize on first use
1407              int hi;
1408              if ((hi = fence) < 0) {
1409 <                synchronized(list) {
1409 >                synchronized (list) {
1410                      array = list.elementData;
1411                      expectedModCount = list.modCount;
1412                      hi = fence = list.elementCount;
# Line 1449 | Line 1445 | public class Vector<E>
1445                  throw new NullPointerException();
1446              if ((lst = list) != null) {
1447                  if ((hi = fence) < 0) {
1448 <                    synchronized(lst) {
1448 >                    synchronized (lst) {
1449                          expectedModCount = lst.modCount;
1450                          a = array = lst.elementData;
1451                          hi = fence = lst.elementCount;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines