Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/util/Vector.java
+++ new/src/share/classes/java/util/Vector.java
1 1 /*
2 2 * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package java.util;
27 27
28 28 /**
29 29 * The {@code Vector} class implements a growable array of
30 30 * objects. Like an array, it contains components that can be
31 31 * accessed using an integer index. However, the size of a
32 32 * {@code Vector} can grow or shrink as needed to accommodate
33 33 * adding and removing items after the {@code Vector} has been created.
34 34 *
35 35 * <p>Each vector tries to optimize storage management by maintaining a
36 36 * {@code capacity} and a {@code capacityIncrement}. The
37 37 * {@code capacity} is always at least as large as the vector
38 38 * size; it is usually larger because as components are added to the
39 39 * vector, the vector's storage increases in chunks the size of
40 40 * {@code capacityIncrement}. An application can increase the
41 41 * capacity of a vector before inserting a large number of
42 42 * components; this reduces the amount of incremental reallocation.
43 43 *
44 44 * <p><a name="fail-fast"/>
45 45 * The iterators returned by this class's {@link #iterator() iterator} and
46 46 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
47 47 * if the vector is structurally modified at any time after the iterator is
48 48 * created, in any way except through the iterator's own
49 49 * {@link ListIterator#remove() remove} or
50 50 * {@link ListIterator#add(Object) add} methods, the iterator will throw a
51 51 * {@link ConcurrentModificationException}. Thus, in the face of
52 52 * concurrent modification, the iterator fails quickly and cleanly, rather
53 53 * than risking arbitrary, non-deterministic behavior at an undetermined
54 54 * time in the future. The {@link Enumeration Enumerations} returned by
55 55 * the {@link #elements() elements} method are <em>not</em> fail-fast.
56 56 *
57 57 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
58 58 * as it is, generally speaking, impossible to make any hard guarantees in the
59 59 * presence of unsynchronized concurrent modification. Fail-fast iterators
60 60 * throw {@code ConcurrentModificationException} on a best-effort basis.
61 61 * Therefore, it would be wrong to write a program that depended on this
62 62 * exception for its correctness: <i>the fail-fast behavior of iterators
63 63 * should be used only to detect bugs.</i>
64 64 *
65 65 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
66 66 * implement the {@link List} interface, making it a member of the
67 67 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
68 68 * Java Collections Framework</a>. Unlike the new collection
69 69 * implementations, {@code Vector} is synchronized. If a thread-safe
70 70 * implementation is not needed, it is recommended to use {@link
71 71 * ArrayList} in place of {@code Vector}.
72 72 *
73 73 * @author Lee Boynton
74 74 * @author Jonathan Payne
75 75 * @see Collection
76 76 * @see LinkedList
77 77 * @since JDK1.0
78 78 */
79 79 public class Vector<E>
80 80 extends AbstractList<E>
81 81 implements List<E>, RandomAccess, Cloneable, java.io.Serializable
82 82 {
83 83 /**
84 84 * The array buffer into which the components of the vector are
85 85 * stored. The capacity of the vector is the length of this array buffer,
86 86 * and is at least large enough to contain all the vector's elements.
87 87 *
88 88 * <p>Any array elements following the last element in the Vector are null.
89 89 *
90 90 * @serial
91 91 */
92 92 protected Object[] elementData;
93 93
94 94 /**
95 95 * The number of valid components in this {@code Vector} object.
96 96 * Components {@code elementData[0]} through
97 97 * {@code elementData[elementCount-1]} are the actual items.
98 98 *
99 99 * @serial
100 100 */
101 101 protected int elementCount;
102 102
103 103 /**
104 104 * The amount by which the capacity of the vector is automatically
105 105 * incremented when its size becomes greater than its capacity. If
106 106 * the capacity increment is less than or equal to zero, the capacity
107 107 * of the vector is doubled each time it needs to grow.
108 108 *
109 109 * @serial
110 110 */
111 111 protected int capacityIncrement;
112 112
113 113 /** use serialVersionUID from JDK 1.0.2 for interoperability */
114 114 private static final long serialVersionUID = -2767605614048989439L;
115 115
116 116 /**
117 117 * Constructs an empty vector with the specified initial capacity and
118 118 * capacity increment.
119 119 *
120 120 * @param initialCapacity the initial capacity of the vector
121 121 * @param capacityIncrement the amount by which the capacity is
122 122 * increased when the vector overflows
123 123 * @throws IllegalArgumentException if the specified initial capacity
124 124 * is negative
125 125 */
126 126 public Vector(int initialCapacity, int capacityIncrement) {
127 127 super();
128 128 if (initialCapacity < 0)
129 129 throw new IllegalArgumentException("Illegal Capacity: "+
130 130 initialCapacity);
131 131 this.elementData = new Object[initialCapacity];
132 132 this.capacityIncrement = capacityIncrement;
133 133 }
134 134
135 135 /**
136 136 * Constructs an empty vector with the specified initial capacity and
137 137 * with its capacity increment equal to zero.
138 138 *
139 139 * @param initialCapacity the initial capacity of the vector
140 140 * @throws IllegalArgumentException if the specified initial capacity
141 141 * is negative
142 142 */
143 143 public Vector(int initialCapacity) {
144 144 this(initialCapacity, 0);
145 145 }
146 146
147 147 /**
148 148 * Constructs an empty vector so that its internal data array
149 149 * has size {@code 10} and its standard capacity increment is
150 150 * zero.
151 151 */
152 152 public Vector() {
153 153 this(10);
154 154 }
155 155
156 156 /**
157 157 * Constructs a vector containing the elements of the specified
158 158 * collection, in the order they are returned by the collection's
159 159 * iterator.
160 160 *
161 161 * @param c the collection whose elements are to be placed into this
162 162 * vector
163 163 * @throws NullPointerException if the specified collection is null
164 164 * @since 1.2
165 165 */
166 166 public Vector(Collection<? extends E> c) {
167 167 elementData = c.toArray();
168 168 elementCount = elementData.length;
169 169 // c.toArray might (incorrectly) not return Object[] (see 6260652)
170 170 if (elementData.getClass() != Object[].class)
171 171 elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
172 172 }
173 173
174 174 /**
175 175 * Copies the components of this vector into the specified array.
176 176 * The item at index {@code k} in this vector is copied into
177 177 * component {@code k} of {@code anArray}.
178 178 *
179 179 * @param anArray the array into which the components get copied
180 180 * @throws NullPointerException if the given array is null
181 181 * @throws IndexOutOfBoundsException if the specified array is not
182 182 * large enough to hold all the components of this vector
183 183 * @throws ArrayStoreException if a component of this vector is not of
184 184 * a runtime type that can be stored in the specified array
185 185 * @see #toArray(Object[])
186 186 */
187 187 public synchronized void copyInto(Object[] anArray) {
188 188 System.arraycopy(elementData, 0, anArray, 0, elementCount);
189 189 }
190 190
191 191 /**
192 192 * Trims the capacity of this vector to be the vector's current
193 193 * size. If the capacity of this vector is larger than its current
194 194 * size, then the capacity is changed to equal the size by replacing
195 195 * its internal data array, kept in the field {@code elementData},
196 196 * with a smaller one. An application can use this operation to
197 197 * minimize the storage of a vector.
198 198 */
199 199 public synchronized void trimToSize() {
200 200 modCount++;
201 201 int oldCapacity = elementData.length;
202 202 if (elementCount < oldCapacity) {
203 203 elementData = Arrays.copyOf(elementData, elementCount);
204 204 }
205 205 }
206 206
207 207 /**
208 208 * Increases the capacity of this vector, if necessary, to ensure
209 209 * that it can hold at least the number of components specified by
210 210 * the minimum capacity argument.
211 211 *
212 212 * <p>If the current capacity of this vector is less than
213 213 * {@code minCapacity}, then its capacity is increased by replacing its
214 214 * internal data array, kept in the field {@code elementData}, with a
215 215 * larger one. The size of the new data array will be the old size plus
216 216 * {@code capacityIncrement}, unless the value of
217 217 * {@code capacityIncrement} is less than or equal to zero, in which case
218 218 * the new capacity will be twice the old capacity; but if this new size
219 219 * is still smaller than {@code minCapacity}, then the new capacity will
220 220 * be {@code minCapacity}.
221 221 *
222 222 * @param minCapacity the desired minimum capacity
223 223 */
224 224 public synchronized void ensureCapacity(int minCapacity) {
225 225 if (minCapacity > 0) {
226 226 modCount++;
227 227 ensureCapacityHelper(minCapacity);
228 228 }
229 229 }
230 230
231 231 /**
232 232 * This implements the unsynchronized semantics of ensureCapacity.
233 233 * Synchronized methods in this class can internally call this
234 234 * method for ensuring capacity without incurring the cost of an
235 235 * extra synchronization.
236 236 *
237 237 * @see #ensureCapacity(int)
238 238 */
239 239 private void ensureCapacityHelper(int minCapacity) {
240 240 // overflow-conscious code
241 241 if (minCapacity - elementData.length > 0)
242 242 grow(minCapacity);
243 243 }
244 244
245 245 /**
246 246 * The maximum size of array to allocate.
247 247 * Some VMs reserve some header words in an array.
248 248 * Attempts to allocate larger arrays may result in
249 249 * OutOfMemoryError: Requested array size exceeds VM limit
250 250 */
251 251 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
252 252
253 253 private void grow(int minCapacity) {
254 254 // overflow-conscious code
255 255 int oldCapacity = elementData.length;
256 256 int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
257 257 capacityIncrement : oldCapacity);
258 258 if (newCapacity - minCapacity < 0)
259 259 newCapacity = minCapacity;
260 260 if (newCapacity - MAX_ARRAY_SIZE > 0)
261 261 newCapacity = hugeCapacity(minCapacity);
262 262 elementData = Arrays.copyOf(elementData, newCapacity);
263 263 }
264 264
265 265 private static int hugeCapacity(int minCapacity) {
266 266 if (minCapacity < 0) // overflow
267 267 throw new OutOfMemoryError();
268 268 return (minCapacity > MAX_ARRAY_SIZE) ?
269 269 Integer.MAX_VALUE :
270 270 MAX_ARRAY_SIZE;
271 271 }
272 272
273 273 /**
274 274 * Sets the size of this vector. If the new size is greater than the
275 275 * current size, new {@code null} items are added to the end of
276 276 * the vector. If the new size is less than the current size, all
277 277 * components at index {@code newSize} and greater are discarded.
278 278 *
279 279 * @param newSize the new size of this vector
280 280 * @throws ArrayIndexOutOfBoundsException if the new size is negative
281 281 */
282 282 public synchronized void setSize(int newSize) {
283 283 modCount++;
284 284 if (newSize > elementCount) {
285 285 ensureCapacityHelper(newSize);
286 286 } else {
287 287 for (int i = newSize ; i < elementCount ; i++) {
288 288 elementData[i] = null;
289 289 }
290 290 }
291 291 elementCount = newSize;
292 292 }
293 293
294 294 /**
295 295 * Returns the current capacity of this vector.
296 296 *
297 297 * @return the current capacity (the length of its internal
298 298 * data array, kept in the field {@code elementData}
299 299 * of this vector)
300 300 */
301 301 public synchronized int capacity() {
302 302 return elementData.length;
303 303 }
304 304
305 305 /**
306 306 * Returns the number of components in this vector.
307 307 *
308 308 * @return the number of components in this vector
309 309 */
310 310 public synchronized int size() {
311 311 return elementCount;
312 312 }
313 313
314 314 /**
315 315 * Tests if this vector has no components.
316 316 *
317 317 * @return {@code true} if and only if this vector has
318 318 * no components, that is, its size is zero;
319 319 * {@code false} otherwise.
320 320 */
321 321 public synchronized boolean isEmpty() {
322 322 return elementCount == 0;
323 323 }
324 324
325 325 /**
326 326 * Returns an enumeration of the components of this vector. The
327 327 * returned {@code Enumeration} object will generate all items in
328 328 * this vector. The first item generated is the item at index {@code 0},
329 329 * then the item at index {@code 1}, and so on.
330 330 *
331 331 * @return an enumeration of the components of this vector
332 332 * @see Iterator
333 333 */
334 334 public Enumeration<E> elements() {
335 335 return new Enumeration<E>() {
336 336 int count = 0;
337 337
338 338 public boolean hasMoreElements() {
339 339 return count < elementCount;
340 340 }
341 341
342 342 public E nextElement() {
343 343 synchronized (Vector.this) {
344 344 if (count < elementCount) {
345 345 return elementData(count++);
346 346 }
347 347 }
348 348 throw new NoSuchElementException("Vector Enumeration");
349 349 }
350 350 };
351 351 }
352 352
353 353 /**
354 354 * Returns {@code true} if this vector contains the specified element.
355 355 * More formally, returns {@code true} if and only if this vector
356 356 * contains at least one element {@code e} such that
357 357 * <tt>(o==null ? e==null : o.equals(e))</tt>.
358 358 *
359 359 * @param o element whose presence in this vector is to be tested
360 360 * @return {@code true} if this vector contains the specified element
361 361 */
362 362 public boolean contains(Object o) {
363 363 return indexOf(o, 0) >= 0;
364 364 }
365 365
366 366 /**
367 367 * Returns the index of the first occurrence of the specified element
368 368 * in this vector, or -1 if this vector does not contain the element.
369 369 * More formally, returns the lowest index {@code i} such that
370 370 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
371 371 * or -1 if there is no such index.
372 372 *
373 373 * @param o element to search for
374 374 * @return the index of the first occurrence of the specified element in
375 375 * this vector, or -1 if this vector does not contain the element
376 376 */
377 377 public int indexOf(Object o) {
378 378 return indexOf(o, 0);
379 379 }
380 380
381 381 /**
382 382 * Returns the index of the first occurrence of the specified element in
383 383 * this vector, searching forwards from {@code index}, or returns -1 if
384 384 * the element is not found.
385 385 * More formally, returns the lowest index {@code i} such that
386 386 * <tt>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>,
387 387 * or -1 if there is no such index.
388 388 *
389 389 * @param o element to search for
390 390 * @param index index to start searching from
391 391 * @return the index of the first occurrence of the element in
392 392 * this vector at position {@code index} or later in the vector;
393 393 * {@code -1} if the element is not found.
394 394 * @throws IndexOutOfBoundsException if the specified index is negative
395 395 * @see Object#equals(Object)
396 396 */
397 397 public synchronized int indexOf(Object o, int index) {
398 398 if (o == null) {
399 399 for (int i = index ; i < elementCount ; i++)
400 400 if (elementData[i]==null)
401 401 return i;
402 402 } else {
403 403 for (int i = index ; i < elementCount ; i++)
404 404 if (o.equals(elementData[i]))
405 405 return i;
406 406 }
407 407 return -1;
408 408 }
409 409
410 410 /**
411 411 * Returns the index of the last occurrence of the specified element
412 412 * in this vector, or -1 if this vector does not contain the element.
413 413 * More formally, returns the highest index {@code i} such that
414 414 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
415 415 * or -1 if there is no such index.
416 416 *
417 417 * @param o element to search for
418 418 * @return the index of the last occurrence of the specified element in
419 419 * this vector, or -1 if this vector does not contain the element
420 420 */
421 421 public synchronized int lastIndexOf(Object o) {
422 422 return lastIndexOf(o, elementCount-1);
423 423 }
424 424
425 425 /**
426 426 * Returns the index of the last occurrence of the specified element in
427 427 * this vector, searching backwards from {@code index}, or returns -1 if
428 428 * the element is not found.
429 429 * More formally, returns the highest index {@code i} such that
430 430 * <tt>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>,
431 431 * or -1 if there is no such index.
432 432 *
433 433 * @param o element to search for
434 434 * @param index index to start searching backwards from
435 435 * @return the index of the last occurrence of the element at position
436 436 * less than or equal to {@code index} in this vector;
437 437 * -1 if the element is not found.
438 438 * @throws IndexOutOfBoundsException if the specified index is greater
439 439 * than or equal to the current size of this vector
440 440 */
441 441 public synchronized int lastIndexOf(Object o, int index) {
442 442 if (index >= elementCount)
443 443 throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
444 444
445 445 if (o == null) {
446 446 for (int i = index; i >= 0; i--)
447 447 if (elementData[i]==null)
448 448 return i;
449 449 } else {
450 450 for (int i = index; i >= 0; i--)
451 451 if (o.equals(elementData[i]))
452 452 return i;
453 453 }
454 454 return -1;
455 455 }
456 456
457 457 /**
458 458 * Returns the component at the specified index.
459 459 *
460 460 * <p>This method is identical in functionality to the {@link #get(int)}
461 461 * method (which is part of the {@link List} interface).
462 462 *
463 463 * @param index an index into this vector
464 464 * @return the component at the specified index
465 465 * @throws ArrayIndexOutOfBoundsException if the index is out of range
466 466 * ({@code index < 0 || index >= size()})
467 467 */
468 468 public synchronized E elementAt(int index) {
469 469 if (index >= elementCount) {
470 470 throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
471 471 }
472 472
473 473 return elementData(index);
474 474 }
475 475
476 476 /**
477 477 * Returns the first component (the item at index {@code 0}) of
478 478 * this vector.
479 479 *
480 480 * @return the first component of this vector
481 481 * @throws NoSuchElementException if this vector has no components
482 482 */
483 483 public synchronized E firstElement() {
484 484 if (elementCount == 0) {
485 485 throw new NoSuchElementException();
486 486 }
487 487 return elementData(0);
488 488 }
489 489
490 490 /**
491 491 * Returns the last component of the vector.
492 492 *
493 493 * @return the last component of the vector, i.e., the component at index
494 494 * <code>size() - 1</code>.
495 495 * @throws NoSuchElementException if this vector is empty
496 496 */
497 497 public synchronized E lastElement() {
498 498 if (elementCount == 0) {
499 499 throw new NoSuchElementException();
500 500 }
501 501 return elementData(elementCount - 1);
502 502 }
503 503
504 504 /**
505 505 * Sets the component at the specified {@code index} of this
506 506 * vector to be the specified object. The previous component at that
507 507 * position is discarded.
508 508 *
509 509 * <p>The index must be a value greater than or equal to {@code 0}
510 510 * and less than the current size of the vector.
511 511 *
512 512 * <p>This method is identical in functionality to the
513 513 * {@link #set(int, Object) set(int, E)}
514 514 * method (which is part of the {@link List} interface). Note that the
515 515 * {@code set} method reverses the order of the parameters, to more closely
516 516 * match array usage. Note also that the {@code set} method returns the
517 517 * old value that was stored at the specified position.
518 518 *
519 519 * @param obj what the component is to be set to
520 520 * @param index the specified index
521 521 * @throws ArrayIndexOutOfBoundsException if the index is out of range
522 522 * ({@code index < 0 || index >= size()})
523 523 */
524 524 public synchronized void setElementAt(E obj, int index) {
525 525 if (index >= elementCount) {
526 526 throw new ArrayIndexOutOfBoundsException(index + " >= " +
527 527 elementCount);
528 528 }
529 529 elementData[index] = obj;
530 530 }
531 531
532 532 /**
533 533 * Deletes the component at the specified index. Each component in
534 534 * this vector with an index greater or equal to the specified
535 535 * {@code index} is shifted downward to have an index one
536 536 * smaller than the value it had previously. The size of this vector
537 537 * is decreased by {@code 1}.
538 538 *
539 539 * <p>The index must be a value greater than or equal to {@code 0}
540 540 * and less than the current size of the vector.
541 541 *
542 542 * <p>This method is identical in functionality to the {@link #remove(int)}
543 543 * method (which is part of the {@link List} interface). Note that the
544 544 * {@code remove} method returns the old value that was stored at the
545 545 * specified position.
546 546 *
547 547 * @param index the index of the object to remove
548 548 * @throws ArrayIndexOutOfBoundsException if the index is out of range
549 549 * ({@code index < 0 || index >= size()})
550 550 */
551 551 public synchronized void removeElementAt(int index) {
552 552 modCount++;
553 553 if (index >= elementCount) {
554 554 throw new ArrayIndexOutOfBoundsException(index + " >= " +
555 555 elementCount);
556 556 }
557 557 else if (index < 0) {
558 558 throw new ArrayIndexOutOfBoundsException(index);
559 559 }
560 560 int j = elementCount - index - 1;
561 561 if (j > 0) {
562 562 System.arraycopy(elementData, index + 1, elementData, index, j);
563 563 }
564 564 elementCount--;
565 565 elementData[elementCount] = null; /* to let gc do its work */
566 566 }
567 567
568 568 /**
569 569 * Inserts the specified object as a component in this vector at the
570 570 * specified {@code index}. Each component in this vector with
571 571 * an index greater or equal to the specified {@code index} is
572 572 * shifted upward to have an index one greater than the value it had
573 573 * previously.
574 574 *
575 575 * <p>The index must be a value greater than or equal to {@code 0}
576 576 * and less than or equal to the current size of the vector. (If the
577 577 * index is equal to the current size of the vector, the new element
578 578 * is appended to the Vector.)
579 579 *
580 580 * <p>This method is identical in functionality to the
581 581 * {@link #add(int, Object) add(int, E)}
582 582 * method (which is part of the {@link List} interface). Note that the
583 583 * {@code add} method reverses the order of the parameters, to more closely
584 584 * match array usage.
585 585 *
586 586 * @param obj the component to insert
587 587 * @param index where to insert the new component
588 588 * @throws ArrayIndexOutOfBoundsException if the index is out of range
589 589 * ({@code index < 0 || index > size()})
590 590 */
591 591 public synchronized void insertElementAt(E obj, int index) {
592 592 modCount++;
593 593 if (index > elementCount) {
594 594 throw new ArrayIndexOutOfBoundsException(index
595 595 + " > " + elementCount);
596 596 }
597 597 ensureCapacityHelper(elementCount + 1);
598 598 System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
599 599 elementData[index] = obj;
600 600 elementCount++;
601 601 }
602 602
603 603 /**
604 604 * Adds the specified component to the end of this vector,
605 605 * increasing its size by one. The capacity of this vector is
606 606 * increased if its size becomes greater than its capacity.
607 607 *
608 608 * <p>This method is identical in functionality to the
609 609 * {@link #add(Object) add(E)}
610 610 * method (which is part of the {@link List} interface).
611 611 *
612 612 * @param obj the component to be added
613 613 */
614 614 public synchronized void addElement(E obj) {
615 615 modCount++;
616 616 ensureCapacityHelper(elementCount + 1);
617 617 elementData[elementCount++] = obj;
618 618 }
619 619
620 620 /**
621 621 * Removes the first (lowest-indexed) occurrence of the argument
622 622 * from this vector. If the object is found in this vector, each
623 623 * component in the vector with an index greater or equal to the
624 624 * object's index is shifted downward to have an index one smaller
625 625 * than the value it had previously.
626 626 *
627 627 * <p>This method is identical in functionality to the
628 628 * {@link #remove(Object)} method (which is part of the
629 629 * {@link List} interface).
630 630 *
631 631 * @param obj the component to be removed
632 632 * @return {@code true} if the argument was a component of this
633 633 * vector; {@code false} otherwise.
634 634 */
635 635 public synchronized boolean removeElement(Object obj) {
636 636 modCount++;
637 637 int i = indexOf(obj);
638 638 if (i >= 0) {
639 639 removeElementAt(i);
640 640 return true;
641 641 }
642 642 return false;
643 643 }
644 644
645 645 /**
646 646 * Removes all components from this vector and sets its size to zero.
647 647 *
648 648 * <p>This method is identical in functionality to the {@link #clear}
649 649 * method (which is part of the {@link List} interface).
650 650 */
651 651 public synchronized void removeAllElements() {
652 652 modCount++;
653 653 // Let gc do its work
654 654 for (int i = 0; i < elementCount; i++)
655 655 elementData[i] = null;
656 656
657 657 elementCount = 0;
658 658 }
659 659
660 660 /**
661 661 * Returns a clone of this vector. The copy will contain a
662 662 * reference to a clone of the internal data array, not a reference
663 663 * to the original internal data array of this {@code Vector} object.
664 664 *
665 665 * @return a clone of this vector
666 666 */
667 667 public synchronized Object clone() {
668 668 try {
669 669 @SuppressWarnings("unchecked")
670 670 Vector<E> v = (Vector<E>) super.clone();
671 671 v.elementData = Arrays.copyOf(elementData, elementCount);
672 672 v.modCount = 0;
673 673 return v;
674 674 } catch (CloneNotSupportedException e) {
675 675 // this shouldn't happen, since we are Cloneable
676 676 throw new InternalError();
677 677 }
678 678 }
679 679
680 680 /**
681 681 * Returns an array containing all of the elements in this Vector
682 682 * in the correct order.
683 683 *
684 684 * @since 1.2
685 685 */
686 686 public synchronized Object[] toArray() {
687 687 return Arrays.copyOf(elementData, elementCount);
688 688 }
689 689
690 690 /**
691 691 * Returns an array containing all of the elements in this Vector in the
692 692 * correct order; the runtime type of the returned array is that of the
693 693 * specified array. If the Vector fits in the specified array, it is
694 694 * returned therein. Otherwise, a new array is allocated with the runtime
695 695 * type of the specified array and the size of this Vector.
696 696 *
697 697 * <p>If the Vector fits in the specified array with room to spare
698 698 * (i.e., the array has more elements than the Vector),
699 699 * the element in the array immediately following the end of the
700 700 * Vector is set to null. (This is useful in determining the length
701 701 * of the Vector <em>only</em> if the caller knows that the Vector
702 702 * does not contain any null elements.)
703 703 *
704 704 * @param a the array into which the elements of the Vector are to
705 705 * be stored, if it is big enough; otherwise, a new array of the
706 706 * same runtime type is allocated for this purpose.
707 707 * @return an array containing the elements of the Vector
708 708 * @throws ArrayStoreException if the runtime type of a is not a supertype
709 709 * of the runtime type of every element in this Vector
710 710 * @throws NullPointerException if the given array is null
711 711 * @since 1.2
712 712 */
713 713 @SuppressWarnings("unchecked")
714 714 public synchronized <T> T[] toArray(T[] a) {
715 715 if (a.length < elementCount)
716 716 return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
717 717
718 718 System.arraycopy(elementData, 0, a, 0, elementCount);
719 719
720 720 if (a.length > elementCount)
721 721 a[elementCount] = null;
722 722
723 723 return a;
724 724 }
725 725
726 726 // Positional Access Operations
727 727
728 728 @SuppressWarnings("unchecked")
729 729 E elementData(int index) {
730 730 return (E) elementData[index];
731 731 }
732 732
733 733 /**
734 734 * Returns the element at the specified position in this Vector.
735 735 *
736 736 * @param index index of the element to return
737 737 * @return object at the specified index
738 738 * @throws ArrayIndexOutOfBoundsException if the index is out of range
739 739 * ({@code index < 0 || index >= size()})
740 740 * @since 1.2
741 741 */
742 742 public synchronized E get(int index) {
743 743 if (index >= elementCount)
744 744 throw new ArrayIndexOutOfBoundsException(index);
745 745
746 746 return elementData(index);
747 747 }
748 748
749 749 /**
750 750 * Replaces the element at the specified position in this Vector with the
751 751 * specified element.
752 752 *
753 753 * @param index index of the element to replace
754 754 * @param element element to be stored at the specified position
755 755 * @return the element previously at the specified position
756 756 * @throws ArrayIndexOutOfBoundsException if the index is out of range
757 757 * ({@code index < 0 || index >= size()})
758 758 * @since 1.2
759 759 */
760 760 public synchronized E set(int index, E element) {
761 761 if (index >= elementCount)
762 762 throw new ArrayIndexOutOfBoundsException(index);
763 763
764 764 E oldValue = elementData(index);
765 765 elementData[index] = element;
766 766 return oldValue;
767 767 }
768 768
769 769 /**
770 770 * Appends the specified element to the end of this Vector.
771 771 *
772 772 * @param e element to be appended to this Vector
773 773 * @return {@code true} (as specified by {@link Collection#add})
774 774 * @since 1.2
775 775 */
776 776 public synchronized boolean add(E e) {
777 777 modCount++;
778 778 ensureCapacityHelper(elementCount + 1);
779 779 elementData[elementCount++] = e;
780 780 return true;
781 781 }
782 782
783 783 /**
784 784 * Removes the first occurrence of the specified element in this Vector
785 785 * If the Vector does not contain the element, it is unchanged. More
786 786 * formally, removes the element with the lowest index i such that
787 787 * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
788 788 * an element exists).
789 789 *
790 790 * @param o element to be removed from this Vector, if present
791 791 * @return true if the Vector contained the specified element
792 792 * @since 1.2
793 793 */
794 794 public boolean remove(Object o) {
795 795 return removeElement(o);
796 796 }
797 797
798 798 /**
799 799 * Inserts the specified element at the specified position in this Vector.
800 800 * Shifts the element currently at that position (if any) and any
801 801 * subsequent elements to the right (adds one to their indices).
802 802 *
803 803 * @param index index at which the specified element is to be inserted
804 804 * @param element element to be inserted
805 805 * @throws ArrayIndexOutOfBoundsException if the index is out of range
806 806 * ({@code index < 0 || index > size()})
807 807 * @since 1.2
808 808 */
809 809 public void add(int index, E element) {
810 810 insertElementAt(element, index);
811 811 }
812 812
813 813 /**
814 814 * Removes the element at the specified position in this Vector.
815 815 * Shifts any subsequent elements to the left (subtracts one from their
816 816 * indices). Returns the element that was removed from the Vector.
817 817 *
818 818 * @throws ArrayIndexOutOfBoundsException if the index is out of range
819 819 * ({@code index < 0 || index >= size()})
820 820 * @param index the index of the element to be removed
821 821 * @return element that was removed
822 822 * @since 1.2
823 823 */
824 824 public synchronized E remove(int index) {
825 825 modCount++;
826 826 if (index >= elementCount)
827 827 throw new ArrayIndexOutOfBoundsException(index);
828 828 E oldValue = elementData(index);
829 829
830 830 int numMoved = elementCount - index - 1;
831 831 if (numMoved > 0)
832 832 System.arraycopy(elementData, index+1, elementData, index,
833 833 numMoved);
834 834 elementData[--elementCount] = null; // Let gc do its work
835 835
836 836 return oldValue;
837 837 }
838 838
839 839 /**
840 840 * Removes all of the elements from this Vector. The Vector will
841 841 * be empty after this call returns (unless it throws an exception).
842 842 *
843 843 * @since 1.2
844 844 */
845 845 public void clear() {
846 846 removeAllElements();
847 847 }
848 848
849 849 // Bulk Operations
850 850
851 851 /**
852 852 * Returns true if this Vector contains all of the elements in the
853 853 * specified Collection.
854 854 *
855 855 * @param c a collection whose elements will be tested for containment
856 856 * in this Vector
857 857 * @return true if this Vector contains all of the elements in the
858 858 * specified collection
859 859 * @throws NullPointerException if the specified collection is null
860 860 */
861 861 public synchronized boolean containsAll(Collection<?> c) {
862 862 return super.containsAll(c);
863 863 }
864 864
865 865 /**
866 866 * Appends all of the elements in the specified Collection to the end of
867 867 * this Vector, in the order that they are returned by the specified
868 868 * Collection's Iterator. The behavior of this operation is undefined if
869 869 * the specified Collection is modified while the operation is in progress.
870 870 * (This implies that the behavior of this call is undefined if the
871 871 * specified Collection is this Vector, and this Vector is nonempty.)
872 872 *
873 873 * @param c elements to be inserted into this Vector
874 874 * @return {@code true} if this Vector changed as a result of the call
875 875 * @throws NullPointerException if the specified collection is null
876 876 * @since 1.2
877 877 */
878 878 public synchronized boolean addAll(Collection<? extends E> c) {
879 879 modCount++;
880 880 Object[] a = c.toArray();
881 881 int numNew = a.length;
882 882 ensureCapacityHelper(elementCount + numNew);
883 883 System.arraycopy(a, 0, elementData, elementCount, numNew);
884 884 elementCount += numNew;
885 885 return numNew != 0;
886 886 }
887 887
888 888 /**
889 889 * Removes from this Vector all of its elements that are contained in the
890 890 * specified Collection.
891 891 *
892 892 * @param c a collection of elements to be removed from the Vector
893 893 * @return true if this Vector changed as a result of the call
894 894 * @throws ClassCastException if the types of one or more elements
895 895 * in this vector are incompatible with the specified
896 896 * collection (optional)
897 897 * @throws NullPointerException if this vector contains one or more null
898 898 * elements and the specified collection does not support null
899 899 * elements (optional), or if the specified collection is null
900 900 * @since 1.2
901 901 */
902 902 public synchronized boolean removeAll(Collection<?> c) {
903 903 return super.removeAll(c);
904 904 }
905 905
906 906 /**
907 907 * Retains only the elements in this Vector that are contained in the
908 908 * specified Collection. In other words, removes from this Vector all
909 909 * of its elements that are not contained in the specified Collection.
910 910 *
911 911 * @param c a collection of elements to be retained in this Vector
↓ open down ↓ |
911 lines elided |
↑ open up ↑ |
912 912 * (all other elements are removed)
913 913 * @return true if this Vector changed as a result of the call
914 914 * @throws ClassCastException if the types of one or more elements
915 915 * in this vector are incompatible with the specified
916 916 * collection (optional)
917 917 * @throws NullPointerException if this vector contains one or more null
918 918 * elements and the specified collection does not support null
919 919 * elements (optional), or if the specified collection is null
920 920 * @since 1.2
921 921 */
922 - public synchronized boolean retainAll(Collection<?> c) {
922 + public synchronized boolean retainAll(Collection<?> c) {
923 923 return super.retainAll(c);
924 924 }
925 925
926 926 /**
927 927 * Inserts all of the elements in the specified Collection into this
928 928 * Vector at the specified position. Shifts the element currently at
929 929 * that position (if any) and any subsequent elements to the right
930 930 * (increases their indices). The new elements will appear in the Vector
931 931 * in the order that they are returned by the specified Collection's
932 932 * iterator.
933 933 *
934 934 * @param index index at which to insert the first element from the
935 935 * specified collection
936 936 * @param c elements to be inserted into this Vector
937 937 * @return {@code true} if this Vector changed as a result of the call
938 938 * @throws ArrayIndexOutOfBoundsException if the index is out of range
939 939 * ({@code index < 0 || index > size()})
940 940 * @throws NullPointerException if the specified collection is null
941 941 * @since 1.2
942 942 */
943 943 public synchronized boolean addAll(int index, Collection<? extends E> c) {
944 944 modCount++;
945 945 if (index < 0 || index > elementCount)
946 946 throw new ArrayIndexOutOfBoundsException(index);
947 947
948 948 Object[] a = c.toArray();
949 949 int numNew = a.length;
950 950 ensureCapacityHelper(elementCount + numNew);
951 951
952 952 int numMoved = elementCount - index;
953 953 if (numMoved > 0)
954 954 System.arraycopy(elementData, index, elementData, index + numNew,
955 955 numMoved);
956 956
957 957 System.arraycopy(a, 0, elementData, index, numNew);
958 958 elementCount += numNew;
959 959 return numNew != 0;
960 960 }
961 961
962 962 /**
963 963 * Compares the specified Object with this Vector for equality. Returns
964 964 * true if and only if the specified Object is also a List, both Lists
965 965 * have the same size, and all corresponding pairs of elements in the two
966 966 * Lists are <em>equal</em>. (Two elements {@code e1} and
967 967 * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
968 968 * e1.equals(e2))}.) In other words, two Lists are defined to be
969 969 * equal if they contain the same elements in the same order.
970 970 *
971 971 * @param o the Object to be compared for equality with this Vector
972 972 * @return true if the specified Object is equal to this Vector
973 973 */
974 974 public synchronized boolean equals(Object o) {
975 975 return super.equals(o);
976 976 }
977 977
978 978 /**
979 979 * Returns the hash code value for this Vector.
980 980 */
981 981 public synchronized int hashCode() {
982 982 return super.hashCode();
983 983 }
984 984
985 985 /**
986 986 * Returns a string representation of this Vector, containing
987 987 * the String representation of each element.
988 988 */
989 989 public synchronized String toString() {
990 990 return super.toString();
991 991 }
992 992
993 993 /**
994 994 * Returns a view of the portion of this List between fromIndex,
995 995 * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are
996 996 * equal, the returned List is empty.) The returned List is backed by this
997 997 * List, so changes in the returned List are reflected in this List, and
998 998 * vice-versa. The returned List supports all of the optional List
999 999 * operations supported by this List.
1000 1000 *
1001 1001 * <p>This method eliminates the need for explicit range operations (of
1002 1002 * the sort that commonly exist for arrays). Any operation that expects
1003 1003 * a List can be used as a range operation by operating on a subList view
1004 1004 * instead of a whole List. For example, the following idiom
1005 1005 * removes a range of elements from a List:
1006 1006 * <pre>
1007 1007 * list.subList(from, to).clear();
1008 1008 * </pre>
1009 1009 * Similar idioms may be constructed for indexOf and lastIndexOf,
1010 1010 * and all of the algorithms in the Collections class can be applied to
1011 1011 * a subList.
1012 1012 *
1013 1013 * <p>The semantics of the List returned by this method become undefined if
1014 1014 * the backing list (i.e., this List) is <i>structurally modified</i> in
1015 1015 * any way other than via the returned List. (Structural modifications are
1016 1016 * those that change the size of the List, or otherwise perturb it in such
1017 1017 * a fashion that iterations in progress may yield incorrect results.)
1018 1018 *
1019 1019 * @param fromIndex low endpoint (inclusive) of the subList
1020 1020 * @param toIndex high endpoint (exclusive) of the subList
1021 1021 * @return a view of the specified range within this List
1022 1022 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
1023 1023 * {@code (fromIndex < 0 || toIndex > size)}
1024 1024 * @throws IllegalArgumentException if the endpoint indices are out of order
1025 1025 * {@code (fromIndex > toIndex)}
1026 1026 */
1027 1027 public synchronized List<E> subList(int fromIndex, int toIndex) {
1028 1028 return Collections.synchronizedList(super.subList(fromIndex, toIndex),
1029 1029 this);
1030 1030 }
1031 1031
1032 1032 /**
1033 1033 * Removes from this list all of the elements whose index is between
1034 1034 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
1035 1035 * Shifts any succeeding elements to the left (reduces their index).
1036 1036 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
1037 1037 * (If {@code toIndex==fromIndex}, this operation has no effect.)
1038 1038 */
1039 1039 protected synchronized void removeRange(int fromIndex, int toIndex) {
1040 1040 modCount++;
1041 1041 int numMoved = elementCount - toIndex;
1042 1042 System.arraycopy(elementData, toIndex, elementData, fromIndex,
1043 1043 numMoved);
1044 1044
1045 1045 // Let gc do its work
1046 1046 int newElementCount = elementCount - (toIndex-fromIndex);
1047 1047 while (elementCount != newElementCount)
1048 1048 elementData[--elementCount] = null;
1049 1049 }
1050 1050
1051 1051 /**
1052 1052 * Save the state of the {@code Vector} instance to a stream (that
1053 1053 * is, serialize it). This method is present merely for synchronization.
1054 1054 * It just calls the default writeObject method.
1055 1055 */
1056 1056 private synchronized void writeObject(java.io.ObjectOutputStream s)
1057 1057 throws java.io.IOException
1058 1058 {
1059 1059 s.defaultWriteObject();
1060 1060 }
1061 1061
1062 1062 /**
1063 1063 * Returns a list iterator over the elements in this list (in proper
1064 1064 * sequence), starting at the specified position in the list.
1065 1065 * The specified index indicates the first element that would be
1066 1066 * returned by an initial call to {@link ListIterator#next next}.
1067 1067 * An initial call to {@link ListIterator#previous previous} would
1068 1068 * return the element with the specified index minus one.
1069 1069 *
1070 1070 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
1071 1071 *
1072 1072 * @throws IndexOutOfBoundsException {@inheritDoc}
1073 1073 */
1074 1074 public synchronized ListIterator<E> listIterator(int index) {
1075 1075 if (index < 0 || index > elementCount)
1076 1076 throw new IndexOutOfBoundsException("Index: "+index);
1077 1077 return new ListItr(index);
1078 1078 }
1079 1079
1080 1080 /**
1081 1081 * Returns a list iterator over the elements in this list (in proper
1082 1082 * sequence).
1083 1083 *
1084 1084 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
1085 1085 *
1086 1086 * @see #listIterator(int)
1087 1087 */
1088 1088 public synchronized ListIterator<E> listIterator() {
1089 1089 return new ListItr(0);
1090 1090 }
1091 1091
1092 1092 /**
1093 1093 * Returns an iterator over the elements in this list in proper sequence.
1094 1094 *
1095 1095 * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
1096 1096 *
1097 1097 * @return an iterator over the elements in this list in proper sequence
1098 1098 */
1099 1099 public synchronized Iterator<E> iterator() {
1100 1100 return new Itr();
1101 1101 }
1102 1102
1103 1103 /**
1104 1104 * An optimized version of AbstractList.Itr
1105 1105 */
1106 1106 private class Itr implements Iterator<E> {
1107 1107 int cursor; // index of next element to return
1108 1108 int lastRet = -1; // index of last element returned; -1 if no such
1109 1109 int expectedModCount = modCount;
1110 1110
1111 1111 public boolean hasNext() {
1112 1112 // Racy but within spec, since modifications are checked
1113 1113 // within or after synchronization in next/previous
1114 1114 return cursor != elementCount;
1115 1115 }
1116 1116
1117 1117 public E next() {
1118 1118 synchronized (Vector.this) {
1119 1119 checkForComodification();
1120 1120 int i = cursor;
1121 1121 if (i >= elementCount)
1122 1122 throw new NoSuchElementException();
1123 1123 cursor = i + 1;
1124 1124 return elementData(lastRet = i);
1125 1125 }
1126 1126 }
1127 1127
1128 1128 public void remove() {
1129 1129 if (lastRet == -1)
1130 1130 throw new IllegalStateException();
1131 1131 synchronized (Vector.this) {
1132 1132 checkForComodification();
1133 1133 Vector.this.remove(lastRet);
1134 1134 expectedModCount = modCount;
1135 1135 }
1136 1136 cursor = lastRet;
1137 1137 lastRet = -1;
1138 1138 }
1139 1139
1140 1140 final void checkForComodification() {
1141 1141 if (modCount != expectedModCount)
1142 1142 throw new ConcurrentModificationException();
1143 1143 }
1144 1144 }
1145 1145
1146 1146 /**
1147 1147 * An optimized version of AbstractList.ListItr
1148 1148 */
1149 1149 final class ListItr extends Itr implements ListIterator<E> {
1150 1150 ListItr(int index) {
1151 1151 super();
1152 1152 cursor = index;
1153 1153 }
1154 1154
1155 1155 public boolean hasPrevious() {
1156 1156 return cursor != 0;
1157 1157 }
1158 1158
1159 1159 public int nextIndex() {
1160 1160 return cursor;
1161 1161 }
1162 1162
1163 1163 public int previousIndex() {
1164 1164 return cursor - 1;
1165 1165 }
1166 1166
1167 1167 public E previous() {
1168 1168 synchronized (Vector.this) {
1169 1169 checkForComodification();
1170 1170 int i = cursor - 1;
1171 1171 if (i < 0)
1172 1172 throw new NoSuchElementException();
1173 1173 cursor = i;
1174 1174 return elementData(lastRet = i);
1175 1175 }
1176 1176 }
1177 1177
1178 1178 public void set(E e) {
1179 1179 if (lastRet == -1)
1180 1180 throw new IllegalStateException();
1181 1181 synchronized (Vector.this) {
1182 1182 checkForComodification();
1183 1183 Vector.this.set(lastRet, e);
1184 1184 }
1185 1185 }
1186 1186
1187 1187 public void add(E e) {
1188 1188 int i = cursor;
1189 1189 synchronized (Vector.this) {
1190 1190 checkForComodification();
1191 1191 Vector.this.add(i, e);
1192 1192 expectedModCount = modCount;
1193 1193 }
1194 1194 cursor = i + 1;
1195 1195 lastRet = -1;
1196 1196 }
1197 1197 }
1198 1198 }
↓ open down ↓ |
266 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX