1 /* 2 * Copyright (c) 1994, 2019, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 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 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 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 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.io.IOException; 29 import java.io.ObjectInputStream; 30 import java.io.StreamCorruptedException; 31 import java.util.function.Consumer; 32 import java.util.function.Predicate; 33 import java.util.function.UnaryOperator; 34 35 import jdk.internal.util.ArraysSupport; 36 37 /** 38 * The {@code Vector} class implements a growable array of 39 * objects. Like an array, it contains components that can be 40 * accessed using an integer index. However, the size of a 41 * {@code Vector} can grow or shrink as needed to accommodate 42 * adding and removing items after the {@code Vector} has been created. 43 * 44 * <p>Each vector tries to optimize storage management by maintaining a 45 * {@code capacity} and a {@code capacityIncrement}. The 46 * {@code capacity} is always at least as large as the vector 47 * size; it is usually larger because as components are added to the 48 * vector, the vector's storage increases in chunks the size of 49 * {@code capacityIncrement}. An application can increase the 50 * capacity of a vector before inserting a large number of 51 * components; this reduces the amount of incremental reallocation. 52 * 53 * <p id="fail-fast"> 54 * The iterators returned by this class's {@link #iterator() iterator} and 55 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>: 56 * if the vector is structurally modified at any time after the iterator is 57 * created, in any way except through the iterator's own 58 * {@link ListIterator#remove() remove} or 59 * {@link ListIterator#add(Object) add} methods, the iterator will throw a 60 * {@link ConcurrentModificationException}. Thus, in the face of 61 * concurrent modification, the iterator fails quickly and cleanly, rather 62 * than risking arbitrary, non-deterministic behavior at an undetermined 63 * time in the future. The {@link Enumeration Enumerations} returned by 64 * the {@link #elements() elements} method are <em>not</em> fail-fast; if the 65 * Vector is structurally modified at any time after the enumeration is 66 * created then the results of enumerating are undefined. 67 * 68 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed 69 * as it is, generally speaking, impossible to make any hard guarantees in the 70 * presence of unsynchronized concurrent modification. Fail-fast iterators 71 * throw {@code ConcurrentModificationException} on a best-effort basis. 72 * Therefore, it would be wrong to write a program that depended on this 73 * exception for its correctness: <i>the fail-fast behavior of iterators 74 * should be used only to detect bugs.</i> 75 * 76 * <p>As of the Java 2 platform v1.2, this class was retrofitted to 77 * implement the {@link List} interface, making it a member of the 78 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> 79 * Java Collections Framework</a>. Unlike the new collection 80 * implementations, {@code Vector} is synchronized. If a thread-safe 81 * implementation is not needed, it is recommended to use {@link 82 * ArrayList} in place of {@code Vector}. 83 * 84 * @param <E> Type of component elements 85 * 86 * @author Lee Boynton 87 * @author Jonathan Payne 88 * @see Collection 89 * @see LinkedList 90 * @since 1.0 91 */ 92 public class Vector<E> 93 extends AbstractList<E> 94 implements List<E>, RandomAccess, Cloneable, java.io.Serializable 95 { 96 /** 97 * The array buffer into which the components of the vector are 98 * stored. The capacity of the vector is the length of this array buffer, 99 * and is at least large enough to contain all the vector's elements. 100 * 101 * <p>Any array elements following the last element in the Vector are null. 102 * 103 * @serial 104 */ 105 protected Object[] elementData; 106 107 /** 108 * The number of valid components in this {@code Vector} object. 109 * Components {@code elementData[0]} through 110 * {@code elementData[elementCount-1]} are the actual items. 111 * 112 * @serial 113 */ 114 protected int elementCount; 115 116 /** 117 * The amount by which the capacity of the vector is automatically 118 * incremented when its size becomes greater than its capacity. If 119 * the capacity increment is less than or equal to zero, the capacity 120 * of the vector is doubled each time it needs to grow. 121 * 122 * @serial 123 */ 124 protected int capacityIncrement; 125 126 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 127 @java.io.Serial 128 private static final long serialVersionUID = -2767605614048989439L; 129 130 /** 131 * Constructs an empty vector with the specified initial capacity and 132 * capacity increment. 133 * 134 * @param initialCapacity the initial capacity of the vector 135 * @param capacityIncrement the amount by which the capacity is 136 * increased when the vector overflows 137 * @throws IllegalArgumentException if the specified initial capacity 138 * is negative 139 */ 140 public Vector(int initialCapacity, int capacityIncrement) { 141 super(); 142 if (initialCapacity < 0) 143 throw new IllegalArgumentException("Illegal Capacity: "+ 144 initialCapacity); 145 this.elementData = new Object[initialCapacity]; 146 this.capacityIncrement = capacityIncrement; 147 } 148 149 /** 150 * Constructs an empty vector with the specified initial capacity and 151 * with its capacity increment equal to zero. 152 * 153 * @param initialCapacity the initial capacity of the vector 154 * @throws IllegalArgumentException if the specified initial capacity 155 * is negative 156 */ 157 public Vector(int initialCapacity) { 158 this(initialCapacity, 0); 159 } 160 161 /** 162 * Constructs an empty vector so that its internal data array 163 * has size {@code 10} and its standard capacity increment is 164 * zero. 165 */ 166 public Vector() { 167 this(10); 168 } 169 170 /** 171 * Constructs a vector containing the elements of the specified 172 * collection, in the order they are returned by the collection's 173 * iterator. 174 * 175 * @param c the collection whose elements are to be placed into this 176 * vector 177 * @throws NullPointerException if the specified collection is null 178 * @since 1.2 179 */ 180 public Vector(Collection<? extends E> c) { 181 elementData = c.toArray(); 182 elementCount = elementData.length; 183 // defend against c.toArray (incorrectly) not returning Object[] 184 // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) 185 if (elementData.getClass() != Object[].class) 186 elementData = Arrays.copyOf(elementData, elementCount, Object[].class); 187 } 188 189 /** 190 * Copies the components of this vector into the specified array. 191 * The item at index {@code k} in this vector is copied into 192 * component {@code k} of {@code anArray}. 193 * 194 * @param anArray the array into which the components get copied 195 * @throws NullPointerException if the given array is null 196 * @throws IndexOutOfBoundsException if the specified array is not 197 * large enough to hold all the components of this vector 198 * @throws ArrayStoreException if a component of this vector is not of 199 * a runtime type that can be stored in the specified array 200 * @see #toArray(Object[]) 201 */ 202 public synchronized void copyInto(Object[] anArray) { 203 System.arraycopy(elementData, 0, anArray, 0, elementCount); 204 } 205 206 /** 207 * Trims the capacity of this vector to be the vector's current 208 * size. If the capacity of this vector is larger than its current 209 * size, then the capacity is changed to equal the size by replacing 210 * its internal data array, kept in the field {@code elementData}, 211 * with a smaller one. An application can use this operation to 212 * minimize the storage of a vector. 213 */ 214 public synchronized void trimToSize() { 215 modCount++; 216 int oldCapacity = elementData.length; 217 if (elementCount < oldCapacity) { 218 elementData = Arrays.copyOf(elementData, elementCount); 219 } 220 } 221 222 /** 223 * Increases the capacity of this vector, if necessary, to ensure 224 * that it can hold at least the number of components specified by 225 * the minimum capacity argument. 226 * 227 * <p>If the current capacity of this vector is less than 228 * {@code minCapacity}, then its capacity is increased by replacing its 229 * internal data array, kept in the field {@code elementData}, with a 230 * larger one. The size of the new data array will be the old size plus 231 * {@code capacityIncrement}, unless the value of 232 * {@code capacityIncrement} is less than or equal to zero, in which case 233 * the new capacity will be twice the old capacity; but if this new size 234 * is still smaller than {@code minCapacity}, then the new capacity will 235 * be {@code minCapacity}. 236 * 237 * @param minCapacity the desired minimum capacity 238 */ 239 public synchronized void ensureCapacity(int minCapacity) { 240 if (minCapacity > 0) { 241 modCount++; 242 if (minCapacity > elementData.length) 243 grow(minCapacity); 244 } 245 } 246 247 /** 248 * Increases the capacity to ensure that it can hold at least the 249 * number of elements specified by the minimum capacity argument. 250 * 251 * @param minCapacity the desired minimum capacity 252 * @throws OutOfMemoryError if minCapacity is less than zero 253 */ 254 private Object[] grow(int minCapacity) { 255 int oldCapacity = elementData.length; 256 int newCapacity = ArraysSupport.newLength(oldCapacity, 257 minCapacity - oldCapacity, /* minimum growth */ 258 capacityIncrement > 0 ? capacityIncrement : oldCapacity 259 /* preferred growth */); 260 return elementData = Arrays.copyOf(elementData, newCapacity); 261 } 262 263 private Object[] grow() { 264 return grow(elementCount + 1); 265 } 266 267 /** 268 * Sets the size of this vector. If the new size is greater than the 269 * current size, new {@code null} items are added to the end of 270 * the vector. If the new size is less than the current size, all 271 * components at index {@code newSize} and greater are discarded. 272 * 273 * @param newSize the new size of this vector 274 * @throws ArrayIndexOutOfBoundsException if the new size is negative 275 */ 276 public synchronized void setSize(int newSize) { 277 modCount++; 278 if (newSize > elementData.length) 279 grow(newSize); 280 final Object[] es = elementData; 281 for (int to = elementCount, i = newSize; i < to; i++) 282 es[i] = null; 283 elementCount = newSize; 284 } 285 286 /** 287 * Returns the current capacity of this vector. 288 * 289 * @return the current capacity (the length of its internal 290 * data array, kept in the field {@code elementData} 291 * of this vector) 292 */ 293 public synchronized int capacity() { 294 return elementData.length; 295 } 296 297 /** 298 * Returns the number of components in this vector. 299 * 300 * @return the number of components in this vector 301 */ 302 public synchronized int size() { 303 return elementCount; 304 } 305 306 /** 307 * Tests if this vector has no components. 308 * 309 * @return {@code true} if and only if this vector has 310 * no components, that is, its size is zero; 311 * {@code false} otherwise. 312 */ 313 public synchronized boolean isEmpty() { 314 return elementCount == 0; 315 } 316 317 /** 318 * Returns an enumeration of the components of this vector. The 319 * returned {@code Enumeration} object will generate all items in 320 * this vector. The first item generated is the item at index {@code 0}, 321 * then the item at index {@code 1}, and so on. If the vector is 322 * structurally modified while enumerating over the elements then the 323 * results of enumerating are undefined. 324 * 325 * @return an enumeration of the components of this vector 326 * @see Iterator 327 */ 328 public Enumeration<E> elements() { 329 return new Enumeration<E>() { 330 int count = 0; 331 332 public boolean hasMoreElements() { 333 return count < elementCount; 334 } 335 336 public E nextElement() { 337 synchronized (Vector.this) { 338 if (count < elementCount) { 339 return elementData(count++); 340 } 341 } 342 throw new NoSuchElementException("Vector Enumeration"); 343 } 344 }; 345 } 346 347 /** 348 * Returns {@code true} if this vector contains the specified element. 349 * More formally, returns {@code true} if and only if this vector 350 * contains at least one element {@code e} such that 351 * {@code Objects.equals(o, e)}. 352 * 353 * @param o element whose presence in this vector is to be tested 354 * @return {@code true} if this vector contains the specified element 355 */ 356 public boolean contains(Object o) { 357 return indexOf(o, 0) >= 0; 358 } 359 360 /** 361 * Returns the index of the first occurrence of the specified element 362 * in this vector, or -1 if this vector does not contain the element. 363 * More formally, returns the lowest index {@code i} such that 364 * {@code Objects.equals(o, get(i))}, 365 * or -1 if there is no such index. 366 * 367 * @param o element to search for 368 * @return the index of the first occurrence of the specified element in 369 * this vector, or -1 if this vector does not contain the element 370 */ 371 public int indexOf(Object o) { 372 return indexOf(o, 0); 373 } 374 375 /** 376 * Returns the index of the first occurrence of the specified element in 377 * this vector, searching forwards from {@code index}, or returns -1 if 378 * the element is not found. 379 * More formally, returns the lowest index {@code i} such that 380 * {@code (i >= index && Objects.equals(o, get(i)))}, 381 * or -1 if there is no such index. 382 * 383 * @param o element to search for 384 * @param index index to start searching from 385 * @return the index of the first occurrence of the element in 386 * this vector at position {@code index} or later in the vector; 387 * {@code -1} if the element is not found. 388 * @throws IndexOutOfBoundsException if the specified index is negative 389 * @see Object#equals(Object) 390 */ 391 public synchronized int indexOf(Object o, int index) { 392 if (o == null) { 393 for (int i = index ; i < elementCount ; i++) 394 if (elementData[i]==null) 395 return i; 396 } else { 397 for (int i = index ; i < elementCount ; i++) 398 if (o.equals(elementData[i])) 399 return i; 400 } 401 return -1; 402 } 403 404 /** 405 * Returns the index of the last occurrence of the specified element 406 * in this vector, or -1 if this vector does not contain the element. 407 * More formally, returns the highest index {@code i} such that 408 * {@code Objects.equals(o, get(i))}, 409 * or -1 if there is no such index. 410 * 411 * @param o element to search for 412 * @return the index of the last occurrence of the specified element in 413 * this vector, or -1 if this vector does not contain the element 414 */ 415 public synchronized int lastIndexOf(Object o) { 416 return lastIndexOf(o, elementCount-1); 417 } 418 419 /** 420 * Returns the index of the last occurrence of the specified element in 421 * this vector, searching backwards from {@code index}, or returns -1 if 422 * the element is not found. 423 * More formally, returns the highest index {@code i} such that 424 * {@code (i <= index && Objects.equals(o, get(i)))}, 425 * or -1 if there is no such index. 426 * 427 * @param o element to search for 428 * @param index index to start searching backwards from 429 * @return the index of the last occurrence of the element at position 430 * less than or equal to {@code index} in this vector; 431 * -1 if the element is not found. 432 * @throws IndexOutOfBoundsException if the specified index is greater 433 * than or equal to the current size of this vector 434 */ 435 public synchronized int lastIndexOf(Object o, int index) { 436 if (index >= elementCount) 437 throw new IndexOutOfBoundsException(index + " >= "+ elementCount); 438 439 if (o == null) { 440 for (int i = index; i >= 0; i--) 441 if (elementData[i]==null) 442 return i; 443 } else { 444 for (int i = index; i >= 0; i--) 445 if (o.equals(elementData[i])) 446 return i; 447 } 448 return -1; 449 } 450 451 /** 452 * Returns the component at the specified index. 453 * 454 * <p>This method is identical in functionality to the {@link #get(int)} 455 * method (which is part of the {@link List} interface). 456 * 457 * @param index an index into this vector 458 * @return the component at the specified index 459 * @throws ArrayIndexOutOfBoundsException if the index is out of range 460 * ({@code index < 0 || index >= size()}) 461 */ 462 public synchronized E elementAt(int index) { 463 if (index >= elementCount) { 464 throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); 465 } 466 467 return elementData(index); 468 } 469 470 /** 471 * Returns the first component (the item at index {@code 0}) of 472 * this vector. 473 * 474 * @return the first component of this vector 475 * @throws NoSuchElementException if this vector has no components 476 */ 477 public synchronized E firstElement() { 478 if (elementCount == 0) { 479 throw new NoSuchElementException(); 480 } 481 return elementData(0); 482 } 483 484 /** 485 * Returns the last component of the vector. 486 * 487 * @return the last component of the vector, i.e., the component at index 488 * {@code size() - 1} 489 * @throws NoSuchElementException if this vector is empty 490 */ 491 public synchronized E lastElement() { 492 if (elementCount == 0) { 493 throw new NoSuchElementException(); 494 } 495 return elementData(elementCount - 1); 496 } 497 498 /** 499 * Sets the component at the specified {@code index} of this 500 * vector to be the specified object. The previous component at that 501 * position is discarded. 502 * 503 * <p>The index must be a value greater than or equal to {@code 0} 504 * and less than the current size of the vector. 505 * 506 * <p>This method is identical in functionality to the 507 * {@link #set(int, Object) set(int, E)} 508 * method (which is part of the {@link List} interface). Note that the 509 * {@code set} method reverses the order of the parameters, to more closely 510 * match array usage. Note also that the {@code set} method returns the 511 * old value that was stored at the specified position. 512 * 513 * @param obj what the component is to be set to 514 * @param index the specified index 515 * @throws ArrayIndexOutOfBoundsException if the index is out of range 516 * ({@code index < 0 || index >= size()}) 517 */ 518 public synchronized void setElementAt(E obj, int index) { 519 if (index >= elementCount) { 520 throw new ArrayIndexOutOfBoundsException(index + " >= " + 521 elementCount); 522 } 523 elementData[index] = obj; 524 } 525 526 /** 527 * Deletes the component at the specified index. Each component in 528 * this vector with an index greater or equal to the specified 529 * {@code index} is shifted downward to have an index one 530 * smaller than the value it had previously. The size of this vector 531 * is decreased by {@code 1}. 532 * 533 * <p>The index must be a value greater than or equal to {@code 0} 534 * and less than the current size of the vector. 535 * 536 * <p>This method is identical in functionality to the {@link #remove(int)} 537 * method (which is part of the {@link List} interface). Note that the 538 * {@code remove} method returns the old value that was stored at the 539 * specified position. 540 * 541 * @param index the index of the object to remove 542 * @throws ArrayIndexOutOfBoundsException if the index is out of range 543 * ({@code index < 0 || index >= size()}) 544 */ 545 public synchronized void removeElementAt(int index) { 546 if (index >= elementCount) { 547 throw new ArrayIndexOutOfBoundsException(index + " >= " + 548 elementCount); 549 } 550 else if (index < 0) { 551 throw new ArrayIndexOutOfBoundsException(index); 552 } 553 int j = elementCount - index - 1; 554 if (j > 0) { 555 System.arraycopy(elementData, index + 1, elementData, index, j); 556 } 557 modCount++; 558 elementCount--; 559 elementData[elementCount] = null; /* to let gc do its work */ 560 } 561 562 /** 563 * Inserts the specified object as a component in this vector at the 564 * specified {@code index}. Each component in this vector with 565 * an index greater or equal to the specified {@code index} is 566 * shifted upward to have an index one greater than the value it had 567 * previously. 568 * 569 * <p>The index must be a value greater than or equal to {@code 0} 570 * and less than or equal to the current size of the vector. (If the 571 * index is equal to the current size of the vector, the new element 572 * is appended to the Vector.) 573 * 574 * <p>This method is identical in functionality to the 575 * {@link #add(int, Object) add(int, E)} 576 * method (which is part of the {@link List} interface). Note that the 577 * {@code add} method reverses the order of the parameters, to more closely 578 * match array usage. 579 * 580 * @param obj the component to insert 581 * @param index where to insert the new component 582 * @throws ArrayIndexOutOfBoundsException if the index is out of range 583 * ({@code index < 0 || index > size()}) 584 */ 585 public synchronized void insertElementAt(E obj, int index) { 586 if (index > elementCount) { 587 throw new ArrayIndexOutOfBoundsException(index 588 + " > " + elementCount); 589 } 590 modCount++; 591 final int s = elementCount; 592 Object[] elementData = this.elementData; 593 if (s == elementData.length) 594 elementData = grow(); 595 System.arraycopy(elementData, index, 596 elementData, index + 1, 597 s - index); 598 elementData[index] = obj; 599 elementCount = s + 1; 600 } 601 602 /** 603 * Adds the specified component to the end of this vector, 604 * increasing its size by one. The capacity of this vector is 605 * increased if its size becomes greater than its capacity. 606 * 607 * <p>This method is identical in functionality to the 608 * {@link #add(Object) add(E)} 609 * method (which is part of the {@link List} interface). 610 * 611 * @param obj the component to be added 612 */ 613 public synchronized void addElement(E obj) { 614 modCount++; 615 add(obj, elementData, elementCount); 616 } 617 618 /** 619 * Removes the first (lowest-indexed) occurrence of the argument 620 * from this vector. If the object is found in this vector, each 621 * component in the vector with an index greater or equal to the 622 * object's index is shifted downward to have an index one smaller 623 * than the value it had previously. 624 * 625 * <p>This method is identical in functionality to the 626 * {@link #remove(Object)} method (which is part of the 627 * {@link List} interface). 628 * 629 * @param obj the component to be removed 630 * @return {@code true} if the argument was a component of this 631 * vector; {@code false} otherwise. 632 */ 633 public synchronized boolean removeElement(Object obj) { 634 modCount++; 635 int i = indexOf(obj); 636 if (i >= 0) { 637 removeElementAt(i); 638 return true; 639 } 640 return false; 641 } 642 643 /** 644 * Removes all components from this vector and sets its size to zero. 645 * 646 * <p>This method is identical in functionality to the {@link #clear} 647 * method (which is part of the {@link List} interface). 648 */ 649 public synchronized void removeAllElements() { 650 final Object[] es = elementData; 651 for (int to = elementCount, i = elementCount = 0; i < to; i++) 652 es[i] = null; 653 modCount++; 654 } 655 656 /** 657 * Returns a clone of this vector. The copy will contain a 658 * reference to a clone of the internal data array, not a reference 659 * to the original internal data array of this {@code Vector} object. 660 * 661 * @return a clone of this vector 662 */ 663 public synchronized Object clone() { 664 try { 665 @SuppressWarnings("unchecked") 666 Vector<E> v = (Vector<E>) super.clone(); 667 v.elementData = Arrays.copyOf(elementData, elementCount); 668 v.modCount = 0; 669 return v; 670 } catch (CloneNotSupportedException e) { 671 // this shouldn't happen, since we are Cloneable 672 throw new InternalError(e); 673 } 674 } 675 676 /** 677 * Returns an array containing all of the elements in this Vector 678 * in the correct order. 679 * 680 * @since 1.2 681 */ 682 public synchronized Object[] toArray() { 683 return Arrays.copyOf(elementData, elementCount); 684 } 685 686 /** 687 * Returns an array containing all of the elements in this Vector in the 688 * correct order; the runtime type of the returned array is that of the 689 * specified array. If the Vector fits in the specified array, it is 690 * returned therein. Otherwise, a new array is allocated with the runtime 691 * type of the specified array and the size of this Vector. 692 * 693 * <p>If the Vector fits in the specified array with room to spare 694 * (i.e., the array has more elements than the Vector), 695 * the element in the array immediately following the end of the 696 * Vector is set to null. (This is useful in determining the length 697 * of the Vector <em>only</em> if the caller knows that the Vector 698 * does not contain any null elements.) 699 * 700 * @param <T> type of array elements. The same type as {@code <E>} or a 701 * supertype of {@code <E>}. 702 * @param a the array into which the elements of the Vector are to 703 * be stored, if it is big enough; otherwise, a new array of the 704 * same runtime type is allocated for this purpose. 705 * @return an array containing the elements of the Vector 706 * @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not 707 * a supertype of the runtime type, {@code <E>}, of every element in this 708 * Vector 709 * @throws NullPointerException if the given array is null 710 * @since 1.2 711 */ 712 @SuppressWarnings("unchecked") 713 public synchronized <T> T[] toArray(T[] a) { 714 if (a.length < elementCount) 715 return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); 716 717 System.arraycopy(elementData, 0, a, 0, elementCount); 718 719 if (a.length > elementCount) 720 a[elementCount] = null; 721 722 return a; 723 } 724 725 // Positional Access Operations 726 727 @SuppressWarnings("unchecked") 728 E elementData(int index) { 729 return (E) elementData[index]; 730 } 731 732 @SuppressWarnings("unchecked") 733 static <E> E elementAt(Object[] es, int index) { 734 return (E) es[index]; 735 } 736 737 /** 738 * Returns the element at the specified position in this Vector. 739 * 740 * @param index index of the element to return 741 * @return object at the specified index 742 * @throws ArrayIndexOutOfBoundsException if the index is out of range 743 * ({@code index < 0 || index >= size()}) 744 * @since 1.2 745 */ 746 public synchronized E get(int index) { 747 if (index >= elementCount) 748 throw new ArrayIndexOutOfBoundsException(index); 749 750 return elementData(index); 751 } 752 753 /** 754 * Replaces the element at the specified position in this Vector with the 755 * specified element. 756 * 757 * @param index index of the element to replace 758 * @param element element to be stored at the specified position 759 * @return the element previously at the specified position 760 * @throws ArrayIndexOutOfBoundsException if the index is out of range 761 * ({@code index < 0 || index >= size()}) 762 * @since 1.2 763 */ 764 public synchronized E set(int index, E element) { 765 if (index >= elementCount) 766 throw new ArrayIndexOutOfBoundsException(index); 767 768 E oldValue = elementData(index); 769 elementData[index] = element; 770 return oldValue; 771 } 772 773 /** 774 * This helper method split out from add(E) to keep method 775 * bytecode size under 35 (the -XX:MaxInlineSize default value), 776 * which helps when add(E) is called in a C1-compiled loop. 777 */ 778 private void add(E e, Object[] elementData, int s) { 779 if (s == elementData.length) 780 elementData = grow(); 781 elementData[s] = e; 782 elementCount = s + 1; 783 } 784 785 /** 786 * Appends the specified element to the end of this Vector. 787 * 788 * @param e element to be appended to this Vector 789 * @return {@code true} (as specified by {@link Collection#add}) 790 * @since 1.2 791 */ 792 public synchronized boolean add(E e) { 793 modCount++; 794 add(e, elementData, elementCount); 795 return true; 796 } 797 798 /** 799 * Removes the first occurrence of the specified element in this Vector 800 * If the Vector does not contain the element, it is unchanged. More 801 * formally, removes the element with the lowest index i such that 802 * {@code Objects.equals(o, get(i))} (if such 803 * an element exists). 804 * 805 * @param o element to be removed from this Vector, if present 806 * @return true if the Vector contained the specified element 807 * @since 1.2 808 */ 809 public boolean remove(Object o) { 810 return removeElement(o); 811 } 812 813 /** 814 * Inserts the specified element at the specified position in this Vector. 815 * Shifts the element currently at that position (if any) and any 816 * subsequent elements to the right (adds one to their indices). 817 * 818 * @param index index at which the specified element is to be inserted 819 * @param element element to be inserted 820 * @throws ArrayIndexOutOfBoundsException if the index is out of range 821 * ({@code index < 0 || index > size()}) 822 * @since 1.2 823 */ 824 public void add(int index, E element) { 825 insertElementAt(element, index); 826 } 827 828 /** 829 * Removes the element at the specified position in this Vector. 830 * Shifts any subsequent elements to the left (subtracts one from their 831 * indices). Returns the element that was removed from the Vector. 832 * 833 * @param index the index of the element to be removed 834 * @return element that was removed 835 * @throws ArrayIndexOutOfBoundsException if the index is out of range 836 * ({@code index < 0 || index >= size()}) 837 * @since 1.2 838 */ 839 public synchronized E remove(int index) { 840 modCount++; 841 if (index >= elementCount) 842 throw new ArrayIndexOutOfBoundsException(index); 843 E oldValue = elementData(index); 844 845 int numMoved = elementCount - index - 1; 846 if (numMoved > 0) 847 System.arraycopy(elementData, index+1, elementData, index, 848 numMoved); 849 elementData[--elementCount] = null; // Let gc do its work 850 851 return oldValue; 852 } 853 854 /** 855 * Removes all of the elements from this Vector. The Vector will 856 * be empty after this call returns (unless it throws an exception). 857 * 858 * @since 1.2 859 */ 860 public void clear() { 861 removeAllElements(); 862 } 863 864 // Bulk Operations 865 866 /** 867 * Returns true if this Vector contains all of the elements in the 868 * specified Collection. 869 * 870 * @param c a collection whose elements will be tested for containment 871 * in this Vector 872 * @return true if this Vector contains all of the elements in the 873 * specified collection 874 * @throws NullPointerException if the specified collection is null 875 */ 876 public synchronized boolean containsAll(Collection<?> c) { 877 return super.containsAll(c); 878 } 879 880 /** 881 * Appends all of the elements in the specified Collection to the end of 882 * this Vector, in the order that they are returned by the specified 883 * Collection's Iterator. The behavior of this operation is undefined if 884 * the specified Collection is modified while the operation is in progress. 885 * (This implies that the behavior of this call is undefined if the 886 * specified Collection is this Vector, and this Vector is nonempty.) 887 * 888 * @param c elements to be inserted into this Vector 889 * @return {@code true} if this Vector changed as a result of the call 890 * @throws NullPointerException if the specified collection is null 891 * @since 1.2 892 */ 893 public boolean addAll(Collection<? extends E> c) { 894 Object[] a = c.toArray(); 895 modCount++; 896 int numNew = a.length; 897 if (numNew == 0) 898 return false; 899 synchronized (this) { 900 Object[] elementData = this.elementData; 901 final int s = elementCount; 902 if (numNew > elementData.length - s) 903 elementData = grow(s + numNew); 904 System.arraycopy(a, 0, elementData, s, numNew); 905 elementCount = s + numNew; 906 return true; 907 } 908 } 909 910 /** 911 * Removes from this Vector all of its elements that are contained in the 912 * specified Collection. 913 * 914 * @param c a collection of elements to be removed from the Vector 915 * @return true if this Vector changed as a result of the call 916 * @throws ClassCastException if the types of one or more elements 917 * in this vector are incompatible with the specified 918 * collection 919 * (<a href="Collection.html#optional-restrictions">optional</a>) 920 * @throws NullPointerException if this vector contains one or more null 921 * elements and the specified collection does not support null 922 * elements 923 * (<a href="Collection.html#optional-restrictions">optional</a>), 924 * or if the specified collection is null 925 * @since 1.2 926 */ 927 public boolean removeAll(Collection<?> c) { 928 Objects.requireNonNull(c); 929 return bulkRemove(e -> c.contains(e)); 930 } 931 932 /** 933 * Retains only the elements in this Vector that are contained in the 934 * specified Collection. In other words, removes from this Vector all 935 * of its elements that are not contained in the specified Collection. 936 * 937 * @param c a collection of elements to be retained in this Vector 938 * (all other elements are removed) 939 * @return true if this Vector changed as a result of the call 940 * @throws ClassCastException if the types of one or more elements 941 * in this vector are incompatible with the specified 942 * collection 943 * (<a href="Collection.html#optional-restrictions">optional</a>) 944 * @throws NullPointerException if this vector contains one or more null 945 * elements and the specified collection does not support null 946 * elements 947 * (<a href="Collection.html#optional-restrictions">optional</a>), 948 * or if the specified collection is null 949 * @since 1.2 950 */ 951 public boolean retainAll(Collection<?> c) { 952 Objects.requireNonNull(c); 953 return bulkRemove(e -> !c.contains(e)); 954 } 955 956 /** 957 * @throws NullPointerException {@inheritDoc} 958 */ 959 @Override 960 public boolean removeIf(Predicate<? super E> filter) { 961 Objects.requireNonNull(filter); 962 return bulkRemove(filter); 963 } 964 965 // A tiny bit set implementation 966 967 private static long[] nBits(int n) { 968 return new long[((n - 1) >> 6) + 1]; 969 } 970 private static void setBit(long[] bits, int i) { 971 bits[i >> 6] |= 1L << i; 972 } 973 private static boolean isClear(long[] bits, int i) { 974 return (bits[i >> 6] & (1L << i)) == 0; 975 } 976 977 private synchronized boolean bulkRemove(Predicate<? super E> filter) { 978 int expectedModCount = modCount; 979 final Object[] es = elementData; 980 final int end = elementCount; 981 int i; 982 // Optimize for initial run of survivors 983 for (i = 0; i < end && !filter.test(elementAt(es, i)); i++) 984 ; 985 // Tolerate predicates that reentrantly access the collection for 986 // read (but writers still get CME), so traverse once to find 987 // elements to delete, a second pass to physically expunge. 988 if (i < end) { 989 final int beg = i; 990 final long[] deathRow = nBits(end - beg); 991 deathRow[0] = 1L; // set bit 0 992 for (i = beg + 1; i < end; i++) 993 if (filter.test(elementAt(es, i))) 994 setBit(deathRow, i - beg); 995 if (modCount != expectedModCount) 996 throw new ConcurrentModificationException(); 997 modCount++; 998 int w = beg; 999 for (i = beg; i < end; i++) 1000 if (isClear(deathRow, i - beg)) 1001 es[w++] = es[i]; 1002 for (i = elementCount = w; i < end; i++) 1003 es[i] = null; 1004 return true; 1005 } else { 1006 if (modCount != expectedModCount) 1007 throw new ConcurrentModificationException(); 1008 return false; 1009 } 1010 } 1011 1012 /** 1013 * Inserts all of the elements in the specified Collection into this 1014 * Vector at the specified position. Shifts the element currently at 1015 * that position (if any) and any subsequent elements to the right 1016 * (increases their indices). The new elements will appear in the Vector 1017 * in the order that they are returned by the specified Collection's 1018 * iterator. 1019 * 1020 * @param index index at which to insert the first element from the 1021 * specified collection 1022 * @param c elements to be inserted into this Vector 1023 * @return {@code true} if this Vector changed as a result of the call 1024 * @throws ArrayIndexOutOfBoundsException if the index is out of range 1025 * ({@code index < 0 || index > size()}) 1026 * @throws NullPointerException if the specified collection is null 1027 * @since 1.2 1028 */ 1029 public synchronized boolean addAll(int index, Collection<? extends E> c) { 1030 if (index < 0 || index > elementCount) 1031 throw new ArrayIndexOutOfBoundsException(index); 1032 1033 Object[] a = c.toArray(); 1034 modCount++; 1035 int numNew = a.length; 1036 if (numNew == 0) 1037 return false; 1038 Object[] elementData = this.elementData; 1039 final int s = elementCount; 1040 if (numNew > elementData.length - s) 1041 elementData = grow(s + numNew); 1042 1043 int numMoved = s - index; 1044 if (numMoved > 0) 1045 System.arraycopy(elementData, index, 1046 elementData, index + numNew, 1047 numMoved); 1048 System.arraycopy(a, 0, elementData, index, numNew); 1049 elementCount = s + numNew; 1050 return true; 1051 } 1052 1053 /** 1054 * Compares the specified Object with this Vector for equality. Returns 1055 * true if and only if the specified Object is also a List, both Lists 1056 * have the same size, and all corresponding pairs of elements in the two 1057 * Lists are <em>equal</em>. (Two elements {@code e1} and 1058 * {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.) 1059 * In other words, two Lists are defined to be 1060 * equal if they contain the same elements in the same order. 1061 * 1062 * @param o the Object to be compared for equality with this Vector 1063 * @return true if the specified Object is equal to this Vector 1064 */ 1065 public synchronized boolean equals(Object o) { 1066 return super.equals(o); 1067 } 1068 1069 /** 1070 * Returns the hash code value for this Vector. 1071 */ 1072 public synchronized int hashCode() { 1073 return super.hashCode(); 1074 } 1075 1076 /** 1077 * Returns a string representation of this Vector, containing 1078 * the String representation of each element. 1079 */ 1080 public synchronized String toString() { 1081 return super.toString(); 1082 } 1083 1084 /** 1085 * Returns a view of the portion of this List between fromIndex, 1086 * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are 1087 * equal, the returned List is empty.) The returned List is backed by this 1088 * List, so changes in the returned List are reflected in this List, and 1089 * vice-versa. The returned List supports all of the optional List 1090 * operations supported by this List. 1091 * 1092 * <p>This method eliminates the need for explicit range operations (of 1093 * the sort that commonly exist for arrays). Any operation that expects 1094 * a List can be used as a range operation by operating on a subList view 1095 * instead of a whole List. For example, the following idiom 1096 * removes a range of elements from a List: 1097 * <pre> 1098 * list.subList(from, to).clear(); 1099 * </pre> 1100 * Similar idioms may be constructed for indexOf and lastIndexOf, 1101 * and all of the algorithms in the Collections class can be applied to 1102 * a subList. 1103 * 1104 * <p>The semantics of the List returned by this method become undefined if 1105 * the backing list (i.e., this List) is <i>structurally modified</i> in 1106 * any way other than via the returned List. (Structural modifications are 1107 * those that change the size of the List, or otherwise perturb it in such 1108 * a fashion that iterations in progress may yield incorrect results.) 1109 * 1110 * @param fromIndex low endpoint (inclusive) of the subList 1111 * @param toIndex high endpoint (exclusive) of the subList 1112 * @return a view of the specified range within this List 1113 * @throws IndexOutOfBoundsException if an endpoint index value is out of range 1114 * {@code (fromIndex < 0 || toIndex > size)} 1115 * @throws IllegalArgumentException if the endpoint indices are out of order 1116 * {@code (fromIndex > toIndex)} 1117 */ 1118 public synchronized List<E> subList(int fromIndex, int toIndex) { 1119 return Collections.synchronizedList(super.subList(fromIndex, toIndex), 1120 this); 1121 } 1122 1123 /** 1124 * Removes from this list all of the elements whose index is between 1125 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. 1126 * Shifts any succeeding elements to the left (reduces their index). 1127 * This call shortens the list by {@code (toIndex - fromIndex)} elements. 1128 * (If {@code toIndex==fromIndex}, this operation has no effect.) 1129 */ 1130 protected synchronized void removeRange(int fromIndex, int toIndex) { 1131 modCount++; 1132 shiftTailOverGap(elementData, fromIndex, toIndex); 1133 } 1134 1135 /** Erases the gap from lo to hi, by sliding down following elements. */ 1136 private void shiftTailOverGap(Object[] es, int lo, int hi) { 1137 System.arraycopy(es, hi, es, lo, elementCount - hi); 1138 for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) 1139 es[i] = null; 1140 } 1141 1142 /** 1143 * Loads a {@code Vector} instance from a stream 1144 * (that is, deserializes it). 1145 * This method performs checks to ensure the consistency 1146 * of the fields. 1147 * 1148 * @param in the stream 1149 * @throws java.io.IOException if an I/O error occurs 1150 * @throws ClassNotFoundException if the stream contains data 1151 * of a non-existing class 1152 */ 1153 @java.io.Serial 1154 private void readObject(ObjectInputStream in) 1155 throws IOException, ClassNotFoundException { 1156 ObjectInputStream.GetField gfields = in.readFields(); 1157 int count = gfields.get("elementCount", 0); 1158 Object[] data = (Object[])gfields.get("elementData", null); 1159 if (count < 0 || data == null || count > data.length) { 1160 throw new StreamCorruptedException("Inconsistent vector internals"); 1161 } 1162 elementCount = count; 1163 elementData = data.clone(); 1164 } 1165 1166 /** 1167 * Saves the state of the {@code Vector} instance to a stream 1168 * (that is, serializes it). 1169 * This method performs synchronization to ensure the consistency 1170 * of the serialized data. 1171 * 1172 * @param s the stream 1173 * @throws java.io.IOException if an I/O error occurs 1174 */ 1175 @java.io.Serial 1176 private void writeObject(java.io.ObjectOutputStream s) 1177 throws java.io.IOException { 1178 final java.io.ObjectOutputStream.PutField fields = s.putFields(); 1179 final Object[] data; 1180 synchronized (this) { 1181 fields.put("capacityIncrement", capacityIncrement); 1182 fields.put("elementCount", elementCount); 1183 data = elementData.clone(); 1184 } 1185 fields.put("elementData", data); 1186 s.writeFields(); 1187 } 1188 1189 /** 1190 * Returns a list iterator over the elements in this list (in proper 1191 * sequence), starting at the specified position in the list. 1192 * The specified index indicates the first element that would be 1193 * returned by an initial call to {@link ListIterator#next next}. 1194 * An initial call to {@link ListIterator#previous previous} would 1195 * return the element with the specified index minus one. 1196 * 1197 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 1198 * 1199 * @throws IndexOutOfBoundsException {@inheritDoc} 1200 */ 1201 public synchronized ListIterator<E> listIterator(int index) { 1202 if (index < 0 || index > elementCount) 1203 throw new IndexOutOfBoundsException("Index: "+index); 1204 return new ListItr(index); 1205 } 1206 1207 /** 1208 * Returns a list iterator over the elements in this list (in proper 1209 * sequence). 1210 * 1211 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 1212 * 1213 * @see #listIterator(int) 1214 */ 1215 public synchronized ListIterator<E> listIterator() { 1216 return new ListItr(0); 1217 } 1218 1219 /** 1220 * Returns an iterator over the elements in this list in proper sequence. 1221 * 1222 * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 1223 * 1224 * @return an iterator over the elements in this list in proper sequence 1225 */ 1226 public synchronized Iterator<E> iterator() { 1227 return new Itr(); 1228 } 1229 1230 /** 1231 * An optimized version of AbstractList.Itr 1232 */ 1233 private class Itr implements Iterator<E> { 1234 int cursor; // index of next element to return 1235 int lastRet = -1; // index of last element returned; -1 if no such 1236 int expectedModCount = modCount; 1237 1238 public boolean hasNext() { 1239 // Racy but within spec, since modifications are checked 1240 // within or after synchronization in next/previous 1241 return cursor != elementCount; 1242 } 1243 1244 public E next() { 1245 synchronized (Vector.this) { 1246 checkForComodification(); 1247 int i = cursor; 1248 if (i >= elementCount) 1249 throw new NoSuchElementException(); 1250 cursor = i + 1; 1251 return elementData(lastRet = i); 1252 } 1253 } 1254 1255 public void remove() { 1256 if (lastRet == -1) 1257 throw new IllegalStateException(); 1258 synchronized (Vector.this) { 1259 checkForComodification(); 1260 Vector.this.remove(lastRet); 1261 expectedModCount = modCount; 1262 } 1263 cursor = lastRet; 1264 lastRet = -1; 1265 } 1266 1267 @Override 1268 public void forEachRemaining(Consumer<? super E> action) { 1269 Objects.requireNonNull(action); 1270 synchronized (Vector.this) { 1271 final int size = elementCount; 1272 int i = cursor; 1273 if (i >= size) { 1274 return; 1275 } 1276 final Object[] es = elementData; 1277 if (i >= es.length) 1278 throw new ConcurrentModificationException(); 1279 while (i < size && modCount == expectedModCount) 1280 action.accept(elementAt(es, i++)); 1281 // update once at end of iteration to reduce heap write traffic 1282 cursor = i; 1283 lastRet = i - 1; 1284 checkForComodification(); 1285 } 1286 } 1287 1288 final void checkForComodification() { 1289 if (modCount != expectedModCount) 1290 throw new ConcurrentModificationException(); 1291 } 1292 } 1293 1294 /** 1295 * An optimized version of AbstractList.ListItr 1296 */ 1297 final class ListItr extends Itr implements ListIterator<E> { 1298 ListItr(int index) { 1299 super(); 1300 cursor = index; 1301 } 1302 1303 public boolean hasPrevious() { 1304 return cursor != 0; 1305 } 1306 1307 public int nextIndex() { 1308 return cursor; 1309 } 1310 1311 public int previousIndex() { 1312 return cursor - 1; 1313 } 1314 1315 public E previous() { 1316 synchronized (Vector.this) { 1317 checkForComodification(); 1318 int i = cursor - 1; 1319 if (i < 0) 1320 throw new NoSuchElementException(); 1321 cursor = i; 1322 return elementData(lastRet = i); 1323 } 1324 } 1325 1326 public void set(E e) { 1327 if (lastRet == -1) 1328 throw new IllegalStateException(); 1329 synchronized (Vector.this) { 1330 checkForComodification(); 1331 Vector.this.set(lastRet, e); 1332 } 1333 } 1334 1335 public void add(E e) { 1336 int i = cursor; 1337 synchronized (Vector.this) { 1338 checkForComodification(); 1339 Vector.this.add(i, e); 1340 expectedModCount = modCount; 1341 } 1342 cursor = i + 1; 1343 lastRet = -1; 1344 } 1345 } 1346 1347 /** 1348 * @throws NullPointerException {@inheritDoc} 1349 */ 1350 @Override 1351 public synchronized void forEach(Consumer<? super E> action) { 1352 Objects.requireNonNull(action); 1353 final int expectedModCount = modCount; 1354 final Object[] es = elementData; 1355 final int size = elementCount; 1356 for (int i = 0; modCount == expectedModCount && i < size; i++) 1357 action.accept(elementAt(es, i)); 1358 if (modCount != expectedModCount) 1359 throw new ConcurrentModificationException(); 1360 } 1361 1362 /** 1363 * @throws NullPointerException {@inheritDoc} 1364 */ 1365 @Override 1366 public synchronized void replaceAll(UnaryOperator<E> operator) { 1367 Objects.requireNonNull(operator); 1368 final int expectedModCount = modCount; 1369 final Object[] es = elementData; 1370 final int size = elementCount; 1371 for (int i = 0; modCount == expectedModCount && i < size; i++) 1372 es[i] = operator.apply(elementAt(es, i)); 1373 if (modCount != expectedModCount) 1374 throw new ConcurrentModificationException(); 1375 // TODO(8203662): remove increment of modCount from ... 1376 modCount++; 1377 } 1378 1379 @SuppressWarnings("unchecked") 1380 @Override 1381 public synchronized void sort(Comparator<? super E> c) { 1382 final int expectedModCount = modCount; 1383 Arrays.sort((E[]) elementData, 0, elementCount, c); 1384 if (modCount != expectedModCount) 1385 throw new ConcurrentModificationException(); 1386 modCount++; 1387 } 1388 1389 /** 1390 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> 1391 * and <em>fail-fast</em> {@link Spliterator} over the elements in this 1392 * list. 1393 * 1394 * <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, 1395 * {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}. 1396 * Overriding implementations should document the reporting of additional 1397 * characteristic values. 1398 * 1399 * @return a {@code Spliterator} over the elements in this list 1400 * @since 1.8 1401 */ 1402 @Override 1403 public Spliterator<E> spliterator() { 1404 return new VectorSpliterator(null, 0, -1, 0); 1405 } 1406 1407 /** Similar to ArrayList Spliterator */ 1408 final class VectorSpliterator implements Spliterator<E> { 1409 private Object[] array; 1410 private int index; // current index, modified on advance/split 1411 private int fence; // -1 until used; then one past last index 1412 private int expectedModCount; // initialized when fence set 1413 1414 /** Creates new spliterator covering the given range. */ 1415 VectorSpliterator(Object[] array, int origin, int fence, 1416 int expectedModCount) { 1417 this.array = array; 1418 this.index = origin; 1419 this.fence = fence; 1420 this.expectedModCount = expectedModCount; 1421 } 1422 1423 private int getFence() { // initialize on first use 1424 int hi; 1425 if ((hi = fence) < 0) { 1426 synchronized (Vector.this) { 1427 array = elementData; 1428 expectedModCount = modCount; 1429 hi = fence = elementCount; 1430 } 1431 } 1432 return hi; 1433 } 1434 1435 public Spliterator<E> trySplit() { 1436 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; 1437 return (lo >= mid) ? null : 1438 new VectorSpliterator(array, lo, index = mid, expectedModCount); 1439 } 1440 1441 @SuppressWarnings("unchecked") 1442 public boolean tryAdvance(Consumer<? super E> action) { 1443 Objects.requireNonNull(action); 1444 int i; 1445 if (getFence() > (i = index)) { 1446 index = i + 1; 1447 action.accept((E)array[i]); 1448 if (modCount != expectedModCount) 1449 throw new ConcurrentModificationException(); 1450 return true; 1451 } 1452 return false; 1453 } 1454 1455 @SuppressWarnings("unchecked") 1456 public void forEachRemaining(Consumer<? super E> action) { 1457 Objects.requireNonNull(action); 1458 final int hi = getFence(); 1459 final Object[] a = array; 1460 int i; 1461 for (i = index, index = hi; i < hi; i++) 1462 action.accept((E) a[i]); 1463 if (modCount != expectedModCount) 1464 throw new ConcurrentModificationException(); 1465 } 1466 1467 public long estimateSize() { 1468 return getFence() - index; 1469 } 1470 1471 public int characteristics() { 1472 return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; 1473 } 1474 } 1475 1476 void checkInvariants() { 1477 // assert elementCount >= 0; 1478 // assert elementCount == elementData.length || elementData[elementCount] == null; 1479 } 1480 }