KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > ANTLRHashString


1 package antlr;
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/ANTLRHashString.java#4 $
8  */

9
10 // class implements a String-like object whose sole purpose is to be
11
// entered into a lexer HashTable. It uses a lexer object to get
12
// information about case sensitivity.
13

14 public class ANTLRHashString {
15     // only one of s or buf is non-null
16
private String JavaDoc s;
17     private char[] buf;
18     private int len;
19     private CharScanner lexer;
20     private static final int prime = 151;
21
22
23     public ANTLRHashString(char[] buf, int length, CharScanner lexer) {
24         this.lexer = lexer;
25         setBuffer(buf, length);
26     }
27
28     // Hash strings constructed this way are unusable until setBuffer or setString are called.
29
public ANTLRHashString(CharScanner lexer) {
30         this.lexer = lexer;
31     }
32
33     public ANTLRHashString(String JavaDoc s, CharScanner lexer) {
34         this.lexer = lexer;
35         setString(s);
36     }
37
38     private final char charAt(int index) {
39         return (s != null) ? s.charAt(index) : buf[index];
40     }
41
42     // Return true if o is an ANTLRHashString equal to this.
43
public boolean equals(Object JavaDoc o) {
44         if (!(o instanceof ANTLRHashString) && !(o instanceof String JavaDoc)) {
45             return false;
46         }
47
48         ANTLRHashString s;
49         if (o instanceof String JavaDoc) {
50             s = new ANTLRHashString((String JavaDoc)o, lexer);
51         }
52         else {
53             s = (ANTLRHashString)o;
54         }
55         int l = length();
56         if (s.length() != l) {
57             return false;
58         }
59         if (lexer.getCaseSensitiveLiterals()) {
60             for (int i = 0; i < l; i++) {
61                 if (charAt(i) != s.charAt(i)) {
62                     return false;
63                 }
64             }
65         }
66         else {
67             for (int i = 0; i < l; i++) {
68                 if (lexer.toLower(charAt(i)) != lexer.toLower(s.charAt(i))) {
69                     return false;
70                 }
71             }
72         }
73         return true;
74     }
75
76     public int hashCode() {
77         int hashval = 0;
78         int l = length();
79
80         if (lexer.getCaseSensitiveLiterals()) {
81             for (int i = 0; i < l; i++) {
82                 hashval = hashval * prime + charAt(i);
83             }
84         }
85         else {
86             for (int i = 0; i < l; i++) {
87                 hashval = hashval * prime + lexer.toLower(charAt(i));
88             }
89         }
90         return hashval;
91     }
92
93     private final int length() {
94         return (s != null) ? s.length() : len;
95     }
96
97     public void setBuffer(char[] buf, int length) {
98         this.buf = buf;
99         this.len = length;
100         s = null;
101     }
102
103     public void setString(String JavaDoc s) {
104         this.s = s;
105         buf = null;
106     }
107 }
108
Popular Tags