KickJava   Java API By Example, From Geeks To Geeks.

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


1 package persistence.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/license.html
6  *
7  */

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

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

58     public synchronized Object JavaDoc elementAt(int i) {
59         if (i >= data.length) {
60             throw new ArrayIndexOutOfBoundsException JavaDoc(i + " >= " + data.length);
61         }
62         if (i < 0) {
63             throw new ArrayIndexOutOfBoundsException JavaDoc(i + " < 0 ");
64         }
65         return data[i];
66     }
67
68     public synchronized Enumeration JavaDoc elements() {
69         return new VectorEnumerator(this);
70     }
71
72     public synchronized void ensureCapacity(int minIndex) {
73         if (minIndex + 1 > data.length) {
74             Object JavaDoc oldData[] = data;
75             int n = data.length * 2;
76             if (minIndex + 1 > n) {
77                 n = minIndex + 1;
78             }
79             data = new Object JavaDoc[n];
80             System.arraycopy(oldData, 0, data, 0, oldData.length);
81         }
82     }
83
84     public synchronized boolean removeElement(Object JavaDoc o) {
85         // find element
86
int i;
87         for (i = 0; i <= lastElement && data[i] != o; i++) {
88             ;
89         }
90         if (i <= lastElement) { // if found it
91
data[i] = null; // kill ref for GC
92
int above = lastElement - i;
93             if (above > 0) {
94                 System.arraycopy(data, i + 1, data, i, above);
95             }
96             lastElement--;
97             return true;
98         }
99         else {
100             return false;
101         }
102     }
103
104     public synchronized void setElementAt(Object JavaDoc obj, int i) {
105         if (i >= data.length) {
106             throw new ArrayIndexOutOfBoundsException JavaDoc(i + " >= " + 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 }
121
Popular Tags