Vector.java 22.1 KB
Newer Older
Bryce McKinlay committed
1 2
/* Vector.java -- Class that provides growable arrays.
   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
Tom Tromey committed
3

Bryce McKinlay committed
4
This file is part of GNU Classpath.
Tom Tromey committed
5

Bryce McKinlay committed
6 7 8 9
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Tom Tromey committed
10

Bryce McKinlay committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
Tom Tromey committed
26

Bryce McKinlay committed
27 28 29

package java.util;
import java.lang.reflect.Array;
Tom Tromey committed
30 31 32
import java.io.Serializable;

/**
Bryce McKinlay committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 * the <b>Vector</b> classes implements growable arrays of Objects.
 * You can access elements in a Vector with an index, just as you
 * can in a built in array, but Vectors can grow and shrink to accomodate
 * more or fewer objects.  
 *
 * Vectors try to mantain efficiency in growing by having a
 * <b>capacityIncrement</b> that can be specified at instantiation.
 * When a Vector can no longer hold a new Object, it grows by the amount
 * in <b>capacityIncrement</b>.  
 *
 * Vector implements the JDK 1.2 List interface, and is therefor a fully
 * compliant Collection object.
 *
 * @specnote The JCL claims that various methods in this class throw
 * IndexOutOfBoundsException, which would be consistent with other collections
 * classes. ArrayIndexOutOfBoundsException is actually thrown, per the online 
 * docs, even for List method implementations.
 * 
 * @author Scott G. Miller
Tom Tromey committed
52
 */
Bryce McKinlay committed
53 54
public class Vector extends AbstractList 
  implements List, Cloneable, Serializable
