home *** CD-ROM | disk | FTP | other *** search
- package java.util;
-
- public class Vector implements Cloneable {
- protected Object[] elementData;
- protected int elementCount;
- protected int capacityIncrement;
-
- public Vector(int initialCapacity, int capacityIncrement) {
- this.elementData = new Object[initialCapacity];
- this.capacityIncrement = capacityIncrement;
- }
-
- public Vector(int initialCapacity) {
- this(initialCapacity, 0);
- }
-
- public Vector() {
- this(10);
- }
-
- public final synchronized void copyInto(Object[] anArray) {
- for(int i = this.elementCount; i-- > 0; anArray[i] = this.elementData[i]) {
- }
-
- }
-
- public final synchronized void trimToSize() {
- int oldCapacity = this.elementData.length;
- if (this.elementCount < oldCapacity) {
- Object[] oldData = this.elementData;
- this.elementData = new Object[this.elementCount];
- System.arraycopy(oldData, 0, this.elementData, 0, this.elementCount);
- }
-
- }
-
- public final synchronized void ensureCapacity(int minCapacity) {
- int oldCapacity = this.elementData.length;
- if (minCapacity > oldCapacity) {
- Object[] oldData = this.elementData;
- int newCapacity = this.capacityIncrement > 0 ? oldCapacity + this.capacityIncrement : oldCapacity * 2;
- if (newCapacity < minCapacity) {
- newCapacity = minCapacity;
- }
-
- this.elementData = new Object[newCapacity];
- System.arraycopy(oldData, 0, this.elementData, 0, this.elementCount);
- }
-
- }
-
- public final synchronized void setSize(int newSize) {
- if (newSize > this.elementCount) {
- this.ensureCapacity(newSize);
- } else {
- for(int i = newSize; i < this.elementCount; ++i) {
- this.elementData[i] = null;
- }
- }
-
- this.elementCount = newSize;
- }
-
- public final int capacity() {
- return this.elementData.length;
- }
-
- public final int size() {
- return this.elementCount;
- }
-
- public final boolean isEmpty() {
- return this.elementCount == 0;
- }
-
- public final synchronized Enumeration elements() {
- return new VectorEnumerator(this);
- }
-
- public final boolean contains(Object elem) {
- return this.indexOf(elem, 0) >= 0;
- }
-
- public final int indexOf(Object elem) {
- return this.indexOf(elem, 0);
- }
-
- public final synchronized int indexOf(Object elem, int index) {
- for(int i = index; i < this.elementCount; ++i) {
- if (elem.equals(this.elementData[i])) {
- return i;
- }
- }
-
- return -1;
- }
-
- public final int lastIndexOf(Object elem) {
- return this.lastIndexOf(elem, this.elementCount);
- }
-
- public final synchronized int lastIndexOf(Object elem, int index) {
- int i = index;
-
- do {
- --i;
- if (i < 0) {
- return -1;
- }
- } while(!elem.equals(this.elementData[i]));
-
- return i;
- }
-
- public final synchronized Object elementAt(int index) {
- if (index >= this.elementCount) {
- throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount);
- } else {
- try {
- return this.elementData[index];
- } catch (ArrayIndexOutOfBoundsException var2) {
- throw new ArrayIndexOutOfBoundsException(index + " < 0");
- }
- }
- }
-
- public final synchronized Object firstElement() {
- if (this.elementCount == 0) {
- throw new NoSuchElementException();
- } else {
- return this.elementData[0];
- }
- }
-
- public final synchronized Object lastElement() {
- if (this.elementCount == 0) {
- throw new NoSuchElementException();
- } else {
- return this.elementData[this.elementCount - 1];
- }
- }
-
- public final synchronized void setElementAt(Object obj, int index) {
- if (index >= this.elementCount) {
- throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount);
- } else {
- this.elementData[index] = obj;
- }
- }
-
- public final synchronized void removeElementAt(int index) {
- if (index >= this.elementCount) {
- throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount);
- } else {
- int j = this.elementCount - index - 1;
- if (j > 0) {
- System.arraycopy(this.elementData, index + 1, this.elementData, index, j);
- }
-
- --this.elementCount;
- this.elementData[this.elementCount] = null;
- }
- }
-
- public final synchronized void insertElementAt(Object obj, int index) {
- if (index >= this.elementCount + 1) {
- throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount + 1);
- } else {
- this.ensureCapacity(this.elementCount + 1);
- System.arraycopy(this.elementData, index, this.elementData, index + 1, this.elementCount - index);
- this.elementData[index] = obj;
- ++this.elementCount;
- }
- }
-
- public final synchronized void addElement(Object obj) {
- this.ensureCapacity(this.elementCount + 1);
- this.elementData[this.elementCount++] = obj;
- }
-
- public final synchronized boolean removeElement(Object obj) {
- int i = this.indexOf(obj);
- if (i >= 0) {
- this.removeElementAt(i);
- return true;
- } else {
- return false;
- }
- }
-
- public final synchronized void removeAllElements() {
- for(int i = 0; i < this.elementCount; ++i) {
- this.elementData[i] = null;
- }
-
- this.elementCount = 0;
- }
-
- public synchronized Object clone() {
- try {
- Vector v = (Vector)super.clone();
- v.elementData = new Object[this.elementCount];
- System.arraycopy(this.elementData, 0, v.elementData, 0, this.elementCount);
- return v;
- } catch (CloneNotSupportedException var2) {
- throw new InternalError();
- }
- }
-
- public final synchronized String toString() {
- int max = this.size() - 1;
- StringBuffer buf = new StringBuffer();
- Enumeration e = this.elements();
- buf.append("[");
-
- for(int i = 0; i <= max; ++i) {
- String s = e.nextElement().toString();
- buf.append(s);
- if (i < max) {
- buf.append(", ");
- }
- }
-
- buf.append("]");
- return buf.toString();
- }
- }
-