KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ro > infoiasi > donald > compiler > lexer > Alphabet


1 package ro.infoiasi.donald.compiler.lexer;
2
3 import ro.infoiasi.donald.compiler.lexer.exceptions.*;
4 import java.util.*;
5
6 /** An ordered set of symbols (characters) */
7 public class Alphabet implements Cloneable JavaDoc {
8     /** An array with all the characters currently in the alphabet */
9     private ArrayList arr;
10     /** A map containing symbol(Char)-index(Integer) pairs used for
11     quickly obtaining the symbol index corresponding to a symbol */

12     private HashMap map;
13
14     /** Constructs an empty alphabet */
15     public Alphabet() {
16         map = new HashMap();
17         arr = new ArrayList();
18     }
19
20     /** Constructs an alphabet containing all the distinct symbols
21     in the specified string (in that particular order) */

22     public Alphabet(String JavaDoc s) {
23         this();
24         for (int i = 0; i<s.length(); i++) {
25             addSymbol(s.charAt(i));
26         }
27     }
28
29     /** Constructs an alphabet by copying an existing one */
30     public Alphabet(Alphabet alpha) {
31         arr = (ArrayList)alpha.arr.clone();
32         map = (HashMap)alpha.map.clone();
33     }
34
35     /** Creates and returns a clone of this object */
36     public Object JavaDoc clone() {
37         return new Alphabet(this);
38     }
39
40     /** Indicates whether some other object is "equal to" this one. */
41     public boolean equals(Object JavaDoc obj) {
42         Alphabet alpha = (Alphabet)obj;
43         if (size() != alpha.size()) {
44             return false;
45         }
46         for (int i = 0; i<size(); i++) {
47             if (getSymbol(i).charValue() != getSymbol(i).charValue()) {
48                 return false;
49             }
50         }
51         return true;
52     }
53
54     /** Returns the number of symbols in this alphabet */
55     public int size() {
56         return arr.size();
57     }
58
59     /** Removes all symbols from this alphabet */
60     public void clear() {
61         arr.clear();
62         map.clear();
63     }
64
65     /** Adds the specified character to this alpabet.
66     Once added symbols can only be removed with clear(). */

67     public void addSymbol(char ch) {
68         if (!containsSymbol(ch)) {
69             Character JavaDoc cObj = new Character JavaDoc(ch);
70             arr.add(cObj);
71             map.put(cObj, new Integer JavaDoc(map.size()));
72         }
73     }
74
75     /** Returns the symbol in the alphabet at the given index */
76     public Character JavaDoc getSymbol(int index) {
77         return (Character JavaDoc) arr.get(index);
78     }
79
80     /** Returns true if this alphabet contains the specified character. */
81     public boolean containsSymbol(char ch) {
82         return map.containsKey(new Character JavaDoc(ch));
83     }
84
85     /** Returns the index in the alphabet for the specified character. */
86     public int idxSymbol(char ch) {
87         Integer JavaDoc i = (Integer JavaDoc)map.get(new Character JavaDoc(ch));
88         return i.intValue();
89     }
90
91     /** Returns a string containg all the symbols of the alphabet
92     in the same order as they were added */

93     public String JavaDoc toString() {
94         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(size());
95         for (int i=0; i<size(); i++) {
96             sb.append((Character JavaDoc)arr.get(i));
97         }
98         return new String JavaDoc(sb);
99     }
100
101     /** Prints the contents of the alphabet. [Debuging purpose] */
102     public void print() {
103         System.out.println("Size: " + size());
104         System.out.print("Array: ");
105         System.out.println(arr);
106         System.out.print("Map: ");
107         System.out.println(map.entrySet());
108     }
109 }
110
Popular Tags