Tom Tromey committed
55
{
Bryce McKinlay committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  /**
   * The amount the Vector's internal array should be increased in size when
   * a new element is added that exceeds the current size of the array,
   * or when {@link #ensureCapacity} is called.
   * @serial
   */
  protected int capacityIncrement = 0;

  /**
   * The number of elements currently in the vector, also returned by
   * {@link #size}.
   * @serial
   */
  protected int elementCount = 0;

  /**
   * The internal array used to hold members of a Vector
   * @serial
   */
  protected Object[] elementData;
Tom Tromey committed
76

Bryce McKinlay committed
77
  private static final long serialVersionUID = -2767605614048989439L;
Tom Tromey committed
78

Bryce McKinlay committed
79 80 81 82 83
  /**
   * Constructs an empty vector with an initial size of 10, and
   * a capacity increment of 0
   */
  public Vector()
Tom Tromey committed
84
  {
Bryce McKinlay committed
85
    this(10);
Tom Tromey committed
86 87
  }

Bryce McKinlay committed
88 89 90 91 92 93 94 95
  /**
   * Constructs a vector containing the contents of Collection, in the
   * order given by the collection
   *
   * @param c A collection of elements to be added to the newly constructed
   * vector
   */
  public Vector(Collection c)
Tom Tromey committed
96
  {
Bryce McKinlay committed
97 98 99 100 101 102 103 104
    int csize = c.size();
    elementData = new Object[csize];
    elementCount = csize;
    Iterator itr = c.iterator();
    for (int i = 0; i < csize; i++)
      {
	elementData[i] = itr.next();
      }
Tom Tromey committed
105 106
  }

Bryce McKinlay committed
107 108 109 110 111 112 113 114 115 116
  /**
   * Constructs a Vector with the initial capacity and capacity 
   * increment specified
   *
   * @param initialCapacity The initial size of the Vector's internal
   * array
   * @param capacityIncrement The amount the internal array should be
   * increased if necessary
   */
  public Vector(int initialCapacity, int capacityIncrement)
Tom Tromey committed
117
  {
118 119
    if (initialCapacity < 0)
      throw new IllegalArgumentException();
Bryce McKinlay committed
120 121
    elementData = new Object[initialCapacity];
    this.capacityIncrement = capacityIncrement;
Tom Tromey committed
122 123
  }

Bryce McKinlay committed
124 125 126 127 128 129
  /**
   * Constructs a Vector with the initial capacity specified
   *
   * @param initialCapacity The initial size of the Vector's internal array
   */
  public Vector(int initialCapacity)
Tom Tromey committed
130
  {
131 132
    if (initialCapacity < 0)
      throw new IllegalArgumentException();
Bryce McKinlay committed
133
    elementData = new Object[initialCapacity];
Tom Tromey committed
134 135
  }

Bryce McKinlay committed
136 137 138 139 140 141 142 143 144 145 146 147
  /**
   * Copies the contents of a provided array into the Vector.  If the 
   * array is too large to fit in the Vector, an ArrayIndexOutOfBoundsException
   * is thrown.  Old elements in the Vector are overwritten by the new
   * elements
   *
   * @param anArray An array from which elements will be copied into the Vector
   * 
   * @throws ArrayIndexOutOfBoundsException the array being copied
   * is larger than the Vectors internal data array
   */
  public synchronized void copyInto(Object[] anArray)
Tom Tromey committed
148
  {
Bryce McKinlay committed
149
    System.arraycopy(elementData, 0, anArray, 0, elementCount);
Tom Tromey committed
150 151
  }

Bryce McKinlay committed
152 153 154 155 156 157
  /**
   * Trims the Vector down to size.  If the internal data array is larger
   * than the number of Objects its holding, a new array is constructed
   * that precisely holds the elements.  
   */
  public synchronized void trimToSize()
Tom Tromey committed
158
  {
159 160 161
    // Don't bother checking for the case where size() == the capacity of the
    // vector since that is a much less likely case; it's more efficient to
    // not do the check and lose a bit of performance in that infrequent case
Tom Tromey committed
162

163
    Object[] newArray = new Object[elementCount];
Bryce McKinlay committed
164 165
    System.arraycopy(elementData, 0, newArray, 0, elementCount);
    elementData = newArray;
Tom Tromey committed
166 167
  }

Bryce McKinlay committed
168 169 170 171 172 173 174 175 176 177 178 179
  /**
   * Ensures that <b>minCapacity</b> elements can fit within this Vector.
   * If it cannot hold this many elements, the internal data array is expanded
   * in the following manner.  If the current size plus the capacityIncrement
   * is sufficient, the internal array is expanded by capacityIncrement.  
   * If capacityIncrement is non-positive, the size is doubled.  If 
   * neither is sufficient, the internal array is expanded to size minCapacity
   *
   * @param minCapacity The minimum capacity the internal array should be
   * able to handle after executing this method
   */
  public synchronized void ensureCapacity(int minCapacity)
Tom Tromey committed
180
  {
Bryce McKinlay committed
181 182
    if (elementData.length >= minCapacity)
      return;
Tom Tromey committed
183

Bryce McKinlay committed
184 185 186 187 188 189 190
    int newCapacity; 
    if (capacityIncrement <= 0)
      newCapacity = elementData.length * 2;
    else
      newCapacity = elementData.length + capacityIncrement;
      
    Object[] newArray = new Object[Math.max(newCapacity, minCapacity)];
Tom Tromey committed
191

Bryce McKinlay committed
192 193
    System.arraycopy(elementData, 0, newArray, 0, elementData.length);
    elementData = newArray;
Tom Tromey committed
194 195
  }

Bryce McKinlay committed
196 197 198 199 200 201 202 203 204
  /**
   * Explicitly sets the size of the internal data array, copying the 
   * old values to the new internal array.  If the new array is smaller
   * than the old one, old values that don't fit are lost. If the new size
   * is larger than the old one, the vector is padded with null entries.
   *
   * @param newSize The new size of the internal array
   */
  public synchronized void setSize(int newSize)
Tom Tromey committed
205
  {
Bryce McKinlay committed
206 207 208 209 210 211
    modCount++;
    Object[] newArray = new Object[newSize];
    System.arraycopy(elementData, 0, newArray, 0, 
                     Math.min(newSize, elementCount));
    elementCount = newSize;
    elementData = newArray;
Tom Tromey committed
212 213
  }

Bryce McKinlay committed
214 215 216 217 218 219 220
  /**
   * Returns the size of the internal data array (not the amount of elements
   * contained in the Vector)
   *
   * @returns capacity of the internal data array
   */
  public int capacity()
Tom Tromey committed
221
  {
Bryce McKinlay committed
222
    return elementData.length;
Tom Tromey committed
223 224
  }

Bryce McKinlay committed
225 226 227 228 229 230
  /**
   * Returns the number of elements stored in this Vector
   *
   * @returns the number of elements in this Vector
   */
  public int size()
Tom Tromey committed
231
  {
Bryce McKinlay committed
232
    return elementCount;
Tom Tromey committed
233 234
  }

Bryce McKinlay committed
235 236 237 238 239 240
  /**
   * Returns true if this Vector is empty, false otherwise
   *
   * @returns true if the Vector is empty, false otherwise
   */
  public boolean isEmpty()
Tom Tromey committed
241
  {
Bryce McKinlay committed
242
    return elementCount == 0;
Tom Tromey committed
243 244
  }

Bryce McKinlay committed
245 246 247 248 249 250 251 252 253 254 255
  /**
   * Searches the vector starting at <b>index</b> for object <b>elem</b>
   * and returns the index of the first occurence of this Object.  If
   * the object is not found, -1 is returned
   *
   * @param e The Object to search for
   * @param index Start searching at this index
   * @returns The index of the first occurence of <b>elem</b>, or -1
   * if it is not found
   */
  public synchronized int indexOf(Object e, int index)
Tom Tromey committed
256
  {
Bryce McKinlay committed
257
    for (int i = index; i < elementCount; i++)
Tom Tromey committed
258
      {
Bryce McKinlay committed
259 260
	if (e == null ? elementData[i] == null : e.equals(elementData[i]))
	  return i;
Tom Tromey committed
261
      }
Bryce McKinlay committed
262
    return -1;
Tom Tromey committed
263 264
  }

Bryce McKinlay committed
265 266 267 268 269 270 271 272 273
  /**
   * Returns the first occurence of <b>elem</b> in the Vector, or -1 if
   * <b>elem</b> is not found.
   *
   * @param elem The object to search for
   * @returns The index of the first occurence of <b>elem</b> or -1 if 
   * not found
   */
  public int indexOf(Object elem)
Tom Tromey committed
274
  {
Bryce McKinlay committed
275
    return indexOf(elem, 0);
Tom Tromey committed
276 277
  }

Bryce McKinlay committed
278 279 280 281 282 283 284
  /**
   * Returns true if <b>elem</b> is contained in this Vector, false otherwise.
   *
   * @param elem The element to check
   * @returns true if the object is contained in this Vector, false otherwise
   */
  public boolean contains(Object elem)
Tom Tromey committed
285
  {
Bryce McKinlay committed
286
    return indexOf(elem, 0) != -1;
Tom Tromey committed
287 288
  }

Bryce McKinlay committed
289 290 291 292 293 294 295 296 297 298
  /**
   * Returns the index of the first occurence of <b>elem</b>, when searching
   * backwards from <b>index</b>.  If the object does not occur in this Vector,
   * -1 is returned.
   *
   * @param eThe object to search for
   * @param index The index to start searching in reverse from
   * @returns The index of the Object if found, -1 otherwise
   */
  public synchronized int lastIndexOf(Object e, int index)
Tom Tromey committed
299
  {
Bryce McKinlay committed
300
    if (index >= elementCount)
301
      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Bryce McKinlay committed
302 303

    for (int i = index; i >= 0; i--)
Tom Tromey committed
304
      {
Bryce McKinlay committed
305
	if (e == null ? elementData[i] == null : e.equals(elementData[i]))
Tom Tromey committed
306 307 308 309 310
	  return i;
      }
    return -1;
  }

Bryce McKinlay committed
311 312 313 314 315 316 317 318
  /**
   * Returns the last index of <b>elem</b> within this Vector, or -1
   * if the object is not within the Vector
   *
   * @param elem The object to search for
   * @returns the last index of the object, or -1 if not found
   */
  public int lastIndexOf(Object elem)
Tom Tromey committed
319
  {
Bryce McKinlay committed
320 321
    return lastIndexOf(elem, elementCount - 1);
  }
Tom Tromey committed
322

Bryce McKinlay committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336
  /**
   * Returns the Object stored at <b>index</b>.  If index is out of range
   * an ArrayIndexOutOfBoundsException is thrown.
   *
   * @param index the index of the Object to retrieve
   * @returns The object at <b>index</b>
   * @throws ArrayIndexOutOfBoundsException <b>index</b> is
   * larger than the Vector
   */
  public synchronized Object elementAt(int index)
  {
    //Within the bounds of this Vector does not necessarily mean within 
    //the bounds of the internal array
    if (index >= elementCount)
337
      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Tom Tromey committed
338

Bryce McKinlay committed
339
    return elementData[index];
Tom Tromey committed
340 341
  }

Bryce McKinlay committed
342 343 344 345 346 347 348 349
  /**
   * Returns the first element in the Vector.  If there is no first Object 
   * (The vector is empty), a NoSuchElementException is thrown.
   *
   * @returns The first Object in the Vector
   * @throws NoSuchElementException the Vector is empty
   */
  public synchronized Object firstElement()
Tom Tromey committed
350
  {
Bryce McKinlay committed
351 352 353 354
    if (elementCount == 0)
      throw new NoSuchElementException();

    return elementAt(0);
Tom Tromey committed
355 356
  }

Bryce McKinlay committed
357 358 359 360 361 362 363 364
  /**
   * Returns the last element in the Vector.  If the Vector has no last element
   * (The vector is empty), a NoSuchElementException is thrown.
   *
   * @returns The last Object in the Vector
   * @throws NoSuchElementException the Vector is empty
   */
  public synchronized Object lastElement()
Tom Tromey committed
365 366 367 368
  {
    if (elementCount == 0)
      throw new NoSuchElementException();

Bryce McKinlay committed
369
    return elementAt(elementCount - 1);
Tom Tromey committed
370 371
  }

Bryce McKinlay committed
372 373 374 375 376 377 378 379 380 381
  /**
   * Places <b>obj</b> at <b>index</b> within the Vector.  If <b>index</b>
   * refers to an index outside the Vector, an ArrayIndexOutOfBoundsException
   * is thrown.  
   * 
   * @param obj The object to store
   * @param index The position in the Vector to store the object
   * @throws ArrayIndexOutOfBoundsException the index is out of range
   */
  public synchronized void setElementAt(Object obj, int index)
Tom Tromey committed
382
  {
383 384
    if (index >= elementCount)
      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Bryce McKinlay committed
385 386

    elementData[index] = obj;
Tom Tromey committed
387 388
  }

Bryce McKinlay committed
389 390 391 392 393 394 395 396 397 398 399
  /**
   * Puts <b>element</b> into the Vector at position <b>index</b> and returns
   * the Object that previously occupied that position.
   *
   * @param index The index within the Vector to place the Object
   * @param element The Object to store in the Vector
   * @returns The previous object at the specified index
   * @throws ArrayIndexOutOfBoundsException the index is out of range
   *
   */
  public synchronized Object set(int index, Object element)
Tom Tromey committed
400
  {
Bryce McKinlay committed
401
    if (index >= elementCount)
402
      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Tom Tromey committed
403

Bryce McKinlay committed
404 405 406
    Object temp = elementData[index];
    elementData[index] = element;
    return temp;
Tom Tromey committed
407 408
  }

Bryce McKinlay committed
409 410 411 412 413 414 415
  /**
   * Removes the element at <b>index</b>, and shifts all elements at
   * positions greater than index to their index - 1.  
   *
   * @param index The index of the element to remove
   */
  public synchronized void removeElementAt(int index)
Tom Tromey committed
416
  {
Bryce McKinlay committed
417
    if (index >= elementCount)
418
      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Bryce McKinlay committed
419 420 421 422 423 424 425 426 427

    modCount++;
    elementCount--;
    if (index < elementCount)
      System.arraycopy(elementData, index + 1, elementData, index,
		       elementCount - index);
    //Delete the last element (which has been copied back one index)
    //so it can be garbage collected;
    elementData[elementCount] = null;
Tom Tromey committed
428 429
  }

Bryce McKinlay committed
430 431 432 433 434 435 436 437
  /**
   * Inserts a new element into the Vector at <b>index</b>.  Any elements
   * at or greater than index are shifted up one position.
   *
   * @param obj The object to insert
   * @param index The index at which the object is inserted
   */
  public void insertElementAt(Object obj, int index)
Tom Tromey committed
438
  {
439 440
    if (index > elementCount)
      throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount);
Bryce McKinlay committed
441

442
    if (elementCount == elementData.length)
443 444 445
      ensureCapacity(elementCount + 1);
    ++modCount;
    ++elementCount;
Bryce McKinlay committed
446 447 448
    System.arraycopy(elementData, index, elementData, index + 1,
		     elementCount - 1 - index);
    elementData[index] = obj;
Tom Tromey committed
449 450
  }

