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