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