Bryce McKinlay committed
451 452 453 454 455 456 457 458 459
  /**
   * Adds an element to the Vector at the end of the Vector.  If the vector
   * cannot hold the element with its present capacity, its size is increased
   * based on the same rules followed if ensureCapacity was called with the
   * argument currentSize+1.
   *
   * @param obj The object to add to the Vector
   */
  public synchronized void addElement(Object obj)
Tom Tromey committed
460
  {
461
    if (elementCount == elementData.length)
462
      ensureCapacity(elementCount + 1);
Bryce McKinlay committed
463 464 465
    modCount++;
    elementData[elementCount++] = obj;
  }
Tom Tromey committed
466

Bryce McKinlay committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  /**
   * Removes the first occurence of the given object from the Vector.
   * If such a remove was performed (the object was found), true is returned.
   * If there was no such object, false is returned.
   *
   * @param obj The object to remove from the Vector
   * @returns true if the Object was in the Vector, false otherwise
   */
  public synchronized boolean removeElement(Object obj)
  {
    int idx = indexOf(obj);
    if (idx != -1)
      {
	removeElementAt(idx);
	return true;
      }
    return false;
Tom Tromey committed
484 485
  }

Bryce McKinlay committed
486 487 488 489 490
  /**
   * Removes all elements from the Vector.  Note that this does not
   * resize the internal data array.
   */
  public synchronized void removeAllElements()
