1 package persistence.antlr; 2 3 8 9 13 public class ANTLRHashString { 14 private String 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 public ANTLRHashString(CharScanner lexer) { 29 this.lexer = lexer; 30 } 31 32 public ANTLRHashString(String 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 public boolean equals(Object o) { 43 if (!(o instanceof ANTLRHashString) && !(o instanceof String )) { 44 return false; 45 } 46 47 ANTLRHashString s; 48 if (o instanceof String ) { 49 s = new ANTLRHashString((String )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 s) { 103 this.s = s; 104 buf = null; 105 } 106 } 107 | Popular Tags |