01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.cpd;
04:
05: import java.util.HashMap;
06: import java.util.Map;
07:
08: public class TokenEntry implements Comparable<TokenEntry> {
09:
10: public static final TokenEntry EOF = new TokenEntry();
11:
12: private String tokenSrcID;
13: private int beginLine;
14: private int index;
15: private int identifier;
16: private int hashCode;
17:
18: private final static Map<String, Integer> Tokens = new HashMap<String, Integer>();
19: private static int TokenCount = 0;
20:
21: private TokenEntry() {
22: this .identifier = 0;
23: this .tokenSrcID = "EOFMarker";
24: }
25:
26: public TokenEntry(String image, String tokenSrcID, int beginLine) {
27: Integer i = Tokens.get(image);
28: if (i == null) {
29: i = Tokens.size() + 1;
30: Tokens.put(image, i);
31: }
32: this .identifier = i.intValue();
33: this .tokenSrcID = tokenSrcID;
34: this .beginLine = beginLine;
35: this .index = TokenCount++;
36: }
37:
38: public static TokenEntry getEOF() {
39: TokenCount++;
40: return EOF;
41: }
42:
43: public static void clearImages() {
44: Tokens.clear();
45: TokenCount = 0;
46: }
47:
48: public String getTokenSrcID() {
49: return tokenSrcID;
50: }
51:
52: public int getBeginLine() {
53: return beginLine;
54: }
55:
56: public int getIdentifier() {
57: return this .identifier;
58: }
59:
60: public int getIndex() {
61: return this .index;
62: }
63:
64: public int hashCode() {
65: return hashCode;
66: }
67:
68: public void setHashCode(int hashCode) {
69: this .hashCode = hashCode;
70: }
71:
72: public boolean equals(Object o) {
73: if (!(o instanceof TokenEntry)) {
74: return false;
75: }
76: TokenEntry other = (TokenEntry) o;
77: return other.hashCode == hashCode;
78: }
79:
80: public int compareTo(TokenEntry other) {
81: return getIndex() - other.getIndex();
82: }
83: }
|