| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Vector |
|
| 2.358974358974359;2.359 | ||||
| VectorEnumerator |
|
| 2.358974358974359;2.359 |
| 1 | /* | |
| 2 | * @(#)Vector.java 1.36 97/01/28 | |
| 3 | * | |
| 4 | * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved. | |
| 5 | * | |
| 6 | * This software is the confidential and proprietary information of Sun | |
| 7 | * Microsystems, Inc. ("Confidential Information"). You shall not | |
| 8 | * disclose such Confidential Information and shall use it only in | |
| 9 | * accordance with the terms of the license agreement you entered into | |
| 10 | * with Sun. | |
| 11 | * | |
| 12 | * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE | |
| 13 | * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
| 14 | * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
| 15 | * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES | |
| 16 | * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING | |
| 17 | * THIS SOFTWARE OR ITS DERIVATIVES. | |
| 18 | * | |
| 19 | * CopyrightVersion 1.1_beta | |
| 20 | * | |
| 21 | */ | |
| 22 | ||
| 23 | // SELF_CHECKING | |
| 24 | // package java.util; | |
| 25 | import java.util.*; | |
| 26 | ||
| 27 | /** | |
| 28 | * The <code>Vector</code> class implements a growable array of | |
| 29 | * objects. Like an array, it contains components that can be | |
| 30 | * accessed using an integer index. However, the size of a | |
| 31 | * <code>Vector</code> can grow or shrink as needed to accommodate | |
| 32 | * adding and removing items after the <code>Vector</code> has been created. | |
| 33 | * <p> | |
| 34 | * Each vector tries to optimize storage management by maintaining a | |
| 35 | * <code>capacity</code> and a <code>capacityIncrement</code>. The | |
| 36 | * <code>capacity</code> is always at least as large as the vector | |
| 37 | * size; it is usually larger because as components are added to the | |
| 38 | * vector, the vector's storage increases in chunks the size of | |
| 39 | * <code>capacityIncrement</code>. An application can increase the | |
| 40 | * capacity of a vector before inserting a large number of | |
| 41 | * components; this reduces the amount of incremental reallocation. | |
| 42 | * | |
| 43 | * @author Lee Boynton | |
| 44 | * @author Jonathan Payne | |
| 45 | * @version 1.36, 28 Jan 1997 | |
| 46 | * @since JDK1.0 | |
| 47 | */ | |
| 48 | public | |
| 49 | class Vector implements Cloneable, java.io.Serializable { | |
| 50 | /** | |
| 51 | * The array buffer into which the components of the vector are | |
| 52 | * stored. The capacity of the vector is the length of this array buffer. | |
| 53 | * | |
| 54 | * @since JDK1.0 | |
| 55 | */ | |
| 56 | protected Object elementData[]; | |
| 57 | ||
| 58 | // SELF_CHECKING | |
| 59 | // This is the size of the representing array or a smaller | |
| 60 | // number if the size of the Vector has been reduce by | |
| 61 | // means of a call to an appropriate method, e.g., setSize | |
| 62 | /** | |
| 63 | * The number of valid components in the vector. | |
| 64 | * | |
| 65 | * @since JDK1.0 | |
| 66 | */ | |
| 67 | protected int elementCount; | |
| 68 | ||
| 69 | /** | |
| 70 | * The amount by which the capacity of the vector is automatically | |
| 71 | * incremented when its size becomes greater than its capacity. If | |
| 72 | * the capacity is <code>0</code>, the capacity of the vector is | |
| 73 | * doubled each time it needs to grow. | |
| 74 | * | |
| 75 | * @since JDK1.0 | |
| 76 | */ | |
| 77 | protected int capacityIncrement; | |
| 78 | ||
| 79 | /** use serialVersionUID from JDK 1.0.2 for interoperability */ | |
| 80 | private static final long serialVersionUID = -2767605614048989439L; | |
| 81 | ||
| 82 | // SELF_CHECKING | |
| 83 | private absVector _abstract; // abstract version of this class | |
| 84 | private absVector concr2abstr() { // representation function | |
| 85 | 157 | absVector v = new absEmpty(elementCount,capacityIncrement,elementData.length); |
| 86 | 157 | Enumeration e = elements(); |
| 87 | 1572 | for (int i = 0; i < elementCount; i++) { |
| 88 | 1415 | Object o = e.nextElement(); |
| 89 | 1415 | if (o != null) v = new absMap(i,o,v); |
| 90 | } | |
| 91 | 157 | return v; |
| 92 | } | |
| 93 | private void display() { | |
| 94 | 0 | System.err.println ("Vector: abstract vector = "+absVector.toString (_abstract)); |
| 95 | 0 | System.err.println ("Vector: concrete vector = "+absVector.toString (concr2abstr())); |
| 96 | 0 | } |
| 97 | private void verifyVectors(String s) { | |
| 98 | 157 | absVector concrete = concr2abstr(); |
| 99 | 157 | if (! absVector.equals(_abstract,concrete)) { |
| 100 | 0 | System.err.println ("Vector: error executing "+s); |
| 101 | 0 | System.err.println ("Vector: abstract = "+absVector.toString (_abstract)); |
| 102 | 0 | System.err.println ("Vector: concrete = "+absVector.toString (concrete)); |
| 103 | } | |
| 104 | 157 | } |
| 105 | private void verifyInts(String s, int x, int y) { | |
| 106 | 515 | if (x != y) { |
| 107 | 0 | System.err.println ("Vector: error executing "+s); |
| 108 | 0 | System.err.println ("Vector: abstract int = "+x); |
| 109 | 0 | System.err.println ("Vector: concrete int = "+y); |
| 110 | 0 | display(); |
| 111 | } | |
| 112 | 515 | } |
| 113 | private void verifyBooleans(String s, boolean x, boolean y) { | |
| 114 | 124 | if (x != y) { |
| 115 | 0 | System.err.println ("Vector: error executing "+s); |
| 116 | 0 | System.err.println ("Vector: abstract bool = "+x); |
| 117 | 0 | System.err.println ("Vector: concrete bool = "+y); |
| 118 | 0 | display(); |
| 119 | } | |
| 120 | 124 | } |
| 121 | private void verifyArrays(String s, Object [] x, Object [] y) { | |
| 122 | // x.length is the minumum length of y | |
| 123 | 0 | if (y.length < x.length) { |
| 124 | 0 | System.err.println ("Vector: error executing "+s); |
| 125 | 0 | System.err.println ("Vector: concrete array is too short"); |
| 126 | 0 | return; |
| 127 | } | |
| 128 | 0 | for (int i = 0; i < x.length; i++) |
| 129 | 0 | if (x[i] != null && x[i].equals(y[i]) || |
| 130 | 0 | x[i] == null && y[i] == null) continue; |
| 131 | else { | |
| 132 | 0 | System.err.println ("Vector: error executing "+s); |
| 133 | 0 | System.err.println ("Vector: abstract elem["+i+"] = "+x[i]); |
| 134 | 0 | System.err.println ("Vector: concrete elem["+i+"] = "+y[i]); |
| 135 | 0 | return; |
| 136 | } | |
| 137 | 0 | display(); |
| 138 | 0 | } |
| 139 | private void verifyObjects(String s, Object x, Object y) { | |
| 140 | 79 | if (x != null && x.equals(y) || |
| 141 | 79 | x == null && y == null) return; |
| 142 | else { | |
| 143 | 0 | String tx = x == null ? "null" : x.toString(); |
| 144 | 0 | String ty = y == null ? "null" : y.toString(); |
| 145 | 0 | System.err.println ("Vector: error executing "+s); |
| 146 | 0 | System.err.println ("Vector: abstract object = "+tx); |
| 147 | 0 | System.err.println ("Vector: concrete object = "+ty); |
| 148 | 0 | display(); |
| 149 | } | |
| 150 | 0 | } |
| 151 | private void verifyEnumerations(String s, Enumeration x, Enumeration y) { | |
| 152 | 199 | if (((VectorEnumerator)x).vector != ((VectorEnumerator)y).vector || |
| 153 | ((VectorEnumerator)x).count != ((VectorEnumerator)y).count) { | |
| 154 | 0 | System.err.println ("Vector: error executing "+s); |
| 155 | 0 | System.err.println ("Vector: abstract enum = "+x); |
| 156 | 0 | System.err.println ("Vector: concrete enum = "+y); |
| 157 | 0 | display(); |
| 158 | } | |
| 159 | 199 | } |
| 160 | private void verifyStrings(String s, String x, String y) { | |
| 161 | 42 | if (! x.equals(y)) { |
| 162 | 0 | System.err.println ("Vector: error executing "+s); |
| 163 | 0 | System.err.println ("Vector: abstract string = "+x); |
| 164 | 0 | System.err.println ("Vector: concrete string = "+y); |
| 165 | 0 | display(); |
| 166 | } | |
| 167 | 42 | } |
| 168 | ||
| 169 | /** | |
| 170 | * Constructs an empty vector with the specified initial capacity and | |
| 171 | * capacity increment. | |
| 172 | * | |
| 173 | * @param initialCapacity the initial capacity of the vector. | |
| 174 | * @param capacityIncrement the amount by which the capacity is | |
| 175 | * increased when the vector overflows. | |
| 176 | * @since JDK1.0 | |
| 177 | */ | |
| 178 | public Vector(int initialCapacity, int capacityIncrement) { | |
| 179 | 1 | super(); |
| 180 | 1 | this.elementData = new Object[initialCapacity]; |
| 181 | 1 | this.capacityIncrement = capacityIncrement; |
| 182 | // SELF_CHECKING | |
| 183 | 1 | _abstract = new absEmpty (0, capacityIncrement, initialCapacity); |
| 184 | 1 | verifyVectors("Vector("+initialCapacity+","+capacityIncrement+")"); |
| 185 | 1 | } |
| 186 | ||
| 187 | /** | |
| 188 | * Constructs an empty vector with the specified initial capacity. | |
| 189 | * | |
| 190 | * @param initialCapacity the initial capacity of the vector. | |
| 191 | * @since JDK1.0 | |
| 192 | */ | |
| 193 | public Vector(int initialCapacity) { | |
| 194 | 0 | this(initialCapacity, 0); |
| 195 | // SELF_CHECKING | |
| 196 | // skip it to optimize, since it is already performed in "this" | |
| 197 | // _abstract = new absEmpty (0, 0, initialCapacity); | |
| 198 | // verifyVectors("Vector("+initialCapacity+")"); | |
| 199 | 0 | } |
| 200 | ||
| 201 | /** | |
| 202 | * Constructs an empty vector. | |
| 203 | * | |
| 204 | * @since JDK1.0 | |
| 205 | */ | |
| 206 | public Vector() { | |
| 207 | 0 | this(10); |
| 208 | // SELF_CHECKING | |
| 209 | // skip it to optimize, since it is already performed in "this" | |
| 210 | // _abstract = new absEmpty (0, 0, 10); | |
| 211 | // verifyVectors("Vector()"); | |
| 212 | 0 | } |
| 213 | ||
| 214 | /** | |
| 215 | * Copies the components of this vector into the specified array. | |
| 216 | * The array must be big enough to hold all the objects in this vector. | |
| 217 | * | |
| 218 | * @param anArray the array into which the components get copied. | |
| 219 | * @since JDK1.0 | |
| 220 | */ | |
| 221 | public final synchronized void copyInto(Object anArray[]) { | |
| 222 | 0 | int i = elementCount; |
| 223 | 0 | while (i-- > 0) { |
| 224 | 0 | anArray[i] = elementData[i]; |
| 225 | } | |
| 226 | // SELF_CHECKING | |
| 227 | 0 | verifyArrays("copyInto", absVector.copyInto(_abstract), anArray); |
| 228 | 0 | } |
| 229 | ||
| 230 | /** | |
| 231 | * Trims the capacity of this vector to be the vector's current | |
| 232 | * size. An application can use this operation to minimize the | |
| 233 | * storage of a vector. | |
| 234 | * | |
| 235 | * @since JDK1.0 | |
| 236 | */ | |
| 237 | public final synchronized void trimToSize() { | |
| 238 | 38 | int oldCapacity = elementData.length; |
| 239 | 38 | if (elementCount < oldCapacity) { |
| 240 | 18 | Object oldData[] = elementData; |
| 241 | 18 | elementData = new Object[elementCount]; |
| 242 | 18 | System.arraycopy(oldData, 0, elementData, 0, elementCount); |
| 243 | } | |
| 244 | // SELF_CHECKING | |
| 245 | 38 | _abstract = absVector.trimToSize (_abstract); |
| 246 | 38 | verifyVectors ("trimToSize()"); |
| 247 | 38 | } |
| 248 | ||
| 249 | /** | |
| 250 | * Increases the capacity of this vector, if necessary, to ensure | |
| 251 | * that it can hold at least the number of components specified by | |
| 252 | * the minimum capacity argument. | |
| 253 | * | |
| 254 | * @param minCapacity the desired minimum capacity. | |
| 255 | * @since JDK1.0 | |
| 256 | */ | |
| 257 | public final synchronized void ensureCapacity(int minCapacity) { | |
| 258 | 31 | int oldCapacity = elementData.length; |
| 259 | 31 | if (minCapacity > oldCapacity) { |
| 260 | 16 | Object oldData[] = elementData; |
| 261 | 16 | int newCapacity = (capacityIncrement > 0) ? |
| 262 | (oldCapacity + capacityIncrement) : (oldCapacity * 2); | |
| 263 | 16 | if (newCapacity < minCapacity) { |
| 264 | 0 | newCapacity = minCapacity; |
| 265 | } | |
| 266 | 16 | elementData = new Object[newCapacity]; |
| 267 | 16 | System.arraycopy(oldData, 0, elementData, 0, elementCount); |
| 268 | } | |
| 269 | // SELF_CHECKING | |
| 270 | 31 | _abstract = absVector.ensureCapacity (minCapacity, _abstract); |
| 271 | 31 | verifyVectors ("ensureCapacity("+minCapacity+")"); |
| 272 | 31 | } |
| 273 | ||
| 274 | /** | |
| 275 | * Sets the size of this vector. If the new size is greater than the | |
| 276 | * current size, new <code>null</code> items are added to the end of | |
| 277 | * the vector. If the new size is less than the current size, all | |
| 278 | * components at index <code>newSize</code> and greater are discarded. | |
| 279 | * | |
| 280 | * @param newSize the new size of this vector. | |
| 281 | * @since JDK1.0 | |
| 282 | */ | |
| 283 | public final synchronized void setSize(int newSize) { | |
| 284 | 0 | if (newSize > elementCount) { |
| 285 | 0 | ensureCapacity(newSize); |
| 286 | } else { | |
| 287 | 0 | for (int i = newSize ; i < elementCount ; i++) { |
| 288 | 0 | elementData[i] = null; |
| 289 | } | |
| 290 | } | |
| 291 | 0 | elementCount = newSize; |
| 292 | // SELF_CHECKING | |
| 293 | 0 | _abstract = absVector.setSize (newSize, _abstract); |
| 294 | 0 | verifyVectors ("setSize("+newSize+")"); |
| 295 | 0 | } |
| 296 | ||
| 297 | /** | |
| 298 | * Returns the current capacity of this vector. | |
| 299 | * | |
| 300 | * @return the current capacity of this vector. | |
| 301 | * @since JDK1.0 | |
| 302 | */ | |
| 303 | public final int capacity() { | |
| 304 | // SELF_CHECKING | |
| 305 | 41 | verifyInts ("capacity", |
| 306 | absVector.capacity(_abstract), elementData.length); | |
| 307 | 41 | return elementData.length; |
| 308 | } | |
| 309 | ||
| 310 | /** | |
| 311 | * Returns the number of components in this vector. | |
| 312 | * | |
| 313 | * @return the number of components in this vector. | |
| 314 | * @since JDK1.0 | |
| 315 | */ | |
| 316 | public final int size() { | |
| 317 | // SELF_CHECKING | |
| 318 | 88 | verifyInts ("size", |
| 319 | absVector.size(_abstract), elementCount); | |
| 320 | 88 | return elementCount; |
| 321 | } | |
| 322 | ||
| 323 | /** | |
| 324 | * Tests if this vector has no components. | |
| 325 | * | |
| 326 | * @return <code>true</code> if this vector has no components; | |
| 327 | * <code>false</code> otherwise. | |
| 328 | * @since JDK1.0 | |
| 329 | */ | |
| 330 | public final boolean isEmpty() { | |
| 331 | // SELF_CHECKING | |
| 332 | 44 | verifyBooleans ("isEmpty", |
| 333 | absVector.size(_abstract) == 0, elementCount == 0); | |
| 334 | 44 | return elementCount == 0; |
| 335 | } | |
| 336 | ||
| 337 | /** | |
| 338 | * Returns an enumeration of the components of this vector. | |
| 339 | * | |
| 340 | * @return an enumeration of the components of this vector. | |
| 341 | * @see java.util.Enumeration | |
| 342 | * @since JDK1.0 | |
| 343 | */ | |
| 344 | public final synchronized Enumeration elements() { | |
| 345 | 199 | Enumeration to_be_returned = new VectorEnumerator(this); |
| 346 | 199 | verifyEnumerations ("elements", |
| 347 | absVector.elements(this, _abstract), to_be_returned); | |
| 348 | 199 | return to_be_returned; |
| 349 | } | |
| 350 | ||
| 351 | /** | |
| 352 | * Tests if the specified object is a component in this vector. | |
| 353 | * | |
| 354 | * @param elem an object. | |
| 355 | * @return <code>true</code> if the specified object is a component in | |
| 356 | * this vector; <code>false</code> otherwise. | |
| 357 | * @since JDK1.0 | |
| 358 | */ | |
| 359 | public final boolean contains(Object elem) { | |
| 360 | // SELF_CHECKING | |
| 361 | 39 | verifyBooleans ("contains", |
| 362 | absVector.contains(elem, _abstract), indexOf(elem, 0) >= 0); | |
| 363 | 39 | return indexOf(elem, 0) >= 0; |
| 364 | } | |
| 365 | ||
| 366 | /** | |
| 367 | * Searches for the first occurence of the given argument, testing | |
| 368 | * for equality using the <code>equals</code> method. | |
| 369 | * | |
| 370 | * @param elem an object. | |
| 371 | * @return the index of the first occurrence of the argument in this | |
| 372 | * vector; returns <code>-1</code> if the object is not found. | |
| 373 | * @see java.lang.Object#equals(java.lang.Object) | |
| 374 | * @since JDK1.0 | |
| 375 | */ | |
| 376 | public final int indexOf(Object elem) { | |
| 377 | // SELF_CHECKING | |
| 378 | 84 | int to_be_returned = indexOf(elem, 0); |
| 379 | 84 | verifyInts ("indexOf ("+elem.toString()+")", |
| 380 | absVector.indexOf(elem, _abstract), to_be_returned); | |
| 381 | 84 | return to_be_returned; |
| 382 | } | |
| 383 | ||
| 384 | /** | |
| 385 | * Searches for the first occurence of the given argument, beginning | |
| 386 | * the search at <code>index</code>, and testing for equality using | |
| 387 | * the <code>equals</code> method. | |
| 388 | * | |
| 389 | * @param elem an object. | |
| 390 | * @param index the index to start searching from. | |
| 391 | * @return the index of the first occurrence of the object argument in | |
| 392 | * this vector at position <code>index</code> or later in the | |
| 393 | * vector; returns <code>-1</code> if the object is not found. | |
| 394 | * @see java.lang.Object#equals(java.lang.Object) | |
| 395 | * @since JDK1.0 | |
| 396 | */ | |
| 397 | public final synchronized int indexOf(Object elem, int index) { | |
| 398 | // SELF_CHECKING | |
| 399 | 211 | int to_be_returned = -1; |
| 400 | 1223 | for (int i = index ; i < elementCount ; i++) { |
| 401 | 1071 | if (elem.equals(elementData[i])) { |
| 402 | 59 | to_be_returned = i; |
| 403 | 59 | break; |
| 404 | } | |
| 405 | } | |
| 406 | // SELF_CHECKING | |
| 407 | 211 | verifyInts ("indexOf ("+elem.toString()+","+index+")", |
| 408 | absVector.indexOf(elem, index, _abstract), to_be_returned); | |
| 409 | 211 | return to_be_returned; |
| 410 | } | |
| 411 | ||
| 412 | /** | |
| 413 | * Returns the index of the last occurrence of the specified object in | |
| 414 | * this vector. | |
| 415 | * | |
| 416 | * @param elem the desired component. | |
| 417 | * @return the index of the last occurrence of the specified object in | |
| 418 | * this vector; returns <code>-1</code> if the object is not found. | |
| 419 | * @since JDK1.0 | |
| 420 | */ | |
| 421 | public final int lastIndexOf(Object elem) { | |
| 422 | // SELF_CHECKING | |
| 423 | 45 | int to_be_returned = lastIndexOf(elem, elementCount-1); |
| 424 | 45 | verifyInts ("lastIndexOf ("+elem.toString()+")", |
| 425 | absVector.lastIndexOf(elem, _abstract), to_be_returned); | |
| 426 | 45 | return to_be_returned; |
| 427 | } | |
| 428 | ||
| 429 | /** | |
| 430 | * Searches backwards for the specified object, starting from the | |
| 431 | * specified index, and returns an index to it. | |
| 432 | * | |
| 433 | * @param elem the desired component. | |
| 434 | * @param index the index to start searching from. | |
| 435 | * @return the index of the last occurrence of the specified object in this | |
| 436 | * vector at position less than <code>index</code> in the vector; | |
| 437 | * <code>-1</code> if the object is not found. | |
| 438 | * @since JDK1.0 | |
| 439 | */ | |
| 440 | public final synchronized int lastIndexOf(Object elem, int index) { | |
| 441 | // SELF_CHECKING | |
| 442 | 90 | int to_be_returned = -1; |
| 443 | 330 | for (int i = index ; i >= 0 ; i--) { |
| 444 | 314 | if (elem.equals(elementData[i])) { |
| 445 | 30 | to_be_returned = i; |
| 446 | 30 | break; |
| 447 | } | |
| 448 | } | |
| 449 | // SELF_CHECKING | |
| 450 | 46 | verifyInts ("lastIndexOf ("+elem.toString()+","+index+")", |
| 451 | absVector.lastIndexOf(elem, index, _abstract), to_be_returned); | |
| 452 | 46 | return to_be_returned; |
| 453 | } | |
| 454 | ||
| 455 | /** | |
| 456 | * Returns the component at the specified index. | |
| 457 | * | |
| 458 | * @param index an index into this vector. | |
| 459 | * @return the component at the specified index. | |
| 460 | * @exception ArrayIndexOutOfBoundsException if an invalid index was | |
| 461 | * given. | |
| 462 | * @since JDK1.0 | |
| 463 | */ | |
| 464 | public final synchronized Object elementAt(int index) { | |
| 465 | 41 | if (index >= elementCount) { |
| 466 | 41 | throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
| 467 | } | |
| 468 | /* Since try/catch is free, except when the exception is thrown, | |
| 469 | put in this extra try/catch to catch negative indexes and | |
| 470 | display a more informative error message. This might not | |
| 471 | be appropriate, especially if we have a decent debugging | |
| 472 | environment - JP. */ | |
| 473 | try { | |
| 474 | // SELF_CHECKING | |
| 475 | 0 | verifyObjects ("elementAt ("+index+")", |
| 476 | absVector.elementAt(index, _abstract), elementData[index]); | |
| 477 | 0 | return elementData[index]; |
| 478 | 0 | } catch (ArrayIndexOutOfBoundsException e) { |
| 479 | 0 | throw new ArrayIndexOutOfBoundsException(index + " < 0"); |
| 480 | } | |
| 481 | } | |
| 482 | ||
| 483 | /** | |
| 484 | * Returns the first component of this vector. | |
| 485 | * | |
| 486 | * @return the first component of this vector. | |
| 487 | * @exception NoSuchElementException if this vector has no components. | |
| 488 | * @since JDK1.0 | |
| 489 | */ | |
| 490 | public final synchronized Object firstElement() { | |
| 491 | 36 | if (elementCount == 0) { |
| 492 | 0 | throw new NoSuchElementException(); |
| 493 | } | |
| 494 | // SELF_CHECKING | |
| 495 | 36 | verifyObjects ("firstElement ()", |
| 496 | absVector.firstElement(_abstract), elementData[0]); | |
| 497 | 36 | return elementData[0]; |
| 498 | } | |
| 499 | ||
| 500 | /** | |
| 501 | * Returns the last component of the vector. | |
| 502 | * | |
| 503 | * @return the last component of the vector, i.e., the component at index | |
| 504 | * <code>size() - 1</code>. | |
| 505 | * @exception NoSuchElementException if this vector is empty. | |
| 506 | * @since JDK1.0 | |
| 507 | */ | |
| 508 | public final synchronized Object lastElement() { | |
| 509 | 45 | if (elementCount == 0) { |
| 510 | 2 | throw new NoSuchElementException(); |
| 511 | } | |
| 512 | // SELF_CHECKING | |
| 513 | 43 | verifyObjects ("lastElement ()", |
| 514 | absVector.lastElement(_abstract), elementData[elementCount - 1]); | |
| 515 | 43 | return elementData[elementCount - 1]; |
| 516 | } | |
| 517 | ||
| 518 | /** | |
| 519 | * Sets the component at the specified <code>index</code> of this | |
| 520 | * vector to be the specified object. The previous component at that | |
| 521 | * position is discarded. | |
| 522 | * <p> | |
| 523 | * The index must be a value greater than or equal to <code>0</code> | |
| 524 | * and less than the current size of the vector. | |
| 525 | * | |
| 526 | * @param obj what the component is to be set to. | |
| 527 | * @param index the specified index. | |
| 528 | * @exception ArrayIndexOutOfBoundsException if the index was invalid. | |
| 529 | * @see java.util.Vector#size() | |
| 530 | * @since JDK1.0 | |
| 531 | */ | |
| 532 | public final synchronized void setElementAt(Object obj, int index) { | |
| 533 | 39 | if (index >= elementCount) { |
| 534 | 39 | throw new ArrayIndexOutOfBoundsException(index + " >= " + |
| 535 | elementCount); | |
| 536 | } | |
| 537 | 0 | elementData[index] = obj; |
| 538 | // SELF_CHECKING | |
| 539 | 0 | _abstract = absVector.setElementAt (obj, index, _abstract); |
| 540 | 0 | String tmp = obj == null ? "null" : obj.toString(); |
| 541 | 0 | verifyVectors ("setElementAt("+tmp+", "+index+")"); |
| 542 | 0 | } |
| 543 | ||
| 544 | /** | |
| 545 | * Deletes the component at the specified index. Each component in | |
| 546 | * this vector with an index greater or equal to the specified | |
| 547 | * <code>index</code> is shifted downward to have an index one | |
| 548 | * smaller than the value it had previously. | |
| 549 | * <p> | |
| 550 | * The index must be a value greater than or equal to <code>0</code> | |
| 551 | * and less than the current size of the vector. | |
| 552 | * | |
| 553 | * @param index the index of the object to remove. | |
| 554 | * @exception ArrayIndexOutOfBoundsException if the index was invalid. | |
| 555 | * @see java.util.Vector#size() | |
| 556 | * @since JDK1.0 | |
| 557 | */ | |
| 558 | public final synchronized void removeElementAt(int index) { | |
| 559 | 54 | if (index >= elementCount) { |
| 560 | 39 | throw new ArrayIndexOutOfBoundsException(index + " >= " + |
| 561 | elementCount); | |
| 562 | } | |
| 563 | 15 | else if (index < 0) { |
| 564 | 0 | throw new ArrayIndexOutOfBoundsException(index); |
| 565 | } | |
| 566 | 15 | int j = elementCount - index - 1; |
| 567 | 15 | if (j > 0) { |
| 568 | 13 | System.arraycopy(elementData, index + 1, elementData, index, j); |
| 569 | } | |
| 570 | 15 | elementCount--; |
| 571 | 15 | elementData[elementCount] = null; /* to let gc do its work */ |
| 572 | // SELF_CHECKING | |
| 573 | 15 | _abstract = absVector.removeElementAt (index, _abstract); |
| 574 | 15 | verifyVectors ("removeElementAt("+index+")"); |
| 575 | ||
| 576 | 15 | } |
| 577 | ||
| 578 | /** | |
| 579 | * Inserts the specified object as a component in this vector at the | |
| 580 | * specified <code>index</code>. Each component in this vector with | |
| 581 | * an index greater or equal to the specified <code>index</code> is | |
| 582 | * shifted upward to have an index one greater than the value it had | |
| 583 | * previously. | |
| 584 | * <p> | |
| 585 | * The index must be a value greater than or equal to <code>0</code> | |
| 586 | * and less than or equal to the current size of the vector. | |
| 587 | * | |
| 588 | * @param obj the component to insert. | |
| 589 | * @param index where to insert the new component. | |
| 590 | * @exception ArrayIndexOutOfBoundsException if the index was invalid. | |
| 591 | * @see java.util.Vector#size() | |
| 592 | * @since JDK1.0 | |
| 593 | */ | |
| 594 | public final synchronized void insertElementAt(Object obj, int index) { | |
| 595 | 39 | if (index >= elementCount + 1) { |
| 596 | 39 | throw new ArrayIndexOutOfBoundsException(index |
| 597 | + " > " + elementCount); | |
| 598 | } | |
| 599 | 0 | ensureCapacity(elementCount + 1); |
| 600 | 0 | System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
| 601 | 0 | elementData[index] = obj; |
| 602 | 0 | elementCount++; |
| 603 | // SELF_CHECKING | |
| 604 | 0 | _abstract = absVector.insertElementAt (obj, index, _abstract); |
| 605 | 0 | String tmp = obj == null ? "null" : obj.toString(); |
| 606 | 0 | verifyVectors ("insertElementAt("+tmp+", "+index+")"); |
| 607 | 0 | } |
| 608 | ||
| 609 | /** | |
| 610 | * Adds the specified component to the end of this vector, | |
| 611 | * increasing its size by one. The capacity of this vector is | |
| 612 | * increased if its size becomes greater than its capacity. | |
| 613 | * | |
| 614 | * @param obj the component to be added. | |
| 615 | * @since JDK1.0 | |
| 616 | */ | |
| 617 | public final synchronized void addElement(Object obj) { | |
| 618 | 31 | ensureCapacity(elementCount + 1); |
| 619 | 31 | elementData[elementCount++] = obj; |
| 620 | // SELF_CHECKING | |
| 621 | 31 | _abstract = absVector. addElement(obj, _abstract); |
| 622 | 31 | String tmp = obj == null ? "null" : obj.toString(); |
| 623 | 31 | verifyVectors ("addElement("+tmp+")"); |
| 624 | 31 | } |
| 625 | ||
| 626 | /** | |
| 627 | * Removes the first occurrence of the argument from this vector. If | |
| 628 | * the object is found in this vector, each component in the vector | |
| 629 | * with an index greater or equal to the object's index is shifted | |
| 630 | * downward to have an index one smaller than the value it had previously. | |
| 631 | * | |
| 632 | * @param obj the component to be removed. | |
| 633 | * @return <code>true</code> if the argument was a component of this | |
| 634 | * vector; <code>false</code> otherwise. | |
| 635 | * @since JDK1.0 | |
| 636 | */ | |
| 637 | public final synchronized boolean removeElement(Object obj) { | |
| 638 | // SELF_CHECKING | |
| 639 | 41 | String tmp = obj == null ? "null" : obj.toString(); |
| 640 | 41 | int i = indexOf(obj); |
| 641 | 41 | if (i >= 0) { |
| 642 | // SELF_CHECKING | |
| 643 | // Next call removeElementAt(i) changes _abstract | |
| 644 | // thus we check membership of object first | |
| 645 | // It would be preferable to return boolean and vector | |
| 646 | // simlutaneously rather than separately. | |
| 647 | 15 | verifyBooleans ("removeElement("+tmp+")", |
| 648 | absVector.removeElement(obj, _abstract), true); | |
| 649 | 15 | removeElementAt(i); |
| 650 | 15 | verifyVectors ("removeElementSide("+tmp+")"); |
| 651 | 15 | return true; |
| 652 | } else { | |
| 653 | // SELF_CHECKING | |
| 654 | 26 | verifyBooleans ("removeElement("+tmp+")", |
| 655 | absVector.removeElement(obj, _abstract), false); | |
| 656 | 26 | _abstract = absVector.removeElementSide (obj, _abstract); |
| 657 | 26 | verifyVectors ("removeElementSide("+tmp+")"); |
| 658 | 26 | return false; |
| 659 | } | |
| 660 | } | |
| 661 | ||
| 662 | /** | |
| 663 | * Removes all components from this vector and sets its size to zero. | |
| 664 | * | |
| 665 | * @since JDK1.0 | |
| 666 | */ | |
| 667 | public final synchronized void removeAllElements() { | |
| 668 | 0 | for (int i = 0; i < elementCount; i++) { |
| 669 | 0 | elementData[i] = null; |
| 670 | } | |
| 671 | 0 | elementCount = 0; |
| 672 | // SELF_CHECKING | |
| 673 | 0 | _abstract = absVector.removeAllElements (_abstract); |
| 674 | 0 | verifyVectors ("removeAllElements()"); |
| 675 | 0 | } |
| 676 | ||
| 677 | /** | |
| 678 | * Returns a clone of this vector. | |
| 679 | * | |
| 680 | * @return a clone of this vector. | |
| 681 | * @since JDK1.0 | |
| 682 | */ | |
| 683 | public synchronized Object clone() { | |
| 684 | try { | |
| 685 | 0 | Vector v = (Vector)super.clone(); |
| 686 | 0 | v.elementData = new Object[elementCount]; |
| 687 | 0 | System.arraycopy(elementData, 0, v.elementData, 0, elementCount); |
| 688 | // SELF_CHECKING | |
| 689 | 0 | _abstract = absVector.clone (_abstract); |
| 690 | 0 | verifyVectors ("clone()"); |
| 691 | 0 | return v; |
| 692 | 0 | } catch (CloneNotSupportedException e) { |
| 693 | // this shouldn't happen, since we are Cloneable | |
| 694 | 0 | throw new InternalError(); |
| 695 | } | |
| 696 | } | |
| 697 | ||
| 698 | /** | |
| 699 | * Returns a string representation of this vector. | |
| 700 | * | |
| 701 | * @return a string representation of this vector. | |
| 702 | * @since JDK1.0 | |
| 703 | */ | |
| 704 | public final synchronized String toString() { | |
| 705 | 42 | int max = size() - 1; |
| 706 | 42 | StringBuffer buf = new StringBuffer(); |
| 707 | 42 | Enumeration e = elements(); |
| 708 | 42 | buf.append("["); |
| 709 | ||
| 710 | 400 | for (int i = 0 ; i <= max ; i++) { |
| 711 | // NOTE: e can be null and an exception can be thrown | |
| 712 | 358 | String s = e.nextElement().toString(); |
| 713 | 358 | buf.append(s); |
| 714 | 358 | if (i < max) { |
| 715 | 320 | buf.append(", "); |
| 716 | } | |
| 717 | } | |
| 718 | 42 | buf.append("]"); |
| 719 | 42 | String to_be_returned = buf.toString(); |
| 720 | 42 | verifyStrings("toString", |
| 721 | absVector.toConcrString(_abstract), to_be_returned); | |
| 722 | 42 | return to_be_returned; |
| 723 | } | |
| 724 | } | |
| 725 | ||
| 726 | final | |
| 727 | class VectorEnumerator implements Enumeration { | |
| 728 | Vector vector; | |
| 729 | int count; | |
| 730 | ||
| 731 | 398 | VectorEnumerator(Vector v) { |
| 732 | 398 | vector = v; |
| 733 | 398 | count = 0; |
| 734 | 398 | } |
| 735 | ||
| 736 | public boolean hasMoreElements() { | |
| 737 | 0 | return count < vector.elementCount; |
| 738 | } | |
| 739 | ||
| 740 | public Object nextElement() { | |
| 741 | 1773 | synchronized (vector) { |
| 742 | 1773 | if (count < vector.elementCount) { |
| 743 | 1773 | return vector.elementData[count++]; |
| 744 | } | |
| 745 | 0 | } |
| 746 | 0 | throw new NoSuchElementException("VectorEnumerator"); |
| 747 | } | |
| 748 | } |