KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > ANTLRHashString


1 package persistence.antlr;
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 // class implements a String-like object whose sole purpose is to be
10
// entered into a lexer HashTable. It uses a lexer object to get
11
// information about case sensitivity.
12

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