Tom Tromey committed
491
  {
Bryce McKinlay committed
492 493 494
    modCount++;
    if (elementCount == 0)
      return;
Tom Tromey committed
495

496
    for (int i = elementCount - 1; i >= 0; --i)
Bryce McKinlay committed
497 498 499 500
      {
	elementData[i] = null;
      }
    elementCount = 0;
Tom Tromey committed
501 502
  }

Bryce McKinlay committed
503 504 505 506
  /**
   * Creates a new Vector with the same contents as this one.
   */
  public synchronized Object clone()
Tom Tromey committed
507
  {
Bryce McKinlay committed
508
    try
Tom Tromey committed
509
      {
Bryce McKinlay committed
510 511 512
	Vector clone = (Vector) super.clone();
	clone.elementData = (Object[]) elementData.clone();
	return clone;
Tom Tromey committed
513
      }
Bryce McKinlay committed
514
    catch (CloneNotSupportedException ex)
Tom Tromey committed
515
      {
Bryce McKinlay committed
516
	throw new InternalError(ex.toString());
Tom Tromey committed
517 518 519
      }
  }

Bryce McKinlay committed
520 521 522 523 524 525 526 527 528 529 530
  /**
   * Returns an Object array with the contents of this Vector, in the order
   * they are stored within this Vector.  Note that the Object array returned 
   * is not the internal data array, and that it holds only the elements 
   * within the Vector.  This is similar to creating a new Object[] with the 
   * size of this Vector, then calling Vector.copyInto(yourArray).
   *
   * @returns An Object[] containing the contents of this Vector in order
   *
   */
  public synchronized Object[] toArray()
