01: package persistence.antlr.collections.impl;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.jGuru.com
05: * Software rights: http://www.antlr.org/license.html
06: *
07: */
08:
09: import java.util.Hashtable;
10: import java.util.Enumeration;
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 index;
22:
23: /**
24: * IndexedVector constructor comment.
25: */
26: public IndexedVector() {
27: elements = new Vector(10);
28: index = new Hashtable(10);
29: }
30:
31: /**
32: * IndexedVector constructor comment.
33: * @param size int
34: */
35: public IndexedVector(int size) {
36: elements = new Vector(size);
37: index = new Hashtable(size);
38: }
39:
40: public synchronized void appendElement(Object key, Object value) {
41: elements.appendElement(value);
42: index.put(key, value);
43: }
44:
45: /**
46: * Returns the element at the specified index.
47: * @param index the index of the desired element
48: * @exception ArrayIndexOutOfBoundsException If an invalid
49: * index was given.
50: */
51: public Object elementAt(int i) {
52: return elements.elementAt(i);
53: }
54:
55: public Enumeration elements() {
56: return elements.elements();
57: }
58:
59: public Object getElement(Object key) {
60: Object o = index.get(key);
61: return o;
62: }
63:
64: /** remove element referred to by key NOT value; return false if not found. */
65: public synchronized boolean removeElement(Object key) {
66: Object value = index.get(key);
67: if (value == null) {
68: return false;
69: }
70: index.remove(key);
71: elements.removeElement(value);
72: return false;
73: }
74:
75: public int size() {
76: return elements.size();
77: }
78: }
|