0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package java.util;
0019:
0020: import java.io.IOException;
0021: import java.io.ObjectOutputStream;
0022: import java.io.Serializable;
0023: import java.lang.reflect.Array;
0024:
0025: /**
0026: * Vector is a variable size contiguous indexable array of Objects. The size of
0027: * the Vector is the number of Objects it contains. The capacity of the Vector
0028: * is the number of Objects it can hold.
0029: * <p>
0030: * Objects may be inserted at any position up to the size of the Vector,
0031: * increasing the size of the Vector. Objects at any position in the Vector may
0032: * be removed, shrinking the size of the Vector. Objects at any position in the
0033: * Vector may be replaced, which does not affect the Vector size.
0034: * <p>
0035: * The capacity of a Vector may be specified when the Vector is created. If the
0036: * capacity of the Vector is exceeded, the capacity is increased, doubling by
0037: * default.
0038: *
0039: * @see java.lang.StringBuffer
0040: */
0041: public class Vector<E> extends AbstractList<E> implements List<E>,
0042: RandomAccess, Cloneable, Serializable {
0043:
0044: private static final long serialVersionUID = -2767605614048989439L;
0045:
0046: /**
0047: * The number of elements or the size of the vector.
0048: */
0049: protected int elementCount;
0050:
0051: /**
0052: * The elements of the vector.
0053: */
0054: protected Object[] elementData;
0055:
0056: /**
0057: * How many elements should be added to the vector when it is detected that
0058: * it needs to grow to accommodate extra entries.
0059: */
0060: protected int capacityIncrement;
0061:
0062: private static final int DEFAULT_SIZE = 10;
0063:
0064: /**
0065: * Constructs a new Vector using the default capacity.
0066: */
0067: public Vector() {
0068: this (DEFAULT_SIZE, 0);
0069: }
0070:
0071: /**
0072: * Constructs a new Vector using the specified capacity.
0073: *
0074: * @param capacity
0075: * the initial capacity of the new vector
0076: */
0077: public Vector(int capacity) {
0078: this (capacity, 0);
0079: }
0080:
0081: /**
0082: * Constructs a new Vector using the specified capacity and capacity
0083: * increment.
0084: *
0085: * @param capacity
0086: * the initial capacity of the new Vector
0087: * @param capacityIncrement
0088: * the amount to increase the capacity when this Vector is full
0089: */
0090: public Vector(int capacity, int capacityIncrement) {
0091: elementCount = 0;
0092: try {
0093: elementData = newElementArray(capacity);
0094: } catch (NegativeArraySizeException e) {
0095: throw new IllegalArgumentException();
0096: }
0097: this .capacityIncrement = capacityIncrement;
0098: }
0099:
0100: /**
0101: * Constructs a new instance of <code>Vector</code> containing the
0102: * elements in <code>collection</code>. The order of the elements in the
0103: * new <code>Vector</code> is dependent on the iteration order of the seed
0104: * collection.
0105: *
0106: * @param collection
0107: * the collection of elements to add
0108: */
0109: public Vector(Collection<? extends E> collection) {
0110: this (collection.size(), 0);
0111: Iterator<? extends E> it = collection.iterator();
0112: while (it.hasNext()) {
0113: elementData[elementCount++] = it.next();
0114: }
0115: }
0116:
0117: @SuppressWarnings("unchecked")
0118: private E[] newElementArray(int size) {
0119: return (E[]) new Object[size];
0120: }
0121:
0122: /**
0123: * Adds the specified object into this Vector at the specified location. The
0124: * object is inserted before any previous element at the specified location.
0125: * If the location is equal to the size of this Vector, the object is added
0126: * at the end.
0127: *
0128: * @param location
0129: * the index at which to insert the element
0130: * @param object
0131: * the object to insert in this Vector
0132: *
0133: * @exception ArrayIndexOutOfBoundsException
0134: * when <code>location < 0 || > size()</code>
0135: *
0136: * @see #addElement
0137: * @see #size
0138: */
0139: @Override
0140: public void add(int location, E object) {
0141: insertElementAt(object, location);
0142: }
0143:
0144: /**
0145: * Adds the specified object at the end of this Vector.
0146: *
0147: * @param object
0148: * the object to add to the Vector
0149: * @return true
0150: */
0151: @Override
0152: public synchronized boolean add(E object) {
0153: if (elementCount == elementData.length) {
0154: growByOne();
0155: }
0156: elementData[elementCount++] = object;
0157: modCount++;
0158: return true;
0159: }
0160:
0161: /**
0162: * Inserts the objects in the specified Collection at the specified location
0163: * in this Vector. The objects are inserted in the order in which they are
0164: * returned from the Collection iterator.
0165: *
0166: * @param location
0167: * the location to insert the objects
0168: * @param collection
0169: * the Collection of objects
0170: * @return true if this Vector is modified, false otherwise
0171: *
0172: * @exception ArrayIndexOutOfBoundsException
0173: * when <code>location < 0</code> or
0174: * <code>location > size()</code>
0175: */
0176: @Override
0177: public synchronized boolean addAll(int location,
0178: Collection<? extends E> collection) {
0179: if (0 <= location && location <= elementCount) {
0180: int size = collection.size();
0181: if (size == 0) {
0182: return false;
0183: }
0184: int required = size - (elementData.length - elementCount);
0185: if (required > 0) {
0186: growBy(required);
0187: }
0188: int count = elementCount - location;
0189: if (count > 0) {
0190: System.arraycopy(elementData, location, elementData,
0191: location + size, count);
0192: }
0193: Iterator<? extends E> it = collection.iterator();
0194: while (it.hasNext()) {
0195: elementData[location++] = it.next();
0196: }
0197: elementCount += size;
0198: modCount++;
0199: return true;
0200: }
0201: throw new ArrayIndexOutOfBoundsException(location);
0202: }
0203:
0204: /**
0205: * Adds the objects in the specified Collection to the end of this Vector.
0206: *
0207: * @param collection
0208: * the Collection of objects
0209: * @return true if this Vector is modified, false otherwise
0210: */
0211: @Override
0212: public synchronized boolean addAll(
0213: Collection<? extends E> collection) {
0214: return addAll(elementCount, collection);
0215: }
0216:
0217: /**
0218: * Adds the specified object at the end of this Vector.
0219: *
0220: * @param object
0221: * the object to add to the Vector
0222: */
0223: public synchronized void addElement(E object) {
0224: if (elementCount == elementData.length) {
0225: growByOne();
0226: }
0227: elementData[elementCount++] = object;
0228: modCount++;
0229: }
0230:
0231: /**
0232: * Answers the number of elements this Vector can hold without growing.
0233: *
0234: * @return the capacity of this Vector
0235: *
0236: * @see #ensureCapacity
0237: * @see #size
0238: */
0239: public synchronized int capacity() {
0240: return elementData.length;
0241: }
0242:
0243: /**
0244: * Removes all elements from this Vector, leaving it empty.
0245: *
0246: * @see #isEmpty
0247: * @see #size
0248: */
0249: @Override
0250: public void clear() {
0251: removeAllElements();
0252: }
0253:
0254: /**
0255: * Answers a new Vector with the same elements, size, capacity and capacity
0256: * increment as this Vector.
0257: *
0258: * @return a shallow copy of this Vector
0259: *
0260: * @see java.lang.Cloneable
0261: */
0262: @Override
0263: @SuppressWarnings("unchecked")
0264: public synchronized Object clone() {
0265: try {
0266: Vector<E> vector = (Vector<E>) super .clone();
0267: vector.elementData = elementData.clone();
0268: return vector;
0269: } catch (CloneNotSupportedException e) {
0270: return null;
0271: }
0272: }
0273:
0274: /**
0275: * Searches this Vector for the specified object.
0276: *
0277: * @param object
0278: * the object to look for in this Vector
0279: * @return true if object is an element of this Vector, false otherwise
0280: *
0281: * @see #indexOf(Object)
0282: * @see #indexOf(Object, int)
0283: * @see java.lang.Object#equals
0284: */
0285: @Override
0286: public boolean contains(Object object) {
0287: return indexOf(object, 0) != -1;
0288: }
0289:
0290: /**
0291: * Searches this Vector for all objects in the specified Collection.
0292: *
0293: * @param collection
0294: * the Collection of objects
0295: * @return true if all objects in the specified Collection are elements of
0296: * this Vector, false otherwise
0297: */
0298: @Override
0299: public synchronized boolean containsAll(Collection<?> collection) {
0300: return super .containsAll(collection);
0301: }
0302:
0303: /**
0304: * Attempts to copy elements contained by this <code>Vector</code> into
0305: * the corresponding elements of the supplied <code>Object</code> array.
0306: *
0307: * @param elements
0308: * the <code>Object</code> array into which the elements of
0309: * this Vector are copied
0310: *
0311: * @see #clone
0312: */
0313: public synchronized void copyInto(Object[] elements) {
0314: System.arraycopy(elementData, 0, elements, 0, elementCount);
0315: }
0316:
0317: /**
0318: * Answers the element at the specified location in this Vector.
0319: *
0320: * @param location
0321: * the index of the element to return in this Vector
0322: * @return the element at the specified location
0323: *
0324: * @exception ArrayIndexOutOfBoundsException
0325: * when <code>location < 0 || >= size()</code>
0326: *
0327: * @see #size
0328: */
0329: @SuppressWarnings("unchecked")
0330: public synchronized E elementAt(int location) {
0331: if (location < elementCount) {
0332: return (E) elementData[location];
0333: }
0334: throw new ArrayIndexOutOfBoundsException(location);
0335: }
0336:
0337: /**
0338: * Answers an Enumeration on the elements of this Vector. The results of the
0339: * Enumeration may be affected if the contents of this Vector are modified.
0340: *
0341: * @return an Enumeration of the elements of this Vector
0342: *
0343: * @see #elementAt
0344: * @see Enumeration
0345: */
0346: public Enumeration<E> elements() {
0347: return new Enumeration<E>() {
0348: int pos = 0;
0349:
0350: public boolean hasMoreElements() {
0351: return pos < elementCount;
0352: }
0353:
0354: @SuppressWarnings("unchecked")
0355: public E nextElement() {
0356: synchronized (Vector.this ) {
0357: if (pos < elementCount) {
0358: return (E) elementData[pos++];
0359: }
0360: }
0361: throw new NoSuchElementException();
0362: }
0363: };
0364: }
0365:
0366: /**
0367: * Ensures that this Vector can hold the specified number of elements
0368: * without growing.
0369: *
0370: * @param minimumCapacity
0371: * the minimum number of elements that this vector will hold
0372: * before growing
0373: *
0374: * @see #capacity
0375: */
0376: public synchronized void ensureCapacity(int minimumCapacity) {
0377: if (elementData.length < minimumCapacity) {
0378: int next = (capacityIncrement <= 0 ? elementData.length
0379: : capacityIncrement)
0380: + elementData.length;
0381: grow(minimumCapacity > next ? minimumCapacity : next);
0382: }
0383: }
0384:
0385: /**
0386: * Compares the specified object to this Vector and answer if they are
0387: * equal. The object must be a List which contains the same objects in the
0388: * same order.
0389: *
0390: * @param object
0391: * the object to compare with this object
0392: * @return true if the specified object is equal to this Vector, false
0393: * otherwise
0394: *
0395: * @see #hashCode
0396: */
0397: @Override
0398: public synchronized boolean equals(Object object) {
0399: if (this == object) {
0400: return true;
0401: }
0402: if (object instanceof List) {
0403: List<?> list = (List<?>) object;
0404: if (list.size() != elementCount) {
0405: return false;
0406: }
0407:
0408: int index = 0;
0409: Iterator<?> it = list.iterator();
0410: while (it.hasNext()) {
0411: Object e1 = elementData[index++], e2 = it.next();
0412: if (!(e1 == null ? e2 == null : e1.equals(e2))) {
0413: return false;
0414: }
0415: }
0416: return true;
0417: }
0418: return false;
0419: }
0420:
0421: /**
0422: * Answers the first element in this Vector.
0423: *
0424: * @return the element at the first position
0425: *
0426: * @exception NoSuchElementException
0427: * when this vector is empty
0428: *
0429: * @see #elementAt
0430: * @see #lastElement
0431: * @see #size
0432: */
0433: @SuppressWarnings("unchecked")
0434: public synchronized E firstElement() {
0435: if (elementCount > 0) {
0436: return (E) elementData[0];
0437: }
0438: throw new NoSuchElementException();
0439: }
0440:
0441: /**
0442: * Answers the element at the specified location in this Vector.
0443: *
0444: * @param location
0445: * the index of the element to return in this Vector
0446: * @return the element at the specified location
0447: *
0448: * @exception ArrayIndexOutOfBoundsException
0449: * when <code>location < 0 || >= size()</code>
0450: *
0451: * @see #size
0452: */
0453: @Override
0454: public E get(int location) {
0455: return elementAt(location);
0456: }
0457:
0458: private void grow(int newCapacity) {
0459: E[] newData = newElementArray(newCapacity);
0460: // Assumes elementCount is <= newCapacity
0461: assert elementCount <= newCapacity;
0462: System.arraycopy(elementData, 0, newData, 0, elementCount);
0463: elementData = newData;
0464: }
0465:
0466: /**
0467: * JIT optimization
0468: */
0469: private void growByOne() {
0470: int adding = 0;
0471: if (capacityIncrement <= 0) {
0472: if ((adding = elementData.length) == 0) {
0473: adding = 1;
0474: }
0475: } else {
0476: adding = capacityIncrement;
0477: }
0478:
0479: E[] newData = newElementArray(elementData.length + adding);
0480: System.arraycopy(elementData, 0, newData, 0, elementCount);
0481: elementData = newData;
0482: }
0483:
0484: private void growBy(int required) {
0485: int adding = 0;
0486: if (capacityIncrement <= 0) {
0487: if ((adding = elementData.length) == 0) {
0488: adding = required;
0489: }
0490: while (adding < required) {
0491: adding += adding;
0492: }
0493: } else {
0494: adding = (required / capacityIncrement) * capacityIncrement;
0495: if (adding < required) {
0496: adding += capacityIncrement;
0497: }
0498: }
0499: E[] newData = newElementArray(elementData.length + adding);
0500: System.arraycopy(elementData, 0, newData, 0, elementCount);
0501: elementData = newData;
0502: }
0503:
0504: /**
0505: * Answers an integer hash code for the receiver. Objects which are equal
0506: * answer the same value for this method.
0507: *
0508: * @return the receiver's hash
0509: *
0510: * @see #equals
0511: */
0512: @Override
0513: public synchronized int hashCode() {
0514: int result = 1;
0515: for (int i = 0; i < elementCount; i++) {
0516: result = (31 * result)
0517: + (elementData[i] == null ? 0 : elementData[i]
0518: .hashCode());
0519: }
0520: return result;
0521: }
0522:
0523: /**
0524: * Searches in this Vector for the index of the specified object. The search
0525: * for the object starts at the beginning and moves towards the end of this
0526: * Vector.
0527: *
0528: * @param object
0529: * the object to find in this Vector
0530: * @return the index in this Vector of the specified element, -1 if the
0531: * element isn't found
0532: *
0533: * @see #contains
0534: * @see #lastIndexOf(Object)
0535: * @see #lastIndexOf(Object, int)
0536: */
0537: @Override
0538: public int indexOf(Object object) {
0539: return indexOf(object, 0);
0540: }
0541:
0542: /**
0543: * Searches in this Vector for the index of the specified object. The search
0544: * for the object starts at the specified location and moves towards the end
0545: * of this Vector.
0546: *
0547: * @param object
0548: * the object to find in this Vector
0549: * @param location
0550: * the index at which to start searching
0551: * @return the index in this Vector of the specified element, -1 if the
0552: * element isn't found
0553: *
0554: * @exception ArrayIndexOutOfBoundsException
0555: * when <code>location < 0</code>
0556: *
0557: * @see #contains
0558: * @see #lastIndexOf(Object)
0559: * @see #lastIndexOf(Object, int)
0560: */
0561: public synchronized int indexOf(Object object, int location) {
0562: if (object != null) {
0563: for (int i = location; i < elementCount; i++) {
0564: if (object.equals(elementData[i])) {
0565: return i;
0566: }
0567: }
0568: } else {
0569: for (int i = location; i < elementCount; i++) {
0570: if (elementData[i] == null) {
0571: return i;
0572: }
0573: }
0574: }
0575: return -1;
0576: }
0577:
0578: /**
0579: * Inserts the specified object into this Vector at the specified location.
0580: * This object is inserted before any previous element at the specified
0581: * location. If the location is equal to the size of this Vector, the object
0582: * is added at the end.
0583: *
0584: * @param object
0585: * the object to insert in this Vector
0586: * @param location
0587: * the index at which to insert the element
0588: *
0589: * @exception ArrayIndexOutOfBoundsException
0590: * when <code>location < 0 || > size()</code>
0591: *
0592: * @see #addElement
0593: * @see #size
0594: */
0595: public synchronized void insertElementAt(E object, int location) {
0596: if (0 <= location && location <= elementCount) {
0597: if (elementCount == elementData.length) {
0598: growByOne();
0599: }
0600: int count = elementCount - location;
0601: if (count > 0) {
0602: System.arraycopy(elementData, location, elementData,
0603: location + 1, count);
0604: }
0605: elementData[location] = object;
0606: elementCount++;
0607: modCount++;
0608: } else {
0609: throw new ArrayIndexOutOfBoundsException(location);
0610: }
0611: }
0612:
0613: /**
0614: * Answers if this Vector has no elements, a size of zero.
0615: *
0616: * @return true if this Vector has no elements, false otherwise
0617: *
0618: * @see #size
0619: */
0620: @Override
0621: public synchronized boolean isEmpty() {
0622: return elementCount == 0;
0623: }
0624:
0625: /**
0626: * Answers the last element in this Vector.
0627: *
0628: * @return the element at the last position
0629: *
0630: * @exception NoSuchElementException
0631: * when this vector is empty
0632: *
0633: * @see #elementAt
0634: * @see #firstElement
0635: * @see #size
0636: */
0637: @SuppressWarnings("unchecked")
0638: public synchronized E lastElement() {
0639: try {
0640: return (E) elementData[elementCount - 1];
0641: } catch (IndexOutOfBoundsException e) {
0642: throw new NoSuchElementException();
0643: }
0644: }
0645:
0646: /**
0647: * Searches in this Vector for the index of the specified object. The search
0648: * for the object starts at the end and moves towards the start of this
0649: * Vector.
0650: *
0651: * @param object
0652: * the object to find in this Vector
0653: * @return the index in this Vector of the specified element, -1 if the
0654: * element isn't found
0655: *
0656: * @see #contains
0657: * @see #indexOf(Object)
0658: * @see #indexOf(Object, int)
0659: */
0660: @Override
0661: public synchronized int lastIndexOf(Object object) {
0662: return lastIndexOf(object, elementCount - 1);
0663: }
0664:
0665: /**
0666: * Searches in this Vector for the index of the specified object. The search
0667: * for the object starts at the specified location and moves towards the
0668: * start of this Vector.
0669: *
0670: * @param object
0671: * the object to find in this Vector
0672: * @param location
0673: * the index at which to start searching
0674: * @return the index in this Vector of the specified element, -1 if the
0675: * element isn't found
0676: *
0677: * @exception ArrayIndexOutOfBoundsException
0678: * when <code>location >= size()</code>
0679: *
0680: * @see #contains
0681: * @see #indexOf(Object)
0682: * @see #indexOf(Object, int)
0683: */
0684: public synchronized int lastIndexOf(Object object, int location) {
0685: if (location < elementCount) {
0686: if (object != null) {
0687: for (int i = location; i >= 0; i--) {
0688: if (object.equals(elementData[i])) {
0689: return i;
0690: }
0691: }
0692: } else {
0693: for (int i = location; i >= 0; i--) {
0694: if (elementData[i] == null) {
0695: return i;
0696: }
0697: }
0698: }
0699: return -1;
0700: }
0701: throw new ArrayIndexOutOfBoundsException(location);
0702: }
0703:
0704: /*
0705: * (non-Javadoc)
0706: *
0707: * @see java.util.List#remove(int)
0708: */
0709: @SuppressWarnings("unchecked")
0710: @Override
0711: public synchronized E remove(int location) {
0712: if (location < elementCount) {
0713: E result = (E) elementData[location];
0714: elementCount--;
0715: int size = elementCount - location;
0716: if (size > 0) {
0717: System.arraycopy(elementData, location + 1,
0718: elementData, location, size);
0719: }
0720: elementData[elementCount] = null;
0721: modCount++;
0722: return result;
0723: }
0724: throw new ArrayIndexOutOfBoundsException(location);
0725: }
0726:
0727: /**
0728: * Removes the first occurrence, starting at the beginning and moving
0729: * towards the end, of the specified object from this Vector.
0730: *
0731: * @param object
0732: * the object to remove from this Vector
0733: * @return true if the specified object was found, false otherwise
0734: *
0735: * @see #removeAllElements
0736: * @see #removeElementAt
0737: * @see #size
0738: */
0739: @Override
0740: public boolean remove(Object object) {
0741: return removeElement(object);
0742: }
0743:
0744: /**
0745: * Removes all occurrences in this Vector of each object in the specified
0746: * Collection.
0747: *
0748: * @param collection
0749: * the Collection of objects to remove
0750: * @return true if this Vector is modified, false otherwise
0751: */
0752: @Override
0753: public synchronized boolean removeAll(Collection<?> collection) {
0754: return super .removeAll(collection);
0755: }
0756:
0757: /**
0758: * Removes all elements from this Vector, leaving the size zero and the
0759: * capacity unchanged.
0760: *
0761: * @see #isEmpty
0762: * @see #size
0763: */
0764: public synchronized void removeAllElements() {
0765: Arrays.fill(elementData, 0, elementCount, null);
0766: modCount++;
0767: elementCount = 0;
0768: }
0769:
0770: /**
0771: * Removes the first occurrence, starting at the beginning and moving
0772: * towards the end, of the specified object from this Vector.
0773: *
0774: * @param object
0775: * the object to remove from this Vector
0776: * @return true if the specified object was found, false otherwise
0777: *
0778: * @see #removeAllElements
0779: * @see #removeElementAt
0780: * @see #size
0781: */
0782: public synchronized boolean removeElement(Object object) {
0783: int index;
0784: if ((index = indexOf(object, 0)) == -1) {
0785: return false;
0786: }
0787: removeElementAt(index);
0788: return true;
0789: }
0790:
0791: /**
0792: * Removes the element found at index position <code>location</code> from
0793: * this <code>Vector</code> and decrements the size accordingly.
0794: *
0795: * @param location
0796: * the index of the element to remove
0797: *
0798: * @exception ArrayIndexOutOfBoundsException
0799: * when <code>location < 0 || >= size()</code>
0800: *
0801: * @see #removeElement
0802: * @see #removeAllElements
0803: * @see #size
0804: */
0805: public synchronized void removeElementAt(int location) {
0806: if (0 <= location && location < elementCount) {
0807: elementCount--;
0808: int size = elementCount - location;
0809: if (size > 0) {
0810: System.arraycopy(elementData, location + 1,
0811: elementData, location, size);
0812: }
0813: elementData[elementCount] = null;
0814: modCount++;
0815: } else {
0816: throw new ArrayIndexOutOfBoundsException(location);
0817: }
0818: }
0819:
0820: /**
0821: * Removes the objects in the specified range from the start to the, but not
0822: * including, end index.
0823: *
0824: * @param start
0825: * the index at which to start removing
0826: * @param end
0827: * the index one past the end of the range to remove
0828: *
0829: * @exception IndexOutOfBoundsException
0830: * when <code>start < 0, start > end</code> or
0831: * <code>end > size()</code>
0832: */
0833: @Override
0834: protected void removeRange(int start, int end) {
0835: if (start >= 0 && start <= end && end <= elementCount) {
0836: if (start == end) {
0837: return;
0838: }
0839: if (end != elementCount) {
0840: System.arraycopy(elementData, end, elementData, start,
0841: elementCount - end);
0842: int newCount = elementCount - (end - start);
0843: Arrays.fill(elementData, newCount, elementCount, null);
0844: elementCount = newCount;
0845: } else {
0846: Arrays.fill(elementData, start, elementCount, null);
0847: elementCount = start;
0848: }
0849: modCount++;
0850: } else {
0851: throw new IndexOutOfBoundsException();
0852: }
0853: }
0854:
0855: /**
0856: * Removes all objects from this Vector that are not contained in the
0857: * specified Collection.
0858: *
0859: * @param collection
0860: * the Collection of objects to retain
0861: * @return true if this Vector is modified, false otherwise
0862: */
0863: @Override
0864: public synchronized boolean retainAll(Collection<?> collection) {
0865: return super .retainAll(collection);
0866: }
0867:
0868: /**
0869: * Replaces the element at the specified location in this Vector with the
0870: * specified object.
0871: *
0872: * @param location
0873: * the index at which to put the specified object
0874: * @param object
0875: * the object to add to this Vector
0876: * @return the previous element at the location
0877: *
0878: * @exception ArrayIndexOutOfBoundsException
0879: * when <code>location < 0 || >= size()</code>
0880: *
0881: * @see #size
0882: */
0883: @SuppressWarnings("unchecked")
0884: @Override
0885: public synchronized E set(int location, E object) {
0886: if (location < elementCount) {
0887: E result = (E) elementData[location];
0888: elementData[location] = object;
0889: return result;
0890: }
0891: throw new ArrayIndexOutOfBoundsException(location);
0892: }
0893:
0894: /**
0895: * Replaces the element at the specified location in this Vector with the
0896: * specified object.
0897: *
0898: * @param object
0899: * the object to add to this Vector
0900: * @param location
0901: * the index at which to put the specified object
0902: *
0903: * @exception ArrayIndexOutOfBoundsException
0904: * when <code>location < 0 || >= size()</code>
0905: *
0906: * @see #size
0907: */
0908: public synchronized void setElementAt(E object, int location) {
0909: if (location < elementCount) {
0910: elementData[location] = object;
0911: } else {
0912: throw new ArrayIndexOutOfBoundsException(location);
0913: }
0914: }
0915:
0916: /**
0917: * Sets the size of this Vector to the specified size. If there are more
0918: * than length elements in this Vector, the elements at end are lost. If
0919: * there are less than length elements in the Vector, the additional
0920: * elements contain null.
0921: *
0922: * @param length
0923: * the new size of this Vector
0924: *
0925: * @see #size
0926: */
0927: public synchronized void setSize(int length) {
0928: if (length == elementCount) {
0929: return;
0930: }
0931: ensureCapacity(length);
0932: if (elementCount > length) {
0933: Arrays.fill(elementData, length, elementCount, null);
0934: }
0935: elementCount = length;
0936: modCount++;
0937: }
0938:
0939: /**
0940: * Answers the number of elements in this Vector.
0941: *
0942: * @return the number of elements in this Vector
0943: *
0944: * @see #elementCount
0945: * @see #lastElement
0946: */
0947: @Override
0948: public synchronized int size() {
0949: return elementCount;
0950: }
0951:
0952: /**
0953: * Answers a List of the specified portion of this Vector from the start
0954: * index to one less than the end index. The returned List is backed by this
0955: * Vector so changes to one are reflected by the other.
0956: *
0957: * @param start
0958: * the index at which to start the sublist
0959: * @param end
0960: * the index one past the end of the sublist
0961: * @return a List of a portion of this Vector
0962: *
0963: * @exception IndexOutOfBoundsException
0964: * when <code>start < 0 or <code>end > size()</code>
0965: * @exception IllegalArgumentException when <code>start > end</code>
0966: */
0967: @Override
0968: public synchronized List<E> subList(int start, int end) {
0969: return new Collections.SynchronizedRandomAccessList<E>(super
0970: .subList(start, end), this );
0971: }
0972:
0973: /**
0974: * Answers a new array containing all elements contained in this Vector.
0975: *
0976: * @return an array of the elements from this Vector
0977: */
0978: @Override
0979: public synchronized Object[] toArray() {
0980: Object[] result = new Object[elementCount];
0981: System.arraycopy(elementData, 0, result, 0, elementCount);
0982: return result;
0983: }
0984:
0985: /**
0986: * Answers an array containing all elements contained in this Vector. If the
0987: * specified array is large enough to hold the elements, the specified array
0988: * is used, otherwise an array of the same type is created. If the specified
0989: * array is used and is larger than this Vector, the array element following
0990: * the collection elements is set to null.
0991: *
0992: * @param contents
0993: * the array
0994: * @return an array of the elements from this Vector
0995: *
0996: * @exception ArrayStoreException
0997: * when the type of an element in this Vector cannot be
0998: * stored in the type of the specified array
0999: */
1000: @Override
1001: @SuppressWarnings("unchecked")
1002: public synchronized <T> T[] toArray(T[] contents) {
1003: if (elementCount > contents.length) {
1004: Class<?> ct = contents.getClass().getComponentType();
1005: contents = (T[]) Array.newInstance(ct, elementCount);
1006: }
1007: System.arraycopy(elementData, 0, contents, 0, elementCount);
1008: if (elementCount < contents.length) {
1009: contents[elementCount] = null;
1010: }
1011: return contents;
1012: }
1013:
1014: /**
1015: * Answers the string representation of this Vector.
1016: *
1017: * @return the string representation of this Vector
1018: *
1019: * @see #elements
1020: */
1021: @Override
1022: public synchronized String toString() {
1023: if (elementCount == 0) {
1024: return "[]"; //$NON-NLS-1$
1025: }
1026: int length = elementCount - 1;
1027: StringBuffer buffer = new StringBuffer(elementCount * 16);
1028: buffer.append('[');
1029: for (int i = 0; i < length; i++) {
1030: if (elementData[i] == this ) {
1031: buffer.append("(this Collection)"); //$NON-NLS-1$
1032: } else {
1033: buffer.append(elementData[i]);
1034: }
1035: buffer.append(", "); //$NON-NLS-1$
1036: }
1037: if (elementData[length] == this ) {
1038: buffer.append("(this Collection)"); //$NON-NLS-1$
1039: } else {
1040: buffer.append(elementData[length]);
1041: }
1042: buffer.append(']');
1043: return buffer.toString();
1044: }
1045:
1046: /**
1047: * Sets the capacity of this Vector to be the same as the size.
1048: *
1049: * @see #capacity
1050: * @see #ensureCapacity
1051: * @see #size
1052: */
1053: public synchronized void trimToSize() {
1054: if (elementData.length != elementCount) {
1055: grow(elementCount);
1056: }
1057: }
1058:
1059: private synchronized void writeObject(ObjectOutputStream stream)
1060: throws IOException {
1061: stream.defaultWriteObject();
1062: }
1063: }
|