Tom Tromey committed
531
  {
Bryce McKinlay committed
532 533 534
    Object[] newArray = new Object[elementCount];
    copyInto(newArray);
    return newArray;
Tom Tromey committed
535 536
  }

Bryce McKinlay committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  /**
   * Returns an array containing the contents of this Vector.  
   * If the provided array is large enough, the contents are copied
   * into that array, and a null is placed in the position size(). 
   * In this manner, you can obtain the size of a Vector by the position
   * of the null element.  If the type of the provided array cannot 
   * hold the elements, an ArrayStoreException is thrown.
   * 
   * If the provided array is not large enough,
   * a new one is created with the contents of the Vector, and no null 
   * element.  The new array is of the same runtime type as the provided
   * array.
   *
   *
   * @param array An array to copy the Vector into if large enough
   * @returns An array with the contents of this Vector in order
   * @throws ArrayStoreException the runtime type of the provided array
   * cannot hold the elements of the Vector
   */
  public synchronized Object[] toArray(Object[] array)
Tom Tromey committed
557
  {
Bryce McKinlay committed
558 559 560
    if (array.length < elementCount)
      array = (Object[]) Array.newInstance(array.getClass().getComponentType(), 
        				   elementCount);
561
    else if (array.length > elementCount)
Bryce McKinlay committed
562 563 564
      array[elementCount] = null;
    System.arraycopy(elementData, 0, array, 0, elementCount);
    return array;
Tom Tromey committed
565 566
  }

