KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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