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.22 by jsr166, Tue Sep 11 15:38:19 2007 UTC vs.
Revision 1.34 by jsr166, Sun Nov 13 19:58:47 2016 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright 1994-2007 Sun Microsystems, Inc.  All Rights Reserved.
2 > * Copyright (c) 1994, 2013, 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
6   * under the terms of the GNU General Public License version 2 only, as
7 < * published by the Free Software Foundation.  Sun designates this
7 > * published by the Free Software Foundation.  Oracle designates this
8   * particular file as subject to the "Classpath" exception as provided
9 < * by Sun in the LICENSE file that accompanied this code.
9 > * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# Line 18 | Line 18
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21 < * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 < * CA 95054 USA or visit www.sun.com if you need additional information or
23 < * have any questions.
21 > * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 > * or visit www.oracle.com if you need additional information or have any
23 > * questions.
24   */
25  
26   package java.util;
27  
28 + import java.util.function.Consumer;
29 + import java.util.function.Predicate;
30 + import java.util.function.UnaryOperator;
31 +
32   /**
33   * The {@code Vector} class implements a growable array of
34   * objects. Like an array, it contains components that can be
# Line 41 | Line 45 | package java.util;
45   * capacity of a vector before inserting a large number of
46   * components; this reduces the amount of incremental reallocation.
47   *
48 < * <p><a name="fail-fast"/>
48 > * <p id="fail-fast">
49   * The iterators returned by this class's {@link #iterator() iterator} and
50   * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
51   * if the vector is structurally modified at any time after the iterator is
# Line 52 | Line 56 | package java.util;
56   * concurrent modification, the iterator fails quickly and cleanly, rather
57   * than risking arbitrary, non-deterministic behavior at an undetermined
58   * time in the future.  The {@link Enumeration Enumerations} returned by
59 < * the {@link #elements() elements} method are <em>not</em> fail-fast.
59 > * the {@link #elements() elements} method are <em>not</em> fail-fast; if the
60 > * Vector is structurally modified at any time after the enumeration is
61 > * created then the results of enumerating are undefined.
62   *
63   * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
64   * as it is, generally speaking, impossible to make any hard guarantees in the
# Line 64 | Line 70 | package java.util;
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"> Java
74 < * Collections Framework</a>.  Unlike the new collection
75 < * implementations, {@code Vector} is synchronized.
73 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
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
77 > * ArrayList} in place of {@code Vector}.
78 > *
79 > * @param <E> Type of component elements
80   *
81   * @author  Lee Boynton
82   * @author  Jonathan Payne
73 * @version %I%, %G%
83   * @see Collection
75 * @see List
76 * @see ArrayList
84   * @see LinkedList
85 < * @since   JDK1.0
85 > * @since   1.0
86   */
87   public class Vector<E>
88      extends AbstractList<E>
# Line 125 | Line 132 | public class Vector<E>
132       *         is negative
133       */
134      public Vector(int initialCapacity, int capacityIncrement) {
135 <        super();
135 >        super();
136          if (initialCapacity < 0)
137              throw new IllegalArgumentException("Illegal Capacity: "+
138                                                 initialCapacity);
139 <        this.elementData = new Object[initialCapacity];
140 <        this.capacityIncrement = capacityIncrement;
139 >        this.elementData = new Object[initialCapacity];
140 >        this.capacityIncrement = capacityIncrement;
141      }
142  
143      /**
# Line 142 | Line 149 | public class Vector<E>
149       *         is negative
150       */
151      public Vector(int initialCapacity) {
152 <        this(initialCapacity, 0);
152 >        this(initialCapacity, 0);
153      }
154  
155      /**
# Line 151 | Line 158 | public class Vector<E>
158       * zero.
159       */
160      public Vector() {
161 <        this(10);
161 >        this(10);
162      }
163  
164      /**
# Line 165 | Line 172 | public class Vector<E>
172       * @since   1.2
173       */
174      public Vector(Collection<? extends E> c) {
175 <        elementData = c.toArray();
176 <        elementCount = elementData.length;
177 <        // c.toArray might (incorrectly) not return Object[] (see 6260652)
178 <        if (elementData.getClass() != Object[].class)
179 <            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
175 >        elementData = c.toArray();
176 >        elementCount = elementData.length;
177 >        // defend against c.toArray (incorrectly) not returning Object[]
178 >        // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
179 >        if (elementData.getClass() != Object[].class)
180 >            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
181      }
182  
183      /**
# Line 186 | Line 194 | public class Vector<E>
194       * @see #toArray(Object[])
195       */
196      public synchronized void copyInto(Object[] anArray) {
197 <        System.arraycopy(elementData, 0, anArray, 0, elementCount);
197 >        System.arraycopy(elementData, 0, anArray, 0, elementCount);
198      }
199  
200      /**
# Line 198 | Line 206 | public class Vector<E>
206       * minimize the storage of a vector.
207       */
208      public synchronized void trimToSize() {
209 <        modCount++;
210 <        int oldCapacity = elementData.length;
211 <        if (elementCount < oldCapacity) {
209 >        modCount++;
210 >        int oldCapacity = elementData.length;
211 >        if (elementCount < oldCapacity) {
212              elementData = Arrays.copyOf(elementData, elementCount);
213 <        }
213 >        }
214      }
215  
216      /**
# Line 223 | Line 231 | public class Vector<E>
231       * @param minCapacity the desired minimum capacity
232       */
233      public synchronized void ensureCapacity(int minCapacity) {
234 <        modCount++;
235 <        ensureCapacityHelper(minCapacity);
234 >        if (minCapacity > 0) {
235 >            modCount++;
236 >            if (minCapacity > elementData.length)
237 >                grow(minCapacity);
238 >        }
239      }
240  
241      /**
242 <     * This implements the unsynchronized semantics of ensureCapacity.
243 <     * Synchronized methods in this class can internally call this
244 <     * method for ensuring capacity without incurring the cost of an
245 <     * extra synchronization.
246 <     *
247 <     * @see #ensureCapacity(int)
248 <     */
249 <    private void ensureCapacityHelper(int minCapacity) {
250 <        int oldCapacity = elementData.length;
251 <        if (minCapacity > oldCapacity) {
252 <            Object[] oldData = elementData;
253 <            int newCapacity = (capacityIncrement > 0) ?
254 <                (oldCapacity + capacityIncrement) : (oldCapacity * 2);
255 <            if (newCapacity < minCapacity) {
256 <                newCapacity = minCapacity;
257 <            }
258 <            elementData = Arrays.copyOf(elementData, newCapacity);
259 <        }
242 >     * The maximum size of array to allocate (unless necessary).
243 >     * Some VMs reserve some header words in an array.
244 >     * Attempts to allocate larger arrays may result in
245 >     * OutOfMemoryError: Requested array size exceeds VM limit
246 >     */
247 >    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
248 >
249 >    /**
250 >     * Increases the capacity to ensure that it can hold at least the
251 >     * number of elements specified by the minimum capacity argument.
252 >     *
253 >     * @param minCapacity the desired minimum capacity
254 >     * @throws OutOfMemoryError if minCapacity is less than zero
255 >     */
256 >    private Object[] grow(int minCapacity) {
257 >        return elementData = Arrays.copyOf(elementData,
258 >                                           newCapacity(minCapacity));
259 >    }
260 >
261 >    private Object[] grow() {
262 >        return grow(elementCount + 1);
263 >    }
264 >
265 >    /**
266 >     * Returns a capacity at least as large as the given minimum capacity.
267 >     * Will not return a capacity greater than MAX_ARRAY_SIZE unless
268 >     * the given minimum capacity is greater than MAX_ARRAY_SIZE.
269 >     *
270 >     * @param minCapacity the desired minimum capacity
271 >     * @throws OutOfMemoryError if minCapacity is less than zero
272 >     */
273 >    private int newCapacity(int minCapacity) {
274 >        // overflow-conscious code
275 >        int oldCapacity = elementData.length;
276 >        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
277 >                                         capacityIncrement : oldCapacity);
278 >        if (newCapacity - minCapacity <= 0) {
279 >            if (minCapacity < 0) // overflow
280 >                throw new OutOfMemoryError();
281 >            return minCapacity;
282 >        }
283 >        return (newCapacity - MAX_ARRAY_SIZE <= 0)
284 >            ? newCapacity
285 >            : hugeCapacity(minCapacity);
286 >    }
287 >
288 >    private static int hugeCapacity(int minCapacity) {
289 >        if (minCapacity < 0) // overflow
290 >            throw new OutOfMemoryError();
291 >        return (minCapacity > MAX_ARRAY_SIZE) ?
292 >            Integer.MAX_VALUE :
293 >            MAX_ARRAY_SIZE;
294      }
295  
296      /**
# Line 258 | Line 303 | public class Vector<E>
303       * @throws ArrayIndexOutOfBoundsException if the new size is negative
304       */
305      public synchronized void setSize(int newSize) {
306 <        modCount++;
307 <        if (newSize > elementCount) {
308 <            ensureCapacityHelper(newSize);
309 <        } else {
310 <            for (int i = newSize ; i < elementCount ; i++) {
311 <                elementData[i] = null;
267 <            }
268 <        }
269 <        elementCount = newSize;
306 >        modCount++;
307 >        if (newSize > elementData.length)
308 >            grow(newSize);
309 >        for (int i = newSize; i < elementCount; i++)
310 >            elementData[i] = null;
311 >        elementCount = newSize;
312      }
313  
314      /**
# Line 277 | Line 319 | public class Vector<E>
319       *          of this vector)
320       */
321      public synchronized int capacity() {
322 <        return elementData.length;
322 >        return elementData.length;
323      }
324  
325      /**
# Line 286 | Line 328 | public class Vector<E>
328       * @return  the number of components in this vector
329       */
330      public synchronized int size() {
331 <        return elementCount;
331 >        return elementCount;
332      }
333  
334      /**
# Line 297 | Line 339 | public class Vector<E>
339       *          {@code false} otherwise.
340       */
341      public synchronized boolean isEmpty() {
342 <        return elementCount == 0;
342 >        return elementCount == 0;
343      }
344  
345      /**
346       * Returns an enumeration of the components of this vector. The
347       * returned {@code Enumeration} object will generate all items in
348       * this vector. The first item generated is the item at index {@code 0},
349 <     * then the item at index {@code 1}, and so on.
349 >     * then the item at index {@code 1}, and so on. If the vector is
350 >     * structurally modified while enumerating over the elements then the
351 >     * results of enumerating are undefined.
352       *
353       * @return  an enumeration of the components of this vector
354       * @see     Iterator
355       */
356      public Enumeration<E> elements() {
357 <        return new Enumeration<E>() {
358 <            int count = 0;
357 >        return new Enumeration<E>() {
358 >            int count = 0;
359 >
360 >            public boolean hasMoreElements() {
361 >                return count < elementCount;
362 >            }
363  
364 <            public boolean hasMoreElements() {
365 <                return count < elementCount;
366 <            }
367 <
368 <            public E nextElement() {
369 <                synchronized (Vector.this) {
370 <                    if (count < elementCount) {
371 <                        return elementData(count++);
372 <                    }
325 <                }
326 <                throw new NoSuchElementException("Vector Enumeration");
327 <            }
328 <        };
364 >            public E nextElement() {
365 >                synchronized (Vector.this) {
366 >                    if (count < elementCount) {
367 >                        return elementData(count++);
368 >                    }
369 >                }
370 >                throw new NoSuchElementException("Vector Enumeration");
371 >            }
372 >        };
373      }
374  
375      /**
376       * Returns {@code true} if this vector contains the specified element.
377       * More formally, returns {@code true} if and only if this vector
378       * contains at least one element {@code e} such that
379 <     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
379 >     * {@code Objects.equals(o, e)}.
380       *
381       * @param o element whose presence in this vector is to be tested
382       * @return {@code true} if this vector contains the specified element
383       */
384      public boolean contains(Object o) {
385 <        return indexOf(o, 0) >= 0;
385 >        return indexOf(o, 0) >= 0;
386      }
387  
388      /**
389       * Returns the index of the first occurrence of the specified element
390       * in this vector, or -1 if this vector does not contain the element.
391       * More formally, returns the lowest index {@code i} such that
392 <     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
392 >     * {@code Objects.equals(o, get(i))},
393       * or -1 if there is no such index.
394       *
395       * @param o element to search for
# Line 353 | Line 397 | public class Vector<E>
397       *         this vector, or -1 if this vector does not contain the element
398       */
399      public int indexOf(Object o) {
400 <        return indexOf(o, 0);
400 >        return indexOf(o, 0);
401      }
402  
403      /**
# Line 361 | Line 405 | public class Vector<E>
405       * this vector, searching forwards from {@code index}, or returns -1 if
406       * the element is not found.
407       * More formally, returns the lowest index {@code i} such that
408 <     * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
408 >     * {@code (i >= index && Objects.equals(o, get(i)))},
409       * or -1 if there is no such index.
410       *
411       * @param o element to search for
# Line 373 | Line 417 | public class Vector<E>
417       * @see     Object#equals(Object)
418       */
419      public synchronized int indexOf(Object o, int index) {
420 <        if (o == null) {
421 <            for (int i = index ; i < elementCount ; i++)
422 <                if (elementData[i]==null)
423 <                    return i;
424 <        } else {
425 <            for (int i = index ; i < elementCount ; i++)
426 <                if (o.equals(elementData[i]))
427 <                    return i;
428 <        }
429 <        return -1;
420 >        if (o == null) {
421 >            for (int i = index ; i < elementCount ; i++)
422 >                if (elementData[i]==null)
423 >                    return i;
424 >        } else {
425 >            for (int i = index ; i < elementCount ; i++)
426 >                if (o.equals(elementData[i]))
427 >                    return i;
428 >        }
429 >        return -1;
430      }
431  
432      /**
433       * Returns the index of the last occurrence of the specified element
434       * in this vector, or -1 if this vector does not contain the element.
435       * More formally, returns the highest index {@code i} such that
436 <     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
436 >     * {@code Objects.equals(o, get(i))},
437       * or -1 if there is no such index.
438       *
439       * @param o element to search for
# Line 397 | Line 441 | public class Vector<E>
441       *         this vector, or -1 if this vector does not contain the element
442       */
443      public synchronized int lastIndexOf(Object o) {
444 <        return lastIndexOf(o, elementCount-1);
444 >        return lastIndexOf(o, elementCount-1);
445      }
446  
447      /**
# Line 405 | Line 449 | public class Vector<E>
449       * this vector, searching backwards from {@code index}, or returns -1 if
450       * the element is not found.
451       * More formally, returns the highest index {@code i} such that
452 <     * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
452 >     * {@code (i <= index && Objects.equals(o, get(i)))},
453       * or -1 if there is no such index.
454       *
455       * @param o element to search for
# Line 420 | Line 464 | public class Vector<E>
464          if (index >= elementCount)
465              throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
466  
467 <        if (o == null) {
468 <            for (int i = index; i >= 0; i--)
469 <                if (elementData[i]==null)
470 <                    return i;
471 <        } else {
472 <            for (int i = index; i >= 0; i--)
473 <                if (o.equals(elementData[i]))
474 <                    return i;
475 <        }
476 <        return -1;
467 >        if (o == null) {
468 >            for (int i = index; i >= 0; i--)
469 >                if (elementData[i]==null)
470 >                    return i;
471 >        } else {
472 >            for (int i = index; i >= 0; i--)
473 >                if (o.equals(elementData[i]))
474 >                    return i;
475 >        }
476 >        return -1;
477      }
478  
479      /**
# Line 441 | Line 485 | public class Vector<E>
485       * @param      index   an index into this vector
486       * @return     the component at the specified index
487       * @throws ArrayIndexOutOfBoundsException if the index is out of range
488 <     *         ({@code index < 0 || index >= size()})
488 >     *         ({@code index < 0 || index >= size()})
489       */
490      public synchronized E elementAt(int index) {
491 <        if (index >= elementCount) {
492 <            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
493 <        }
491 >        if (index >= elementCount) {
492 >            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
493 >        }
494  
495          return elementData(index);
496      }
# Line 459 | Line 503 | public class Vector<E>
503       * @throws NoSuchElementException if this vector has no components
504       */
505      public synchronized E firstElement() {
506 <        if (elementCount == 0) {
507 <            throw new NoSuchElementException();
508 <        }
509 <        return elementData(0);
506 >        if (elementCount == 0) {
507 >            throw new NoSuchElementException();
508 >        }
509 >        return elementData(0);
510      }
511  
512      /**
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() {
520 <        if (elementCount == 0) {
521 <            throw new NoSuchElementException();
522 <        }
523 <        return elementData(elementCount - 1);
520 >        if (elementCount == 0) {
521 >            throw new NoSuchElementException();
522 >        }
523 >        return elementData(elementCount - 1);
524      }
525  
526      /**
# Line 497 | Line 541 | public class Vector<E>
541       * @param      obj     what the component is to be set to
542       * @param      index   the specified index
543       * @throws ArrayIndexOutOfBoundsException if the index is out of range
544 <     *         ({@code index < 0 || index >= size()})
544 >     *         ({@code index < 0 || index >= size()})
545       */
546      public synchronized void setElementAt(E obj, int index) {
547 <        if (index >= elementCount) {
548 <            throw new ArrayIndexOutOfBoundsException(index + " >= " +
549 <                                                     elementCount);
550 <        }
551 <        elementData[index] = obj;
547 >        if (index >= elementCount) {
548 >            throw new ArrayIndexOutOfBoundsException(index + " >= " +
549 >                                                     elementCount);
550 >        }
551 >        elementData[index] = obj;
552      }
553  
554      /**
# Line 524 | Line 568 | public class Vector<E>
568       *
569       * @param      index   the index of the object to remove
570       * @throws ArrayIndexOutOfBoundsException if the index is out of range
571 <     *         ({@code index < 0 || index >= size()})
571 >     *         ({@code index < 0 || index >= size()})
572       */
573      public synchronized void removeElementAt(int index) {
574 <        modCount++;
575 <        if (index >= elementCount) {
576 <            throw new ArrayIndexOutOfBoundsException(index + " >= " +
577 <                                                     elementCount);
578 <        }
579 <        else if (index < 0) {
580 <            throw new ArrayIndexOutOfBoundsException(index);
581 <        }
582 <        int j = elementCount - index - 1;
583 <        if (j > 0) {
584 <            System.arraycopy(elementData, index + 1, elementData, index, j);
585 <        }
586 <        elementCount--;
587 <        elementData[elementCount] = null; /* to let gc do its work */
574 >        if (index >= elementCount) {
575 >            throw new ArrayIndexOutOfBoundsException(index + " >= " +
576 >                                                     elementCount);
577 >        }
578 >        else if (index < 0) {
579 >            throw new ArrayIndexOutOfBoundsException(index);
580 >        }
581 >        int j = elementCount - index - 1;
582 >        if (j > 0) {
583 >            System.arraycopy(elementData, index + 1, elementData, index, j);
584 >        }
585 >        modCount++;
586 >        elementCount--;
587 >        elementData[elementCount] = null; /* to let gc do its work */
588      }
589  
590      /**
# Line 564 | Line 608 | public class Vector<E>
608       * @param      obj     the component to insert
609       * @param      index   where to insert the new component
610       * @throws ArrayIndexOutOfBoundsException if the index is out of range
611 <     *         ({@code index < 0 || index > size()})
611 >     *         ({@code index < 0 || index > size()})
612       */
613      public synchronized void insertElementAt(E obj, int index) {
614 <        modCount++;
615 <        if (index > elementCount) {
616 <            throw new ArrayIndexOutOfBoundsException(index
617 <                                                     + " > " + elementCount);
618 <        }
619 <        ensureCapacityHelper(elementCount + 1);
620 <        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
621 <        elementData[index] = obj;
622 <        elementCount++;
614 >        if (index > elementCount) {
615 >            throw new ArrayIndexOutOfBoundsException(index
616 >                                                     + " > " + elementCount);
617 >        }
618 >        modCount++;
619 >        final int s = elementCount;
620 >        Object[] elementData = this.elementData;
621 >        if (s == elementData.length)
622 >            elementData = grow();
623 >        System.arraycopy(elementData, index,
624 >                         elementData, index + 1,
625 >                         s - index);
626 >        elementData[index] = obj;
627 >        elementCount = s + 1;
628      }
629  
630      /**
# Line 590 | Line 639 | public class Vector<E>
639       * @param   obj   the component to be added
640       */
641      public synchronized void addElement(E obj) {
642 <        modCount++;
643 <        ensureCapacityHelper(elementCount + 1);
595 <        elementData[elementCount++] = obj;
642 >        modCount++;
643 >        add(obj, elementData, elementCount);
644      }
645  
646      /**
# Line 611 | Line 659 | public class Vector<E>
659       *          vector; {@code false} otherwise.
660       */
661      public synchronized boolean removeElement(Object obj) {
662 <        modCount++;
663 <        int i = indexOf(obj);
664 <        if (i >= 0) {
665 <            removeElementAt(i);
666 <            return true;
667 <        }
668 <        return false;
662 >        modCount++;
663 >        int i = indexOf(obj);
664 >        if (i >= 0) {
665 >            removeElementAt(i);
666 >            return true;
667 >        }
668 >        return false;
669      }
670  
671      /**
# Line 627 | Line 675 | public class Vector<E>
675       * method (which is part of the {@link List} interface).
676       */
677      public synchronized void removeAllElements() {
678 <        modCount++;
679 <        // Let gc do its work
680 <        for (int i = 0; i < elementCount; i++)
633 <            elementData[i] = null;
678 >        // Let gc do its work
679 >        for (int i = 0; i < elementCount; i++)
680 >            elementData[i] = null;
681  
682 <        elementCount = 0;
682 >        modCount++;
683 >        elementCount = 0;
684      }
685  
686      /**
# Line 643 | Line 691 | public class Vector<E>
691       * @return  a clone of this vector
692       */
693      public synchronized Object clone() {
694 <        try {
695 <            @SuppressWarnings("unchecked")
696 <                Vector<E> v = (Vector<E>) super.clone();
697 <            v.elementData = Arrays.copyOf(elementData, elementCount);
698 <            v.modCount = 0;
699 <            return v;
700 <        } catch (CloneNotSupportedException e) {
701 <            // this shouldn't happen, since we are Cloneable
702 <            throw new InternalError();
703 <        }
694 >        try {
695 >            @SuppressWarnings("unchecked")
696 >            Vector<E> v = (Vector<E>) super.clone();
697 >            v.elementData = Arrays.copyOf(elementData, elementCount);
698 >            v.modCount = 0;
699 >            return v;
700 >        } catch (CloneNotSupportedException e) {
701 >            // this shouldn't happen, since we are Cloneable
702 >            throw new InternalError(e);
703 >        }
704      }
705  
706      /**
# Line 679 | Line 727 | public class Vector<E>
727       * of the Vector <em>only</em> if the caller knows that the Vector
728       * does not contain any null elements.)
729       *
730 +     * @param <T> type of array elements. The same type as {@code <E>} or a
731 +     * supertype of {@code <E>}.
732       * @param a the array into which the elements of the Vector are to
733 <     *          be stored, if it is big enough; otherwise, a new array of the
734 <     *          same runtime type is allocated for this purpose.
733 >     *          be stored, if it is big enough; otherwise, a new array of the
734 >     *          same runtime type is allocated for this purpose.
735       * @return an array containing the elements of the Vector
736 <     * @throws ArrayStoreException if the runtime type of a is not a supertype
737 <     * of the runtime type of every element in this Vector
736 >     * @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not
737 >     * a supertype of the runtime type, {@code <E>}, of every element in this
738 >     * Vector
739       * @throws NullPointerException if the given array is null
740       * @since 1.2
741       */
# Line 693 | Line 744 | public class Vector<E>
744          if (a.length < elementCount)
745              return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
746  
747 <        System.arraycopy(elementData, 0, a, 0, elementCount);
747 >        System.arraycopy(elementData, 0, a, 0, elementCount);
748  
749          if (a.length > elementCount)
750              a[elementCount] = null;
# Line 705 | Line 756 | public class Vector<E>
756  
757      @SuppressWarnings("unchecked")
758      E elementData(int index) {
759 <        return (E) elementData[index];
759 >        return (E) elementData[index];
760 >    }
761 >
762 >    @SuppressWarnings("unchecked")
763 >    static <E> E elementAt(Object[] es, int index) {
764 >        return (E) es[index];
765      }
766  
767      /**
# Line 718 | Line 774 | public class Vector<E>
774       * @since 1.2
775       */
776      public synchronized E get(int index) {
777 <        if (index >= elementCount)
778 <            throw new ArrayIndexOutOfBoundsException(index);
777 >        if (index >= elementCount)
778 >            throw new ArrayIndexOutOfBoundsException(index);
779  
780 <        return elementData(index);
780 >        return elementData(index);
781      }
782  
783      /**
# Line 732 | Line 788 | public class Vector<E>
788       * @param element element to be stored at the specified position
789       * @return the element previously at the specified position
790       * @throws ArrayIndexOutOfBoundsException if the index is out of range
791 <     *         ({@code index < 0 || index >= size()})
791 >     *         ({@code index < 0 || index >= size()})
792       * @since 1.2
793       */
794      public synchronized E set(int index, E element) {
795 <        if (index >= elementCount)
796 <            throw new ArrayIndexOutOfBoundsException(index);
795 >        if (index >= elementCount)
796 >            throw new ArrayIndexOutOfBoundsException(index);
797 >
798 >        E oldValue = elementData(index);
799 >        elementData[index] = element;
800 >        return oldValue;
801 >    }
802  
803 <        E oldValue = elementData(index);
804 <        elementData[index] = element;
805 <        return oldValue;
803 >    /**
804 >     * This helper method split out from add(E) to keep method
805 >     * bytecode size under 35 (the -XX:MaxInlineSize default value),
806 >     * which helps when add(E) is called in a C1-compiled loop.
807 >     */
808 >    private void add(E e, Object[] elementData, int s) {
809 >        if (s == elementData.length)
810 >            elementData = grow();
811 >        elementData[s] = e;
812 >        elementCount = s + 1;
813      }
814  
815      /**
# Line 752 | Line 820 | public class Vector<E>
820       * @since 1.2
821       */
822      public synchronized boolean add(E e) {
823 <        modCount++;
824 <        ensureCapacityHelper(elementCount + 1);
757 <        elementData[elementCount++] = e;
823 >        modCount++;
824 >        add(e, elementData, elementCount);
825          return true;
826      }
827  
# Line 762 | Line 829 | public class Vector<E>
829       * Removes the first occurrence of the specified element in this Vector
830       * If the Vector does not contain the element, it is unchanged.  More
831       * formally, removes the element with the lowest index i such that
832 <     * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
832 >     * {@code Objects.equals(o, get(i))} (if such
833       * an element exists).
834       *
835       * @param o element to be removed from this Vector, if present
# Line 793 | Line 860 | public class Vector<E>
860       * Shifts any subsequent elements to the left (subtracts one from their
861       * indices).  Returns the element that was removed from the Vector.
862       *
796     * @throws ArrayIndexOutOfBoundsException if the index is out of range
797     *         ({@code index < 0 || index >= size()})
863       * @param index the index of the element to be removed
864       * @return element that was removed
865 +     * @throws ArrayIndexOutOfBoundsException if the index is out of range
866 +     *         ({@code index < 0 || index >= size()})
867       * @since 1.2
868       */
869      public synchronized E remove(int index) {
870 <        modCount++;
871 <        if (index >= elementCount)
872 <            throw new ArrayIndexOutOfBoundsException(index);
873 <        E oldValue = elementData(index);
807 <
808 <        int numMoved = elementCount - index - 1;
809 <        if (numMoved > 0)
810 <            System.arraycopy(elementData, index+1, elementData, index,
811 <                             numMoved);
812 <        elementData[--elementCount] = null; // Let gc do its work
870 >        modCount++;
871 >        if (index >= elementCount)
872 >            throw new ArrayIndexOutOfBoundsException(index);
873 >        E oldValue = elementData(index);
874  
875 <        return oldValue;
875 >        int numMoved = elementCount - index - 1;
876 >        if (numMoved > 0)
877 >            System.arraycopy(elementData, index+1, elementData, index,
878 >                             numMoved);
879 >        elementData[--elementCount] = null; // Let gc do its work
880 >
881 >        return oldValue;
882      }
883  
884      /**
# Line 833 | Line 900 | public class Vector<E>
900       * @param   c a collection whose elements will be tested for containment
901       *          in this Vector
902       * @return true if this Vector contains all of the elements in the
903 <     *         specified collection
903 >     *         specified collection
904       * @throws NullPointerException if the specified collection is null
905       */
906      public synchronized boolean containsAll(Collection<?> c) {
# Line 853 | Line 920 | public class Vector<E>
920       * @throws NullPointerException if the specified collection is null
921       * @since 1.2
922       */
923 <    public synchronized boolean addAll(Collection<? extends E> c) {
857 <        modCount++;
923 >    public boolean addAll(Collection<? extends E> c) {
924          Object[] a = c.toArray();
925 +        modCount++;
926          int numNew = a.length;
927 <        ensureCapacityHelper(elementCount + numNew);
928 <        System.arraycopy(a, 0, elementData, elementCount, numNew);
929 <        elementCount += numNew;
930 <        return numNew != 0;
927 >        if (numNew == 0)
928 >            return false;
929 >        synchronized (this) {
930 >            Object[] elementData = this.elementData;
931 >            final int s = elementCount;
932 >            if (numNew > elementData.length - s)
933 >                elementData = grow(s + numNew);
934 >            System.arraycopy(a, 0, elementData, s, numNew);
935 >            elementCount = s + numNew;
936 >            return true;
937 >        }
938      }
939  
940      /**
# Line 871 | Line 945 | public class Vector<E>
945       * @return true if this Vector changed as a result of the call
946       * @throws ClassCastException if the types of one or more elements
947       *         in this vector are incompatible with the specified
948 <     *         collection (optional)
948 >     *         collection
949 >     * (<a href="Collection.html#optional-restrictions">optional</a>)
950       * @throws NullPointerException if this vector contains one or more null
951       *         elements and the specified collection does not support null
952 <     *         elements (optional), or if the specified collection is null
952 >     *         elements
953 >     * (<a href="Collection.html#optional-restrictions">optional</a>),
954 >     *         or if the specified collection is null
955       * @since 1.2
956       */
957 <    public synchronized boolean removeAll(Collection<?> c) {
958 <        return super.removeAll(c);
957 >    public boolean removeAll(Collection<?> c) {
958 >        Objects.requireNonNull(c);
959 >        return bulkRemove(e -> c.contains(e));
960      }
961  
962      /**
# Line 891 | Line 969 | public class Vector<E>
969       * @return true if this Vector changed as a result of the call
970       * @throws ClassCastException if the types of one or more elements
971       *         in this vector are incompatible with the specified
972 <     *         collection (optional)
972 >     *         collection
973 >     * (<a href="Collection.html#optional-restrictions">optional</a>)
974       * @throws NullPointerException if this vector contains one or more null
975       *         elements and the specified collection does not support null
976 <     *         elements (optional), or if the specified collection is null
976 >     *         elements
977 >     *         (<a href="Collection.html#optional-restrictions">optional</a>),
978 >     *         or if the specified collection is null
979       * @since 1.2
980       */
981 <    public synchronized boolean retainAll(Collection<?> c)  {
982 <        return super.retainAll(c);
981 >    public boolean retainAll(Collection<?> c) {
982 >        Objects.requireNonNull(c);
983 >        return bulkRemove(e -> !c.contains(e));
984 >    }
985 >
986 >    @Override
987 >    public boolean removeIf(Predicate<? super E> filter) {
988 >        Objects.requireNonNull(filter);
989 >        return bulkRemove(filter);
990 >    }
991 >
992 >    // A tiny bit set implementation
993 >
994 >    private static long[] nBits(int n) {
995 >        return new long[((n - 1) >> 6) + 1];
996 >    }
997 >    private static void setBit(long[] bits, int i) {
998 >        bits[i >> 6] |= 1L << i;
999 >    }
1000 >    private static boolean isClear(long[] bits, int i) {
1001 >        return (bits[i >> 6] & (1L << i)) == 0;
1002 >    }
1003 >
1004 >    private synchronized boolean bulkRemove(Predicate<? super E> filter) {
1005 >        int expectedModCount = modCount;
1006 >        final Object[] es = elementData;
1007 >        final int end = elementCount;
1008 >        final boolean modified;
1009 >        int i;
1010 >        // Optimize for initial run of survivors
1011 >        for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
1012 >            ;
1013 >        // Tolerate predicates that reentrantly access the collection for
1014 >        // read (but writers still get CME), so traverse once to find
1015 >        // elements to delete, a second pass to physically expunge.
1016 >        if (modified = (i < end)) {
1017 >            expectedModCount++;
1018 >            modCount++;
1019 >            final int beg = i;
1020 >            final long[] deathRow = nBits(end - beg);
1021 >            deathRow[0] = 1L;   // set bit 0
1022 >            for (i = beg + 1; i < end; i++)
1023 >                if (filter.test(elementAt(es, i)))
1024 >                    setBit(deathRow, i - beg);
1025 >            int w = beg;
1026 >            for (i = beg; i < end; i++)
1027 >                if (isClear(deathRow, i - beg))
1028 >                    es[w++] = es[i];
1029 >            Arrays.fill(es, elementCount = w, end, null);
1030 >        }
1031 >        if (modCount != expectedModCount)
1032 >            throw new ConcurrentModificationException();
1033 >        return modified;
1034      }
1035  
1036      /**
# Line 919 | Line 1051 | public class Vector<E>
1051       * @since 1.2
1052       */
1053      public synchronized boolean addAll(int index, Collection<? extends E> c) {
1054 <        modCount++;
1055 <        if (index < 0 || index > elementCount)
924 <            throw new ArrayIndexOutOfBoundsException(index);
1054 >        if (index < 0 || index > elementCount)
1055 >            throw new ArrayIndexOutOfBoundsException(index);
1056  
1057          Object[] a = c.toArray();
1058 <        int numNew = a.length;
1059 <        ensureCapacityHelper(elementCount + numNew);
1060 <
1061 <        int numMoved = elementCount - index;
1062 <        if (numMoved > 0)
1063 <            System.arraycopy(elementData, index, elementData, index + numNew,
1064 <                             numMoved);
1065 <
1058 >        modCount++;
1059 >        int numNew = a.length;
1060 >        if (numNew == 0)
1061 >            return false;
1062 >        Object[] elementData = this.elementData;
1063 >        final int s = elementCount;
1064 >        if (numNew > elementData.length - s)
1065 >            elementData = grow(s + numNew);
1066 >
1067 >        int numMoved = s - index;
1068 >        if (numMoved > 0)
1069 >            System.arraycopy(elementData, index,
1070 >                             elementData, index + numNew,
1071 >                             numMoved);
1072          System.arraycopy(a, 0, elementData, index, numNew);
1073 <        elementCount += numNew;
1074 <        return numNew != 0;
1073 >        elementCount = s + numNew;
1074 >        return true;
1075      }
1076  
1077      /**
# Line 942 | Line 1079 | public class Vector<E>
1079       * true if and only if the specified Object is also a List, both Lists
1080       * have the same size, and all corresponding pairs of elements in the two
1081       * Lists are <em>equal</em>.  (Two elements {@code e1} and
1082 <     * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
1083 <     * e1.equals(e2))}.)  In other words, two Lists are defined to be
1082 >     * {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.)
1083 >     * In other words, two Lists are defined to be
1084       * equal if they contain the same elements in the same order.
1085       *
1086       * @param o the Object to be compared for equality with this Vector
# Line 982 | Line 1119 | public class Vector<E>
1119       * instead of a whole List.  For example, the following idiom
1120       * removes a range of elements from a List:
1121       * <pre>
1122 <     *      list.subList(from, to).clear();
1122 >     *      list.subList(from, to).clear();
1123       * </pre>
1124       * Similar idioms may be constructed for indexOf and lastIndexOf,
1125       * and all of the algorithms in the Collections class can be applied to
# Line 1000 | Line 1137 | public class Vector<E>
1137       * @throws IndexOutOfBoundsException if an endpoint index value is out of range
1138       *         {@code (fromIndex < 0 || toIndex > size)}
1139       * @throws IllegalArgumentException if the endpoint indices are out of order
1140 <     *         {@code (fromIndex > toIndex)}
1140 >     *         {@code (fromIndex > toIndex)}
1141       */
1142      public synchronized List<E> subList(int fromIndex, int toIndex) {
1143          return Collections.synchronizedList(super.subList(fromIndex, toIndex),
# Line 1015 | Line 1152 | public class Vector<E>
1152       * (If {@code toIndex==fromIndex}, this operation has no effect.)
1153       */
1154      protected synchronized void removeRange(int fromIndex, int toIndex) {
1155 <        modCount++;
1019 <        int numMoved = elementCount - toIndex;
1155 >        int numMoved = elementCount - toIndex;
1156          System.arraycopy(elementData, toIndex, elementData, fromIndex,
1157                           numMoved);
1158  
1159 <        // Let gc do its work
1160 <        int newElementCount = elementCount - (toIndex-fromIndex);
1161 <        while (elementCount != newElementCount)
1162 <            elementData[--elementCount] = null;
1159 >        // Let gc do its work
1160 >        modCount++;
1161 >        int newElementCount = elementCount - (toIndex-fromIndex);
1162 >        while (elementCount != newElementCount)
1163 >            elementData[--elementCount] = null;
1164      }
1165  
1166      /**
1167       * Save the state of the {@code Vector} instance to a stream (that
1168 <     * is, serialize it).  This method is present merely for synchronization.
1169 <     * It just calls the default writeObject method.
1170 <     */
1171 <    private synchronized void writeObject(java.io.ObjectOutputStream s)
1172 <        throws java.io.IOException
1173 <    {
1174 <        s.defaultWriteObject();
1168 >     * is, serialize it).
1169 >     * This method performs synchronization to ensure the consistency
1170 >     * of the serialized data.
1171 >     */
1172 >    private void writeObject(java.io.ObjectOutputStream s)
1173 >            throws java.io.IOException {
1174 >        final java.io.ObjectOutputStream.PutField fields = s.putFields();
1175 >        final Object[] data;
1176 >        synchronized (this) {
1177 >            fields.put("capacityIncrement", capacityIncrement);
1178 >            fields.put("elementCount", elementCount);
1179 >            data = elementData.clone();
1180 >        }
1181 >        fields.put("elementData", data);
1182 >        s.writeFields();
1183      }
1184  
1185      /**
# Line 1050 | Line 1195 | public class Vector<E>
1195       * @throws IndexOutOfBoundsException {@inheritDoc}
1196       */
1197      public synchronized ListIterator<E> listIterator(int index) {
1198 <        if (index < 0 || index > elementCount)
1198 >        if (index < 0 || index > elementCount)
1199              throw new IndexOutOfBoundsException("Index: "+index);
1200 <        return new ListItr(index);
1200 >        return new ListItr(index);
1201      }
1202  
1203      /**
# Line 1064 | Line 1209 | public class Vector<E>
1209       * @see #listIterator(int)
1210       */
1211      public synchronized ListIterator<E> listIterator() {
1212 <        return new ListItr(0);
1212 >        return new ListItr(0);
1213      }
1214  
1215      /**
# Line 1075 | Line 1220 | public class Vector<E>
1220       * @return an iterator over the elements in this list in proper sequence
1221       */
1222      public synchronized Iterator<E> iterator() {
1223 <        return new Itr();
1223 >        return new Itr();
1224      }
1225  
1226      /**
1227       * An optimized version of AbstractList.Itr
1228       */
1229      private class Itr implements Iterator<E> {
1230 <        int cursor;       // index of next element to return
1231 <        int lastRet = -1; // index of last element returned; -1 if no such
1232 <        int expectedModCount = modCount;
1230 >        int cursor;       // index of next element to return
1231 >        int lastRet = -1; // index of last element returned; -1 if no such
1232 >        int expectedModCount = modCount;
1233  
1234 <        public boolean hasNext() {
1234 >        public boolean hasNext() {
1235              // Racy but within spec, since modifications are checked
1236              // within or after synchronization in next/previous
1237              return cursor != elementCount;
1238 <        }
1238 >        }
1239  
1240 <        public E next() {
1241 <            synchronized (Vector.this) {
1242 <                checkForComodification();
1243 <                int i = cursor;
1244 <                if (i >= elementCount)
1245 <                    throw new NoSuchElementException();
1246 <                cursor = i + 1;
1247 <                return elementData(lastRet = i);
1103 <            }
1104 <        }
1105 <
1106 <        public void remove() {
1107 <            if (lastRet == -1)
1108 <                throw new IllegalStateException();
1109 <            synchronized (Vector.this) {
1110 <                checkForComodification();
1111 <                Vector.this.remove(lastRet);
1112 <                expectedModCount = modCount;
1240 >        public E next() {
1241 >            synchronized (Vector.this) {
1242 >                checkForComodification();
1243 >                int i = cursor;
1244 >                if (i >= elementCount)
1245 >                    throw new NoSuchElementException();
1246 >                cursor = i + 1;
1247 >                return elementData(lastRet = i);
1248              }
1249 <            cursor = lastRet;
1250 <            lastRet = -1;
1251 <        }
1252 <
1253 <        final void checkForComodification() {
1254 <            if (modCount != expectedModCount)
1255 <                throw new ConcurrentModificationException();
1256 <        }
1249 >        }
1250 >
1251 >        public void remove() {
1252 >            if (lastRet == -1)
1253 >                throw new IllegalStateException();
1254 >            synchronized (Vector.this) {
1255 >                checkForComodification();
1256 >                Vector.this.remove(lastRet);
1257 >                expectedModCount = modCount;
1258 >            }
1259 >            cursor = lastRet;
1260 >            lastRet = -1;
1261 >        }
1262 >
1263 >        @Override
1264 >        public void forEachRemaining(Consumer<? super E> action) {
1265 >            Objects.requireNonNull(action);
1266 >            synchronized (Vector.this) {
1267 >                final int size = elementCount;
1268 >                int i = cursor;
1269 >                if (i >= size) {
1270 >                    return;
1271 >                }
1272 >                final Object[] es = elementData;
1273 >                if (i >= es.length)
1274 >                    throw new ConcurrentModificationException();
1275 >                while (i < size && modCount == expectedModCount)
1276 >                    action.accept(elementAt(es, i++));
1277 >                // update once at end of iteration to reduce heap write traffic
1278 >                cursor = i;
1279 >                lastRet = i - 1;
1280 >                checkForComodification();
1281 >            }
1282 >        }
1283 >
1284 >        final void checkForComodification() {
1285 >            if (modCount != expectedModCount)
1286 >                throw new ConcurrentModificationException();
1287 >        }
1288      }
1289  
1290      /**
1291       * An optimized version of AbstractList.ListItr
1292       */
1293      final class ListItr extends Itr implements ListIterator<E> {
1294 <        ListItr(int index) {
1295 <            super();
1296 <            cursor = index;
1297 <        }
1298 <
1299 <        public boolean hasPrevious() {
1300 <            return cursor != 0;
1301 <        }
1302 <
1303 <        public int nextIndex() {
1304 <            return cursor;
1305 <        }
1306 <
1307 <        public int previousIndex() {
1308 <            return cursor - 1;
1309 <        }
1310 <
1311 <        public E previous() {
1312 <            synchronized (Vector.this) {
1313 <                checkForComodification();
1314 <                int i = cursor - 1;
1315 <                if (i < 0)
1316 <                    throw new NoSuchElementException();
1317 <                cursor = i;
1318 <                return elementData(lastRet = i);
1319 <            }
1320 <        }
1321 <
1322 <        public void set(E e) {
1323 <            if (lastRet == -1)
1324 <                throw new IllegalStateException();
1325 <            synchronized (Vector.this) {
1326 <                checkForComodification();
1327 <                Vector.this.set(lastRet, e);
1328 <            }
1329 <        }
1330 <
1331 <        public void add(E e) {
1332 <            int i = cursor;
1333 <            synchronized (Vector.this) {
1334 <                checkForComodification();
1335 <                Vector.this.add(i, e);
1336 <                expectedModCount = modCount;
1337 <            }
1338 <            cursor = i + 1;
1339 <            lastRet = -1;
1340 <        }
1294 >        ListItr(int index) {
1295 >            super();
1296 >            cursor = index;
1297 >        }
1298 >
1299 >        public boolean hasPrevious() {
1300 >            return cursor != 0;
1301 >        }
1302 >
1303 >        public int nextIndex() {
1304 >            return cursor;
1305 >        }
1306 >
1307 >        public int previousIndex() {
1308 >            return cursor - 1;
1309 >        }
1310 >
1311 >        public E previous() {
1312 >            synchronized (Vector.this) {
1313 >                checkForComodification();
1314 >                int i = cursor - 1;
1315 >                if (i < 0)
1316 >                    throw new NoSuchElementException();
1317 >                cursor = i;
1318 >                return elementData(lastRet = i);
1319 >            }
1320 >        }
1321 >
1322 >        public void set(E e) {
1323 >            if (lastRet == -1)
1324 >                throw new IllegalStateException();
1325 >            synchronized (Vector.this) {
1326 >                checkForComodification();
1327 >                Vector.this.set(lastRet, e);
1328 >            }
1329 >        }
1330 >
1331 >        public void add(E e) {
1332 >            int i = cursor;
1333 >            synchronized (Vector.this) {
1334 >                checkForComodification();
1335 >                Vector.this.add(i, e);
1336 >                expectedModCount = modCount;
1337 >            }
1338 >            cursor = i + 1;
1339 >            lastRet = -1;
1340 >        }
1341 >    }
1342 >
1343 >    @Override
1344 >    public synchronized void forEach(Consumer<? super E> action) {
1345 >        Objects.requireNonNull(action);
1346 >        final int expectedModCount = modCount;
1347 >        final Object[] es = elementData;
1348 >        final int size = elementCount;
1349 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1350 >            action.accept(elementAt(es, i));
1351 >        if (modCount != expectedModCount)
1352 >            throw new ConcurrentModificationException();
1353 >    }
1354 >
1355 >    @Override
1356 >    public synchronized void replaceAll(UnaryOperator<E> operator) {
1357 >        Objects.requireNonNull(operator);
1358 >        final int expectedModCount = modCount;
1359 >        final Object[] es = elementData;
1360 >        final int size = elementCount;
1361 >        for (int i = 0; modCount == expectedModCount && i < size; i++)
1362 >            es[i] = operator.apply(elementAt(es, i));
1363 >        if (modCount != expectedModCount)
1364 >            throw new ConcurrentModificationException();
1365 >        modCount++;
1366 >    }
1367 >
1368 >    @SuppressWarnings("unchecked")
1369 >    @Override
1370 >    public synchronized void sort(Comparator<? super E> c) {
1371 >        final int expectedModCount = modCount;
1372 >        Arrays.sort((E[]) elementData, 0, elementCount, c);
1373 >        if (modCount != expectedModCount) {
1374 >            throw new ConcurrentModificationException();
1375 >        }
1376 >        modCount++;
1377 >    }
1378 >
1379 >    /**
1380 >     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
1381 >     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
1382 >     * list.
1383 >     *
1384 >     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
1385 >     * {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}.
1386 >     * Overriding implementations should document the reporting of additional
1387 >     * characteristic values.
1388 >     *
1389 >     * @return a {@code Spliterator} over the elements in this list
1390 >     * @since 1.8
1391 >     */
1392 >    @Override
1393 >    public Spliterator<E> spliterator() {
1394 >        return new VectorSpliterator<>(this, null, 0, -1, 0);
1395 >    }
1396 >
1397 >    /** Similar to ArrayList Spliterator */
1398 >    static final class VectorSpliterator<E> implements Spliterator<E> {
1399 >        private final Vector<E> list;
1400 >        private Object[] array;
1401 >        private int index; // current index, modified on advance/split
1402 >        private int fence; // -1 until used; then one past last index
1403 >        private int expectedModCount; // initialized when fence set
1404 >
1405 >        /** Create new spliterator covering the given range */
1406 >        VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1407 >                          int expectedModCount) {
1408 >            this.list = list;
1409 >            this.array = array;
1410 >            this.index = origin;
1411 >            this.fence = fence;
1412 >            this.expectedModCount = expectedModCount;
1413 >        }
1414 >
1415 >        private int getFence() { // initialize on first use
1416 >            int hi;
1417 >            if ((hi = fence) < 0) {
1418 >                synchronized (list) {
1419 >                    array = list.elementData;
1420 >                    expectedModCount = list.modCount;
1421 >                    hi = fence = list.elementCount;
1422 >                }
1423 >            }
1424 >            return hi;
1425 >        }
1426 >
1427 >        public Spliterator<E> trySplit() {
1428 >            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1429 >            return (lo >= mid) ? null :
1430 >                new VectorSpliterator<>(list, array, lo, index = mid,
1431 >                                        expectedModCount);
1432 >        }
1433 >
1434 >        @SuppressWarnings("unchecked")
1435 >        public boolean tryAdvance(Consumer<? super E> action) {
1436 >            int i;
1437 >            if (action == null)
1438 >                throw new NullPointerException();
1439 >            if (getFence() > (i = index)) {
1440 >                index = i + 1;
1441 >                action.accept((E)array[i]);
1442 >                if (list.modCount != expectedModCount)
1443 >                    throw new ConcurrentModificationException();
1444 >                return true;
1445 >            }
1446 >            return false;
1447 >        }
1448 >
1449 >        @SuppressWarnings("unchecked")
1450 >        public void forEachRemaining(Consumer<? super E> action) {
1451 >            int i, hi; // hoist accesses and checks from loop
1452 >            Vector<E> lst; Object[] a;
1453 >            if (action == null)
1454 >                throw new NullPointerException();
1455 >            if ((lst = list) != null) {
1456 >                if ((hi = fence) < 0) {
1457 >                    synchronized (lst) {
1458 >                        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();
1473 >        }
1474 >
1475 >        public long estimateSize() {
1476 >            return getFence() - index;
1477 >        }
1478 >
1479 >        public int characteristics() {
1480 >            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1481 >        }
1482      }
1483   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines