KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > collections > impl > Vector


1 package antlr.collections.impl;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/collections/impl/Vector.java#4 $
8  */

9
10 import java.util.Enumeration JavaDoc;
11 import java.util.NoSuchElementException JavaDoc;
12
13 import antlr.collections.Enumerator;
14
15 public class Vector implements Cloneable JavaDoc {
16     protected Object JavaDoc[] data;
17     protected int lastElement = -1;
18
19     public Vector() {
20         this(10);
21     }
22
23     public Vector(int size) {
24         data = new Object JavaDoc[size];
25     }
26
27     public synchronized void appendElement(Object JavaDoc o) {
28         ensureCapacity(lastElement + 2);
29         data[++lastElement] = o;
30     }
31
32     /**
33      * Returns the current capacity of the vector.
34      */

35     public int capacity() {
36         return data.length;
37     }
38
39     public Object JavaDoc clone() {
40         Vector v = null;
41         try {
42             v = (Vector)super.clone();
43         }
44         catch (CloneNotSupportedException JavaDoc e) {
45             System.err.println("cannot clone Vector.super");
46             return null;
47         }
48         v.data = new Object JavaDoc[size()];
49         System.arraycopy(data, 0, v.data, 0, size());
50         return v;
51     }
52
53     /**
54      * Returns the element at the specified index.
55      * @param index the index of the desired element
56      * @exception ArrayIndexOutOfBoundsException If an invalid
57      * index was given.
58      */

59     public synchronized Object JavaDoc elementAt(int i) {
60         if (i >= data.length) {
61             throw new ArrayIndexOutOfBoundsException JavaDoc(i + " >= " + data.length);
62         }
63         if (i < 0) {
64             throw new ArrayIndexOutOfBoundsException JavaDoc(i + " < 0 ");
65         }
66         return data[i];
67     }
68
69     public synchronized Enumeration JavaDoc elements() {
70         return new VectorEnumerator(this);
71     }
72
73     public synchronized void ensureCapacity(int minIndex) {
74         if (minIndex + 1 > data.length) {
75             Object JavaDoc oldData[] = data;
76             int n = data.length * 2;
77             if (minIndex + 1 > n) {
78                 n = minIndex + 1;
79             }
80             data = new Object JavaDoc[n];
81             System.arraycopy(oldData, 0, data, 0, oldData.length);
82         }
83     }
84
85     public synchronized boolean removeElement(Object JavaDoc o) {
86         // find element
87
int i;
88         for (i = 0; i <= lastElement && data[i] != o; i++) {
89             ;
90         }
91         if (i <= lastElement) { // if found it
92
data[i] = null; // kill ref for GC
93
int above = lastElement - i;
94             if (above > 0) {
95                 System.arraycopy(data, i + 1, data, i, above);
96             }
97             lastElement--;
98             return true;
99         }
100         else {
101             return false;
102         }
103     }
104
105     public synchronized void setElementAt(Object JavaDoc obj, int i) {
106         if (i >= data.length) {
107             throw new ArrayIndexOutOfBoundsException JavaDoc(i + " >= " + data.length);
108         }
109         data[i] = obj;
110         // track last element in the vector so we can append things
111
if (i > lastElement) {
112             lastElement = i;
113         }
114     }
115
116     // return number of slots in the vector; e.g., you can set
117
// the 30th element and size() will return 31.
118
public int size() {
119         return lastElement + 1;
120     }
121 }
122
Popular Tags