Bryce McKinlay committed
567 568 569 570 571 572 573 574
  /**
   * Returns the element at position <b>index</b>
   *
   * @param index the position from which an element will be retrieved
   * @throws ArrayIndexOutOfBoundsException the index is not within the 
   * range of the Vector
   */
  public synchronized Object get(int index)
Tom Tromey committed
575
  {
Bryce McKinlay committed
576
    return elementAt(index);
Tom Tromey committed
577 578
  }

Bryce McKinlay committed
579 580 581 582 583 584 585 586 587 588 589
  /**
   * Removes the given Object from the Vector.  If it exists, true
   * is returned, if not, false is returned.
   *
   * @param o The object to remove from the Vector
   * @returns true if the Object existed in the Vector, false otherwise
   */
  public boolean remove(Object o)
  {
    return removeElement(o);
  }
Tom Tromey committed
590

Bryce McKinlay committed
591 592 593 594 595 596 597 598 599 600
  /**
   * Adds an object to the Vector.
   *
   * @param o The element to add to the Vector
   */
  public synchronized boolean add(Object o)
  {
    addElement(o);
    return true;
  }
Tom Tromey committed
601

Bryce McKinlay committed
602 603 604 605 606 607 608 609 610 611 612
  /**
   * Adds an object at the specified index.  Elements at or above
   * index are shifted up one position.
   *
   * @param index The index at which to add the element
   * @param element The element to add to the Vector
   */
  public void add(int index, Object element)
  {
    insertElementAt(element, index);
  }
Tom Tromey committed
613

Bryce McKinlay committed
614 615 616 617 618 619 620 621 622 623 624
  /**
   * Removes the element at the specified index, and returns it.
   *
   * @param index The position from which to remove the element
   * @returns The object removed
   * @throws ArrayIndexOutOfBoundsException the index was out of the range
   * of the Vector
   */
  public synchronized Object remove(int index)
  {
    if (index >= elementCount)
625
      throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
Bryce McKinlay committed
626 627 628 629 630
  
    Object temp = elementData[index];
    removeElementAt(index);
    return temp;
  }
Tom Tromey committed
631

