KickJava   Java API By Example, From Geeks To Geeks.

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


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/IndexedVector.java#4 $
8  */

9
10 import java.util.Hashtable JavaDoc;
11 import java.util.Enumeration JavaDoc;
12
13 import antlr.collections.impl.Vector;
14
15 /**
16  * A simple indexed vector: a normal vector except that you must
17  * specify a key when adding an element. This allows fast lookup
18  * and allows the order of specification to be preserved.
19  */

20 public class IndexedVector {
21     protected Vector elements;
22     protected Hashtable JavaDoc index;
23
24
25     /**
26      * IndexedVector constructor comment.
27      */

28     public IndexedVector() {
29         elements = new Vector(10);
30         index = new Hashtable JavaDoc(10);
31     }
32
33     /**
34      * IndexedVector constructor comment.
35      * @param size int
36      */

37     public IndexedVector(int size) {
38         elements = new Vector(size);
39         index = new Hashtable JavaDoc(size);
40     }
41
42     public synchronized void appendElement(Object JavaDoc key, Object JavaDoc value) {
43         elements.appendElement(value);
44         index.put(key, value);
45     }
46
47     /**
48      * Returns the element at the specified index.
49      * @param index the index of the desired element
50      * @exception ArrayIndexOutOfBoundsException If an invalid
51      * index was given.
52      */

53     public Object JavaDoc elementAt(int i) {
54         return elements.elementAt(i);
55     }
56
57     public Enumeration JavaDoc elements() {
58         return elements.elements();
59     }
60
61     public Object JavaDoc getElement(Object JavaDoc key) {
62         Object JavaDoc o = index.get(key);
63         return o;
64     }
65
66     /** remove element referred to by key NOT value; return false if not found. */
67     public synchronized boolean removeElement(Object JavaDoc key) {
68         Object JavaDoc value = index.get(key);
69         if (value == null) {
70             return false;
71         }
72         index.remove(key);
73         elements.removeElement(value);
74         return false;
75     }
76
77     public int size() {
78         return elements.size();
79     }
80 }
81
Popular Tags