Vector.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /* Vector.java -- Class that provides growable arrays.
  2. Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005, 2006,
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. import java.io.IOException;
  34. import java.io.ObjectOutputStream;
  35. import java.io.Serializable;
  36. import java.lang.reflect.Array;
  37. /**
  38. * The <code>Vector</code> classes implements growable arrays of Objects.
  39. * You can access elements in a Vector with an index, just as you
  40. * can in a built in array, but Vectors can grow and shrink to accommodate
  41. * more or fewer objects.<p>
  42. *
  43. * Vectors try to mantain efficiency in growing by having a
  44. * <code>capacityIncrement</code> that can be specified at instantiation.
  45. * When a Vector can no longer hold a new Object, it grows by the amount
  46. * in <code>capacityIncrement</code>. If this value is 0, the vector doubles in
  47. * size.<p>
  48. *
  49. * Vector implements the JDK 1.2 List interface, and is therefore a fully
  50. * compliant Collection object. The iterators are fail-fast - if external
  51. * code structurally modifies the vector, any operation on the iterator will
  52. * then throw a {@link ConcurrentModificationException}. The Vector class is
  53. * fully synchronized, but the iterators are not. So, when iterating over a
  54. * vector, be sure to synchronize on the vector itself. If you don't want the
  55. * expense of synchronization, use ArrayList instead. On the other hand, the
  56. * Enumeration of elements() is not thread-safe, nor is it fail-fast; so it
  57. * can lead to undefined behavior even in a single thread if you modify the
  58. * vector during iteration.<p>
  59. *
  60. * Note: Some methods, especially those specified by List, specify throwing
  61. * {@link IndexOutOfBoundsException}, but it is easier to implement by
  62. * throwing the subclass {@link ArrayIndexOutOfBoundsException}. Others
  63. * directly specify this subclass.
  64. *
  65. * @author Scott G. Miller
  66. * @author Bryce McKinlay
  67. * @author Eric Blake (ebb9@email.byu.edu)
  68. * @see Collection
  69. * @see List
  70. * @see ArrayList
  71. * @see LinkedList
  72. * @since 1.0
  73. * @status updated to 1.4
  74. */
  75. public class Vector<T> extends AbstractList<T>
  76. implements List<T>, RandomAccess, Cloneable, Serializable
  77. {
  78. /**
  79. * Compatible with JDK 1.0+.
  80. */
  81. private static final long serialVersionUID = -2767605614048989439L;
  82. /**
  83. * The internal array used to hold members of a Vector. The elements are
  84. * in positions 0 through elementCount - 1, and all remaining slots are null.
  85. * @serial the elements
  86. */
  87. protected Object[] elementData;
  88. /**
  89. * The number of elements currently in the vector, also returned by
  90. * {@link #size}.
  91. * @serial the size
  92. */
  93. protected int elementCount;
  94. /**
  95. * The amount the Vector's internal array should be increased in size when
  96. * a new element is added that exceeds the current size of the array,
  97. * or when {@link #ensureCapacity} is called. If &lt;= 0, the vector just
  98. * doubles in size.
  99. * @serial the amount to grow the vector by
  100. */
  101. protected int capacityIncrement;
  102. /**
  103. * Constructs an empty vector with an initial size of 10, and
  104. * a capacity increment of 0
  105. */
  106. public Vector()
  107. {
  108. this(10, 0);
  109. }
  110. /**
  111. * Constructs a vector containing the contents of Collection, in the
  112. * order given by the collection.
  113. *
  114. * @param c collection of elements to add to the new vector
  115. * @throws NullPointerException if c is null
  116. * @since 1.2
  117. */
  118. public Vector(Collection<? extends T> c)
  119. {
  120. elementCount = c.size();
  121. elementData = c.toArray(new Object[elementCount]);
  122. }
  123. /**
  124. * Constructs a Vector with the initial capacity and capacity
  125. * increment specified.
  126. *
  127. * @param initialCapacity the initial size of the Vector's internal array
  128. * @param capacityIncrement the amount the internal array should be
  129. * increased by when necessary, 0 to double the size
  130. * @throws IllegalArgumentException if initialCapacity &lt; 0
  131. */
  132. public Vector(int initialCapacity, int capacityIncrement)
  133. {
  134. if (initialCapacity < 0)
  135. throw new IllegalArgumentException();
  136. elementData = new Object[initialCapacity];
  137. this.capacityIncrement = capacityIncrement;
  138. }
  139. /**
  140. * Constructs a Vector with the initial capacity specified, and a capacity
  141. * increment of 0 (double in size).
  142. *
  143. * @param initialCapacity the initial size of the Vector's internal array
  144. * @throws IllegalArgumentException if initialCapacity &lt; 0
  145. */
  146. public Vector(int initialCapacity)
  147. {
  148. this(initialCapacity, 0);
  149. }
  150. /**
  151. * Copies the contents of the Vector into the provided array. If the
  152. * array is too small to fit all the elements in the Vector, an
  153. * {@link IndexOutOfBoundsException} is thrown without modifying the array.
  154. * Old elements in the array are overwritten by the new elements.
  155. *
  156. * @param a target array for the copy
  157. * @throws IndexOutOfBoundsException the array is not large enough
  158. * @throws NullPointerException the array is null
  159. * @see #toArray(Object[])
  160. */
  161. public synchronized void copyInto(Object[] a)
  162. {
  163. System.arraycopy(elementData, 0, a, 0, elementCount);
  164. }
  165. /**
  166. * Trims the Vector down to size. If the internal data array is larger
  167. * than the number of Objects its holding, a new array is constructed
  168. * that precisely holds the elements. Otherwise this does nothing.
  169. */
  170. public synchronized void trimToSize()
  171. {
  172. // Don't bother checking for the case where size() == the capacity of the
  173. // vector since that is a much less likely case; it's more efficient to
  174. // not do the check and lose a bit of performance in that infrequent case
  175. T[] newArray = (T[]) new Object[elementCount];
  176. System.arraycopy(elementData, 0, newArray, 0, elementCount);
  177. elementData = newArray;
  178. }
  179. /**
  180. * Ensures that <code>minCapacity</code> elements can fit within this Vector.
  181. * If <code>elementData</code> is too small, it is expanded as follows:
  182. * If the <code>elementCount + capacityIncrement</code> is adequate, that
  183. * is the new size. If <code>capacityIncrement</code> is non-zero, the
  184. * candidate size is double the current. If that is not enough, the new
  185. * size is <code>minCapacity</code>.
  186. *
  187. * @param minCapacity the desired minimum capacity, negative values ignored
  188. */
  189. public synchronized void ensureCapacity(int minCapacity)
  190. {
  191. if (elementData.length >= minCapacity)
  192. return;
  193. int newCapacity;
  194. if (capacityIncrement <= 0)
  195. newCapacity = elementData.length * 2;
  196. else
  197. newCapacity = elementData.length + capacityIncrement;
  198. T[] newArray = (T[]) new Object[Math.max(newCapacity, minCapacity)];
  199. System.arraycopy(elementData, 0, newArray, 0, elementCount);
  200. elementData = newArray;
  201. }
  202. /**
  203. * Explicitly sets the size of the vector (but not necessarily the size of
  204. * the internal data array). If the new size is smaller than the old one,
  205. * old values that don't fit are lost. If the new size is larger than the
  206. * old one, the vector is padded with null entries.
  207. *
  208. * @param newSize The new size of the internal array
  209. * @throws ArrayIndexOutOfBoundsException if the new size is negative
  210. */
  211. public synchronized void setSize(int newSize)
  212. {
  213. // Don't bother checking for the case where size() == the capacity of the
  214. // vector since that is a much less likely case; it's more efficient to
  215. // not do the check and lose a bit of performance in that infrequent case
  216. modCount++;
  217. ensureCapacity(newSize);
  218. if (newSize < elementCount)
  219. Arrays.fill(elementData, newSize, elementCount, null);
  220. elementCount = newSize;
  221. }
  222. /**
  223. * Returns the size of the internal data array (not the amount of elements
  224. * contained in the Vector).
  225. *
  226. * @return capacity of the internal data array
  227. */
  228. public synchronized int capacity()
  229. {
  230. return elementData.length;
  231. }
  232. /**
  233. * Returns the number of elements stored in this Vector.
  234. *
  235. * @return the number of elements in this Vector
  236. */
  237. public synchronized int size()
  238. {
  239. return elementCount;
  240. }
  241. /**
  242. * Returns true if this Vector is empty, false otherwise
  243. *
  244. * @return true if the Vector is empty, false otherwise
  245. */
  246. public synchronized boolean isEmpty()
  247. {
  248. return elementCount == 0;
  249. }
  250. /**
  251. * Returns an Enumeration of the elements of this Vector. The enumeration
  252. * visits the elements in increasing index order, but is NOT thread-safe.
  253. *
  254. * @return an Enumeration
  255. * @see #iterator()
  256. */
  257. // No need to synchronize as the Enumeration is not thread-safe!
  258. public Enumeration<T> elements()
  259. {
  260. return new Enumeration<T>()
  261. {
  262. private int i = 0;
  263. public boolean hasMoreElements()
  264. {
  265. return i < elementCount;
  266. }
  267. @SuppressWarnings("unchecked")
  268. public T nextElement()
  269. {
  270. if (i >= elementCount)
  271. throw new NoSuchElementException();
  272. return (T) elementData[i++];
  273. }
  274. };
  275. }
  276. /**
  277. * Returns true when <code>elem</code> is contained in this Vector.
  278. *
  279. * @param elem the element to check
  280. * @return true if the object is contained in this Vector, false otherwise
  281. */
  282. public boolean contains(Object elem)
  283. {
  284. return indexOf(elem, 0) >= 0;
  285. }
  286. /**
  287. * Returns the first occurrence of <code>elem</code> in the Vector, or -1 if
  288. * <code>elem</code> is not found.
  289. *
  290. * @param elem the object to search for
  291. * @return the index of the first occurrence, or -1 if not found
  292. */
  293. public int indexOf(Object elem)
  294. {
  295. return indexOf(elem, 0);
  296. }
  297. /**
  298. * Searches the vector starting at <code>index</code> for object
  299. * <code>elem</code> and returns the index of the first occurrence of this
  300. * Object. If the object is not found, or index is larger than the size
  301. * of the vector, -1 is returned.
  302. *
  303. * @param e the Object to search for
  304. * @param index start searching at this index
  305. * @return the index of the next occurrence, or -1 if it is not found
  306. * @throws IndexOutOfBoundsException if index &lt; 0
  307. */
  308. public synchronized int indexOf(Object e, int index)
  309. {
  310. for (int i = index; i < elementCount; i++)
  311. if (equals(e, elementData[i]))
  312. return i;
  313. return -1;
  314. }
  315. /**
  316. * Returns the last index of <code>elem</code> within this Vector, or -1
  317. * if the object is not within the Vector.
  318. *
  319. * @param elem the object to search for
  320. * @return the last index of the object, or -1 if not found
  321. */
  322. public int lastIndexOf(Object elem)
  323. {
  324. return lastIndexOf(elem, elementCount - 1);
  325. }
  326. /**
  327. * Returns the index of the first occurrence of <code>elem</code>, when
  328. * searching backwards from <code>index</code>. If the object does not
  329. * occur in this Vector, or index is less than 0, -1 is returned.
  330. *
  331. * @param e the object to search for
  332. * @param index the index to start searching in reverse from
  333. * @return the index of the Object if found, -1 otherwise
  334. * @throws IndexOutOfBoundsException if index &gt;= size()
  335. */
  336. public synchronized int lastIndexOf(Object e, int index)
  337. {
  338. checkBoundExclusive(index);
  339. for (int i = index; i >= 0; i--)
  340. if (equals(e, elementData[i]))
  341. return i;
  342. return -1;
  343. }
  344. /**
  345. * Returns the Object stored at <code>index</code>.
  346. *
  347. * @param index the index of the Object to retrieve
  348. * @return the object at <code>index</code>
  349. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
  350. * @see #get(int)
  351. */
  352. @SuppressWarnings("unchecked")
  353. public synchronized T elementAt(int index)
  354. {
  355. checkBoundExclusive(index);
  356. return (T) elementData[index];
  357. }
  358. /**
  359. * Returns the first element (index 0) in the Vector.
  360. *
  361. * @return the first Object in the Vector
  362. * @throws NoSuchElementException the Vector is empty
  363. */
  364. @SuppressWarnings("unchecked")
  365. public synchronized T firstElement()
  366. {
  367. if (elementCount == 0)
  368. throw new NoSuchElementException();
  369. return (T) elementData[0];
  370. }
  371. /**
  372. * Returns the last element in the Vector.
  373. *
  374. * @return the last Object in the Vector
  375. * @throws NoSuchElementException the Vector is empty
  376. */
  377. @SuppressWarnings("unchecked")
  378. public synchronized T lastElement()
  379. {
  380. if (elementCount == 0)
  381. throw new NoSuchElementException();
  382. return (T) elementData[elementCount - 1];
  383. }
  384. /**
  385. * Changes the element at <code>index</code> to be <code>obj</code>
  386. *
  387. * @param obj the object to store
  388. * @param index the position in the Vector to store the object
  389. * @throws ArrayIndexOutOfBoundsException the index is out of range
  390. * @see #set(int, Object)
  391. */
  392. public void setElementAt(T obj, int index)
  393. {
  394. set(index, obj);
  395. }
  396. /**
  397. * Removes the element at <code>index</code>, and shifts all elements at
  398. * positions greater than index to their index - 1.
  399. *
  400. * @param index the index of the element to remove
  401. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size();
  402. * @see #remove(int)
  403. */
  404. public void removeElementAt(int index)
  405. {
  406. remove(index);
  407. }
  408. /**
  409. * Inserts a new element into the Vector at <code>index</code>. Any elements
  410. * at or greater than index are shifted up one position.
  411. *
  412. * @param obj the object to insert
  413. * @param index the index at which the object is inserted
  414. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt; size()
  415. * @see #add(int, Object)
  416. */
  417. public synchronized void insertElementAt(T obj, int index)
  418. {
  419. checkBoundInclusive(index);
  420. if (elementCount == elementData.length)
  421. ensureCapacity(elementCount + 1);
  422. modCount++;
  423. System.arraycopy(elementData, index, elementData, index + 1,
  424. elementCount - index);
  425. elementCount++;
  426. elementData[index] = obj;
  427. }
  428. /**
  429. * Adds an element to the Vector at the end of the Vector. The vector
  430. * is increased by ensureCapacity(size() + 1) if needed.
  431. *
  432. * @param obj the object to add to the Vector
  433. */
  434. public synchronized void addElement(T obj)
  435. {
  436. if (elementCount == elementData.length)
  437. ensureCapacity(elementCount + 1);
  438. modCount++;
  439. elementData[elementCount++] = obj;
  440. }
  441. /**
  442. * Removes the first (the lowest index) occurrence of the given object from
  443. * the Vector. If such a remove was performed (the object was found), true
  444. * is returned. If there was no such object, false is returned.
  445. *
  446. * @param obj the object to remove from the Vector
  447. * @return true if the Object was in the Vector, false otherwise
  448. * @see #remove(Object)
  449. */
  450. public synchronized boolean removeElement(Object obj)
  451. {
  452. int idx = indexOf(obj, 0);
  453. if (idx >= 0)
  454. {
  455. remove(idx);
  456. return true;
  457. }
  458. return false;
  459. }
  460. /**
  461. * Removes all elements from the Vector. Note that this does not
  462. * resize the internal data array.
  463. *
  464. * @see #clear()
  465. */
  466. public synchronized void removeAllElements()
  467. {
  468. if (elementCount == 0)
  469. return;
  470. modCount++;
  471. Arrays.fill(elementData, 0, elementCount, null);
  472. elementCount = 0;
  473. }
  474. /**
  475. * Creates a new Vector with the same contents as this one. The clone is
  476. * shallow; elements are not cloned.
  477. *
  478. * @return the clone of this vector
  479. */
  480. public synchronized Object clone()
  481. {
  482. try
  483. {
  484. Vector clone = (Vector) super.clone();
  485. clone.elementData = (Object[]) elementData.clone();
  486. return clone;
  487. }
  488. catch (CloneNotSupportedException ex)
  489. {
  490. // Impossible to get here.
  491. throw new InternalError(ex.toString());
  492. }
  493. }
  494. /**
  495. * Returns an Object array with the contents of this Vector, in the order
  496. * they are stored within this Vector. Note that the Object array returned
  497. * is not the internal data array, and that it holds only the elements
  498. * within the Vector. This is similar to creating a new Object[] with the
  499. * size of this Vector, then calling Vector.copyInto(yourArray).
  500. *
  501. * @return an Object[] containing the contents of this Vector in order
  502. * @since 1.2
  503. */
  504. public synchronized Object[] toArray()
  505. {
  506. Object[] newArray = new Object[elementCount];
  507. copyInto(newArray);
  508. return newArray;
  509. }
  510. /**
  511. * Returns an array containing the contents of this Vector.
  512. * If the provided array is large enough, the contents are copied
  513. * into that array, and a null is placed in the position size().
  514. * In this manner, you can obtain the size of a Vector by the position
  515. * of the null element, if you know the vector does not itself contain
  516. * null entries. If the array is not large enough, reflection is used
  517. * to create a bigger one of the same runtime type.
  518. *
  519. * @param a an array to copy the Vector into if large enough
  520. * @return an array with the contents of this Vector in order
  521. * @throws ArrayStoreException the runtime type of the provided array
  522. * cannot hold the elements of the Vector
  523. * @throws NullPointerException if <code>a</code> is null
  524. * @since 1.2
  525. */
  526. public synchronized <S> S[] toArray(S[] a)
  527. {
  528. if (a.length < elementCount)
  529. a = (S[]) Array.newInstance(a.getClass().getComponentType(),
  530. elementCount);
  531. else if (a.length > elementCount)
  532. a[elementCount] = null;
  533. System.arraycopy(elementData, 0, a, 0, elementCount);
  534. return a;
  535. }
  536. /**
  537. * Returns the element at position <code>index</code>.
  538. *
  539. * @param index the position from which an element will be retrieved
  540. * @return the element at that position
  541. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
  542. * @since 1.2
  543. */
  544. public T get(int index)
  545. {
  546. return elementAt(index);
  547. }
  548. /**
  549. * Puts <code>element</code> into the Vector at position <code>index</code>
  550. * and returns the Object that previously occupied that position.
  551. *
  552. * @param index the index within the Vector to place the Object
  553. * @param element the Object to store in the Vector
  554. * @return the previous object at the specified index
  555. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
  556. * @since 1.2
  557. */
  558. @SuppressWarnings("unchecked")
  559. public synchronized T set(int index, T element)
  560. {
  561. checkBoundExclusive(index);
  562. T temp = (T) elementData[index];
  563. elementData[index] = element;
  564. return temp;
  565. }
  566. /**
  567. * Adds an object to the Vector.
  568. *
  569. * @param o the element to add to the Vector
  570. * @return true, as specified by List
  571. * @since 1.2
  572. */
  573. public boolean add(T o)
  574. {
  575. addElement(o);
  576. return true;
  577. }
  578. /**
  579. * Removes the given Object from the Vector. If it exists, true
  580. * is returned, if not, false is returned.
  581. *
  582. * @param o the object to remove from the Vector
  583. * @return true if the Object existed in the Vector, false otherwise
  584. * @since 1.2
  585. */
  586. public boolean remove(Object o)
  587. {
  588. return removeElement(o);
  589. }
  590. /**
  591. * Adds an object at the specified index. Elements at or above
  592. * index are shifted up one position.
  593. *
  594. * @param index the index at which to add the element
  595. * @param element the element to add to the Vector
  596. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt; size()
  597. * @since 1.2
  598. */
  599. public void add(int index, T element)
  600. {
  601. insertElementAt(element, index);
  602. }
  603. /**
  604. * Removes the element at the specified index, and returns it.
  605. *
  606. * @param index the position from which to remove the element
  607. * @return the object removed
  608. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
  609. * @since 1.2
  610. */
  611. @SuppressWarnings("unchecked")
  612. public synchronized T remove(int index)
  613. {
  614. checkBoundExclusive(index);
  615. T temp = (T) elementData[index];
  616. modCount++;
  617. elementCount--;
  618. if (index < elementCount)
  619. System.arraycopy(elementData, index + 1, elementData, index,
  620. elementCount - index);
  621. elementData[elementCount] = null;
  622. return temp;
  623. }
  624. /**
  625. * Clears all elements in the Vector and sets its size to 0.
  626. */
  627. public void clear()
  628. {
  629. removeAllElements();
  630. }
  631. /**
  632. * Returns true if this Vector contains all the elements in c.
  633. *
  634. * @param c the collection to compare to
  635. * @return true if this vector contains all elements of c
  636. * @throws NullPointerException if c is null
  637. * @since 1.2
  638. */
  639. public synchronized boolean containsAll(Collection<?> c)
  640. {
  641. // Here just for the sychronization.
  642. return super.containsAll(c);
  643. }
  644. /**
  645. * Appends all elements of the given collection to the end of this Vector.
  646. * Behavior is undefined if the collection is modified during this operation
  647. * (for example, if this == c).
  648. *
  649. * @param c the collection to append
  650. * @return true if this vector changed, in other words c was not empty
  651. * @throws NullPointerException if c is null
  652. * @since 1.2
  653. */
  654. public synchronized boolean addAll(Collection<? extends T> c)
  655. {
  656. return addAll(elementCount, c);
  657. }
  658. /**
  659. * Remove from this vector all elements contained in the given collection.
  660. *
  661. * @param c the collection to filter out
  662. * @return true if this vector changed
  663. * @throws NullPointerException if c is null
  664. * @since 1.2
  665. */
  666. public synchronized boolean removeAll(Collection<?> c)
  667. {
  668. // The NullPointerException is thrown implicitly when the Vector
  669. // is not empty and c is null. The RI allows null arguments when
  670. // the vector is empty. See Mauve test:
  671. // gnu/testlet/java/util/Vector/removeAll.java
  672. int i;
  673. int j;
  674. for (i = 0; i < elementCount; i++)
  675. if (c.contains(elementData[i]))
  676. break;
  677. if (i == elementCount)
  678. return false;
  679. modCount++;
  680. for (j = i++; i < elementCount; i++)
  681. if (! c.contains(elementData[i]))
  682. elementData[j++] = elementData[i];
  683. elementCount -= i - j;
  684. return true;
  685. }
  686. /**
  687. * Retain in this vector only the elements contained in the given collection.
  688. *
  689. * @param c the collection to filter by
  690. * @return true if this vector changed
  691. * @throws NullPointerException if c is null
  692. * @since 1.2
  693. */
  694. public synchronized boolean retainAll(Collection<?> c)
  695. {
  696. // The NullPointerException is thrown implicitly when the Vector
  697. // is not empty and c is null. The RI allows null arguments when
  698. // the vector is empty. See Mauve test:
  699. // gnu/testlet/java/util/Vector/retainAll.java
  700. int i;
  701. int j;
  702. for (i = 0; i < elementCount; i++)
  703. if (! c.contains(elementData[i]))
  704. break;
  705. if (i == elementCount)
  706. return false;
  707. modCount++;
  708. for (j = i++; i < elementCount; i++)
  709. if (c.contains(elementData[i]))
  710. elementData[j++] = elementData[i];
  711. elementCount -= i - j;
  712. return true;
  713. }
  714. /**
  715. * Inserts all elements of the given collection at the given index of
  716. * this Vector. Behavior is undefined if the collection is modified during
  717. * this operation (for example, if this == c).
  718. *
  719. * @param c the collection to append
  720. * @return true if this vector changed, in other words c was not empty
  721. * @throws NullPointerException if c is null
  722. * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt; size()
  723. * @since 1.2
  724. */
  725. public synchronized boolean addAll(int index, Collection<? extends T> c)
  726. {
  727. checkBoundInclusive(index);
  728. Iterator<? extends T> itr = c.iterator();
  729. int csize = c.size();
  730. modCount++;
  731. ensureCapacity(elementCount + csize);
  732. int end = index + csize;
  733. if (elementCount > 0 && index != elementCount)
  734. System.arraycopy(elementData, index,
  735. elementData, end, elementCount - index);
  736. elementCount += csize;
  737. for ( ; index < end; index++)
  738. elementData[index] = itr.next();
  739. return (csize > 0);
  740. }
  741. /**
  742. * Compares this to the given object.
  743. *
  744. * @param o the object to compare to
  745. * @return true if the two are equal
  746. * @since 1.2
  747. */
  748. public synchronized boolean equals(Object o)
  749. {
  750. // Here just for the sychronization.
  751. return super.equals(o);
  752. }
  753. /**
  754. * Computes the hashcode of this object.
  755. *
  756. * @return the hashcode
  757. * @since 1.2
  758. */
  759. public synchronized int hashCode()
  760. {
  761. // Here just for the sychronization.
  762. return super.hashCode();
  763. }
  764. /**
  765. * Returns a string representation of this Vector in the form
  766. * "[element0, element1, ... elementN]".
  767. *
  768. * @return the String representation of this Vector
  769. */
  770. public synchronized String toString()
  771. {
  772. // Here just for the sychronization.
  773. return super.toString();
  774. }
  775. /**
  776. * Obtain a List view of a subsection of this list, from fromIndex
  777. * (inclusive) to toIndex (exclusive). If the two indices are equal, the
  778. * sublist is empty. The returned list is modifiable, and changes in one
  779. * reflect in the other. If this list is structurally modified in
  780. * any way other than through the returned list, the result of any subsequent
  781. * operations on the returned list is undefined.
  782. * <p>
  783. *
  784. * @param fromIndex the index that the returned list should start from
  785. * (inclusive)
  786. * @param toIndex the index that the returned list should go to (exclusive)
  787. * @return a List backed by a subsection of this vector
  788. * @throws IndexOutOfBoundsException if fromIndex &lt; 0
  789. * || toIndex &gt; size()
  790. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  791. * @see ConcurrentModificationException
  792. * @since 1.2
  793. */
  794. public synchronized List<T> subList(int fromIndex, int toIndex)
  795. {
  796. List<T> sub = super.subList(fromIndex, toIndex);
  797. // We must specify the correct object to synchronize upon, hence the
  798. // use of a non-public API
  799. return new Collections.SynchronizedList<T>(this, sub);
  800. }
  801. /**
  802. * Removes a range of elements from this list.
  803. * Does nothing when toIndex is equal to fromIndex.
  804. *
  805. * @param fromIndex the index to start deleting from (inclusive)
  806. * @param toIndex the index to delete up to (exclusive)
  807. * @throws IndexOutOfBoundsException if fromIndex &gt; toIndex
  808. */
  809. // This does not need to be synchronized, because it is only called through
  810. // clear() of a sublist, and clear() had already synchronized.
  811. protected void removeRange(int fromIndex, int toIndex)
  812. {
  813. int change = toIndex - fromIndex;
  814. if (change > 0)
  815. {
  816. modCount++;
  817. System.arraycopy(elementData, toIndex, elementData, fromIndex,
  818. elementCount - toIndex);
  819. int save = elementCount;
  820. elementCount -= change;
  821. Arrays.fill(elementData, elementCount, save, null);
  822. }
  823. else if (change < 0)
  824. throw new IndexOutOfBoundsException();
  825. }
  826. /**
  827. * Checks that the index is in the range of possible elements (inclusive).
  828. *
  829. * @param index the index to check
  830. * @throws ArrayIndexOutOfBoundsException if index &gt; size
  831. */
  832. private void checkBoundInclusive(int index)
  833. {
  834. // Implementation note: we do not check for negative ranges here, since
  835. // use of a negative index will cause an ArrayIndexOutOfBoundsException
  836. // with no effort on our part.
  837. if (index > elementCount)
  838. raiseBoundsError(index, " > ");
  839. }
  840. /**
  841. * Checks that the index is in the range of existing elements (exclusive).
  842. *
  843. * @param index the index to check
  844. * @throws ArrayIndexOutOfBoundsException if index &gt;= size
  845. */
  846. private void checkBoundExclusive(int index)
  847. {
  848. // Implementation note: we do not check for negative ranges here, since
  849. // use of a negative index will cause an ArrayIndexOutOfBoundsException
  850. // with no effort on our part.
  851. if (index >= elementCount)
  852. raiseBoundsError(index, " >= ");
  853. }
  854. /**
  855. * Raise the ArrayIndexOfOutBoundsException.
  856. *
  857. * @param index the index of the access
  858. * @param operator the operator to include in the error message
  859. * @throws IndexOutOfBoundsException unconditionally
  860. */
  861. private void raiseBoundsError(int index, String operator)
  862. {
  863. // Implementaion note: put in a separate method to make the JITs job easier
  864. // (separate common from uncommon code at method boundaries when trivial to
  865. // do so).
  866. throw new ArrayIndexOutOfBoundsException(index + operator + elementCount);
  867. }
  868. /**
  869. * Serializes this object to the given stream.
  870. *
  871. * @param s the stream to write to
  872. * @throws IOException if the underlying stream fails
  873. * @serialData just calls default write function
  874. */
  875. private synchronized void writeObject(ObjectOutputStream s)
  876. throws IOException
  877. {
  878. s.defaultWriteObject();
  879. }
  880. }