Bryce McKinlay committed
632 633 634 635 636 637 638
  /**
   * Clears all elements in the Vector and sets its size to 0
   */
  public void clear()
  {
    removeAllElements();
  }
Tom Tromey committed
639

Bryce McKinlay committed
640
  public synchronized boolean containsAll(Collection c)
641
  {
Bryce McKinlay committed
642 643 644
    Iterator itr = c.iterator();
    int size = c.size();
    for (int pos = 0; pos < size; pos++)
645
      {
Bryce McKinlay committed
646 647
	if (!contains(itr.next()))
	  return false;
648
      }
Bryce McKinlay committed
649
    return true;
650
  }
Tom Tromey committed
651

Bryce McKinlay committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665
  public synchronized boolean addAll(Collection c)
  {
    return addAll(elementCount, c);
  }
  
  public synchronized boolean removeAll(Collection c)
  {
    return super.removeAll(c);
  }
  
  public synchronized boolean retainAll(Collection c)
  {
    return super.retainAll(c);
  }
Tom Tromey committed
666

Bryce McKinlay committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
  public synchronized boolean addAll(int index, Collection c)
  {
    if (index < 0 || index > elementCount)
      throw new ArrayIndexOutOfBoundsException(index);
    modCount++;
    Iterator itr = c.iterator();
    int csize = c.size();

    ensureCapacity(elementCount + csize);
    int end = index + csize;
    if (elementCount > 0 && index != elementCount)
      System.arraycopy(elementData, index, elementData, end, csize);
    elementCount += csize;
    for (; index < end; index++)
      {
        elementData[index] = itr.next();
      }
    return (csize > 0);  
  }
Tom Tromey committed
686

Bryce McKinlay committed
687 688 689 690
  public synchronized boolean equals(Object c)
  {
    return super.equals(c);
  }
Tom Tromey committed
691

Bryce McKinlay committed
692 693 694 695
  public synchronized int hashCode()
  {
    return super.hashCode();
  }
Tom Tromey committed
696

Bryce McKinlay committed
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  /**
   * Returns a string representation of this Vector in the form 
   * [element0, element1, ... elementN]
   *
   * @returns the String representation of this Vector
   */
  public synchronized String toString()
  {
    String r = "[";
    for (int i = 0; i < elementCount; i++)
      {
	r += elementData[i];
	if (i < elementCount - 1)
	  r += ", ";
      }
    r += "]";
    return r;
  }
Tom Tromey committed
715

Bryce McKinlay committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
  /**
   * Returns an Enumeration of the elements of this List.
   * The Enumeration returned is compatible behavior-wise with
   * the 1.1 elements() method, in that it does not check for
   * concurrent modification.
   *
   * @returns an Enumeration
   */
  public synchronized Enumeration elements()
  {
    return new Enumeration()
    {
      int i = 0;
      public boolean hasMoreElements()
      {
	return (i < elementCount);
      }
      public Object nextElement()
      {
	if (i >= elementCount)
	  throw new NoSuchElementException();
	return (elementAt(i++));
      }
    };
  }
  
  public List subList(int fromIndex, int toIndex)
  {
    List sub = super.subList(fromIndex, toIndex);
    return Collections.synchronizedList(sub);
  }
  
  /** @specnote This is not specified as synchronized in the JCL, but it seems
    * to me that is should be. If it isn't, a clear() operation on a sublist
    * will not be synchronized w.r.t. the Vector object.
    */
  protected synchronized void removeRange(int fromIndex, int toIndex)
  {
    modCount++;
    if (fromIndex != toIndex)
      {
	System.arraycopy(elementData, toIndex, elementData, fromIndex, 
	                 elementCount - toIndex);
759 760
	// Clear unused elements so objects can be collected.
	int save = elementCount;
Bryce McKinlay committed
761
	elementCount -= (toIndex - fromIndex);
762 763
	for (int i = elementCount; i < save; ++i)
	  elementData[i] = null;
Bryce McKinlay committed
764 765
      }
  }
Tom Tromey committed
766
}