001: package persistence.antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.jGuru.com
005: * Software rights: http://www.antlr.org/license.html
006: *
007: */
008:
009: // class implements a String-like object whose sole purpose is to be
010: // entered into a lexer HashTable. It uses a lexer object to get
011: // information about case sensitivity.
012: public class ANTLRHashString {
013: // only one of s or buf is non-null
014: private String s;
015: private char[] buf;
016: private int len;
017: private CharScanner lexer;
018: private static final int prime = 151;
019:
020: public ANTLRHashString(char[] buf, int length, CharScanner lexer) {
021: this .lexer = lexer;
022: setBuffer(buf, length);
023: }
024:
025: // Hash strings constructed this way are unusable until setBuffer or setString are called.
026: public ANTLRHashString(CharScanner lexer) {
027: this .lexer = lexer;
028: }
029:
030: public ANTLRHashString(String s, CharScanner lexer) {
031: this .lexer = lexer;
032: setString(s);
033: }
034:
035: private final char charAt(int index) {
036: return (s != null) ? s.charAt(index) : buf[index];
037: }
038:
039: // Return true if o is an ANTLRHashString equal to this.
040: public boolean equals(Object o) {
041: if (!(o instanceof ANTLRHashString) && !(o instanceof String)) {
042: return false;
043: }
044:
045: ANTLRHashString s;
046: if (o instanceof String) {
047: s = new ANTLRHashString((String) o, lexer);
048: } else {
049: s = (ANTLRHashString) o;
050: }
051: int l = length();
052: if (s.length() != l) {
053: return false;
054: }
055: if (lexer.getCaseSensitiveLiterals()) {
056: for (int i = 0; i < l; i++) {
057: if (charAt(i) != s.charAt(i)) {
058: return false;
059: }
060: }
061: } else {
062: for (int i = 0; i < l; i++) {
063: if (lexer.toLower(charAt(i)) != lexer.toLower(s
064: .charAt(i))) {
065: return false;
066: }
067: }
068: }
069: return true;
070: }
071:
072: public int hashCode() {
073: int hashval = 0;
074: int l = length();
075:
076: if (lexer.getCaseSensitiveLiterals()) {
077: for (int i = 0; i < l; i++) {
078: hashval = hashval * prime + charAt(i);
079: }
080: } else {
081: for (int i = 0; i < l; i++) {
082: hashval = hashval * prime + lexer.toLower(charAt(i));
083: }
084: }
085: return hashval;
086: }
087:
088: private final int length() {
089: return (s != null) ? s.length() : len;
090: }
091:
092: public void setBuffer(char[] buf, int length) {
093: this .buf = buf;
094: this .len = length;
095: s = null;
096: }
097:
098: public void setString(String s) {
099: this.s = s;
100: buf = null;
101: }
102: }
|