[cvs] / jsr166 / src / main / java / util / Vector.java Repository:
ViewVC logotype

Diff of /jsr166/src/main/java/util/Vector.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.30, Thu Nov 3 20:49:07 2016 UTC revision 1.31, Fri Nov 4 02:56:17 2016 UTC
# Line 513  Line 513 
513       * Returns the last component of the vector.       * Returns the last component of the vector.
514       *       *
515       * @return  the last component of the vector, i.e., the component at index       * @return  the last component of the vector, i.e., the component at index
516       *          <code>size()&nbsp;-&nbsp;1</code>.       *          {@code size() - 1}
517       * @throws NoSuchElementException if this vector is empty       * @throws NoSuchElementException if this vector is empty
518       */       */
519      public synchronized E lastElement() {      public synchronized E lastElement() {
# Line 855  Line 855 
855       * Shifts any subsequent elements to the left (subtracts one from their       * Shifts any subsequent elements to the left (subtracts one from their
856       * indices).  Returns the element that was removed from the Vector.       * indices).  Returns the element that was removed from the Vector.
857       *       *
      * @throws ArrayIndexOutOfBoundsException if the index is out of range  
      *         ({@code index < 0 || index >= size()})  
858       * @param index the index of the element to be removed       * @param index the index of the element to be removed
859       * @return element that was removed       * @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       * @since 1.2
863       */       */
864      public synchronized E remove(int index) {      public synchronized E remove(int index) {
# Line 949  Line 949 
949       *         or if the specified collection is null       *         or if the specified collection is null
950       * @since 1.2       * @since 1.2
951       */       */
952      public synchronized boolean removeAll(Collection<?> c) {      public boolean removeAll(Collection<?> c) {
953          return super.removeAll(c);          Objects.requireNonNull(c);
954            return bulkRemove(e -> c.contains(e));
955      }      }
956    
957      /**      /**
# Line 972  Line 973 
973       *         or if the specified collection is null       *         or if the specified collection is null
974       * @since 1.2       * @since 1.2
975       */       */
976      public synchronized boolean retainAll(Collection<?> c) {      public boolean retainAll(Collection<?> c) {
977          return super.retainAll(c);          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 
1344    
1345      @Override      @Override
1346      @SuppressWarnings("unchecked")      @SuppressWarnings("unchecked")
     public synchronized boolean removeIf(Predicate<? super E> filter) {  
         Objects.requireNonNull(filter);  
         // figure out which elements are to be removed  
         // any exception thrown from the filter predicate at this stage  
         // will leave the collection unmodified  
         int removeCount = 0;  
         final int size = elementCount;  
         final BitSet removeSet = new BitSet(size);  
         final int expectedModCount = modCount;  
         for (int i=0; modCount == expectedModCount && i < size; i++) {  
             @SuppressWarnings("unchecked")  
             final E element = (E) elementData[i];  
             if (filter.test(element)) {  
                 removeSet.set(i);  
                 removeCount++;  
             }  
         }  
         if (modCount != expectedModCount) {  
             throw new ConcurrentModificationException();  
         }  
   
         // shift surviving elements left over the spaces left by removed elements  
         final boolean anyToRemove = removeCount > 0;  
         if (anyToRemove) {  
             final int newSize = size - removeCount;  
             for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {  
                 i = removeSet.nextClearBit(i);  
                 elementData[j] = elementData[i];  
             }  
             for (int k=newSize; k < size; k++) {  
                 elementData[k] = null;  // Let gc do its work  
             }  
             elementCount = newSize;  
             if (modCount != expectedModCount) {  
                 throw new ConcurrentModificationException();  
             }  
             modCount++;  
         }  
   
         return anyToRemove;  
     }  
   
     @Override  
     @SuppressWarnings("unchecked")  
1347      public synchronized void replaceAll(UnaryOperator<E> operator) {      public synchronized void replaceAll(UnaryOperator<E> operator) {
1348          Objects.requireNonNull(operator);          Objects.requireNonNull(operator);
1349          final int expectedModCount = modCount;          final int expectedModCount = modCount;

Legend:
Removed from v.1.30  
changed lines
  Added in v.1.31

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8