1 package antlr.collections.impl;
2
3 import java.util.Enumeration;
4 import java.util.NoSuchElementException;
5 import antlr.collections.Enumerator;
6
7 public class Vector {
8 protected Object[] data;
9 protected int lastElement = -1;
10
11
12 public Vector() {
13 this(10);
14 }
15 public Vector(int size) {
16 data = new Object[size];
17 }
18 public synchronized void appendElement(Object o) {
19 ensureCapacity(lastElement+2);
20 data[++lastElement] = o;
21 }
22 * Returns the current capacity of the vector.
24 */
25 public int capacity() {
26 return data.length;
27 }
28 * Returns the element at the specified index.
30 * @param index the index of the desired element
31 * @exception ArrayIndexOutOfBoundsException If an invalid
32 * index was given.
33 */
34 public synchronized Object elementAt(int i) {
35 if (i >= data.length) {
36 throw new ArrayIndexOutOfBoundsException(i + " >= " + data.length);
37 }
38 if ( i<0 ) {
39 throw new ArrayIndexOutOfBoundsException(i + " < 0 ");
40 }
41 return data[i];
42 }
43 public synchronized Enumeration elements() {
44 return new VectorEnumerator(this);
45 }
46 public synchronized void ensureCapacity(int minIndex) {
47 if ( minIndex+1 > data.length ) {
48 Object oldData[] = data;
49 int n = data.length * 2;
50 if ( minIndex+1 > n ) {
51 n = minIndex+1;
52 }
53 data = new Object[n];
54 System.arraycopy(oldData, 0, data, 0, oldData.length);
55 }
56 }
57 public synchronized boolean removeElement(Object o) {
58 int i;
60 for (i=0; i<=lastElement && data[i]!=o; i++) {
61 ;
62 }
63 if ( i<=lastElement ) { data[i] = null; int above = lastElement - i;
66 if (above > 0) {
67 System.arraycopy(data, i + 1, data, i, above);
68 }
69 lastElement--;
70 return true;
71 }
72 else {
73 return false;
74 }
75 }
76 public synchronized void setElementAt(Object obj, int i) {
77 if (i >= data.length) {
78 throw new ArrayIndexOutOfBoundsException(i + " >= " + data.length);
79 }
80 data[i] = obj;
81 if ( i>lastElement ) {
83 lastElement = i;
84 }
85 }
86 public int size() {
89 return lastElement+1;
90 }
91 }
92