001: package antlr.collections.impl;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.cs.usfca.edu
005: * Software rights: http://www.antlr.org/license.html
006: */
007:
008: import java.util.Enumeration;
009: import java.util.NoSuchElementException;
010:
011: import antlr.collections.Enumerator;
012:
013: public class Vector implements Cloneable {
014: protected Object[] data;
015: protected int lastElement = -1;
016:
017: public Vector() {
018: this (10);
019: }
020:
021: public Vector(int size) {
022: data = new Object[size];
023: }
024:
025: public synchronized void appendElement(Object o) {
026: ensureCapacity(lastElement + 2);
027: data[++lastElement] = o;
028: }
029:
030: /**
031: * Returns the current capacity of the vector.
032: */
033: public int capacity() {
034: return data.length;
035: }
036:
037: public Object clone() {
038: Vector v = null;
039: try {
040: v = (Vector) super .clone();
041: } catch (CloneNotSupportedException e) {
042: System.err.println("cannot clone Vector.super");
043: return null;
044: }
045: v.data = new Object[size()];
046: System.arraycopy(data, 0, v.data, 0, size());
047: return v;
048: }
049:
050: /**
051: * Returns the element at the specified index.
052: * @param index the index of the desired element
053: * @exception ArrayIndexOutOfBoundsException If an invalid
054: * index was given.
055: */
056: public synchronized Object elementAt(int i) {
057: if (i >= data.length) {
058: throw new ArrayIndexOutOfBoundsException(i + " >= "
059: + data.length);
060: }
061: if (i < 0) {
062: throw new ArrayIndexOutOfBoundsException(i + " < 0 ");
063: }
064: return data[i];
065: }
066:
067: public synchronized Enumeration elements() {
068: return new VectorEnumerator(this );
069: }
070:
071: public synchronized void ensureCapacity(int minIndex) {
072: if (minIndex + 1 > data.length) {
073: Object oldData[] = data;
074: int n = data.length * 2;
075: if (minIndex + 1 > n) {
076: n = minIndex + 1;
077: }
078: data = new Object[n];
079: System.arraycopy(oldData, 0, data, 0, oldData.length);
080: }
081: }
082:
083: public synchronized boolean removeElement(Object o) {
084: // find element
085: int i;
086: for (i = 0; i <= lastElement && data[i] != o; i++) {
087: ;
088: }
089: if (i <= lastElement) { // if found it
090: data[i] = null; // kill ref for GC
091: int above = lastElement - i;
092: if (above > 0) {
093: System.arraycopy(data, i + 1, data, i, above);
094: }
095: lastElement--;
096: return true;
097: } else {
098: return false;
099: }
100: }
101:
102: public synchronized void setElementAt(Object obj, int i) {
103: if (i >= data.length) {
104: throw new ArrayIndexOutOfBoundsException(i + " >= "
105: + data.length);
106: }
107: data[i] = obj;
108: // track last element in the vector so we can append things
109: if (i > lastElement) {
110: lastElement = i;
111: }
112: }
113:
114: // return number of slots in the vector; e.g., you can set
115: // the 30th element and size() will return 31.
116: public int size() {
117: return lastElement + 1;
118: }
119: }
|