1 package net.sourceforge.pmd.cpd;
2
3 public class TokenEntry implements Comparable {
4
5 public static final TokenEntry EOF = new TokenEntry();
6 private char[] chars;
7 private int hash;
8 private String image;
9 private int index;
10 private String tokenSrcID;
11 private int beginLine;
12
13 private int sortCode;
14
15 private TokenEntry() {
16 this.image = "EOF";
17 this.chars = image.toCharArray();
18 this.tokenSrcID = "EOFMarker";
19 }
20
21 public TokenEntry(String image, int index, String tokenSrcID, int beginLine) {
22 this.image = image;
23 this.index = index;
24 this.tokenSrcID = tokenSrcID;
25 this.beginLine = beginLine;
26 this.chars = image.toCharArray();
27 }
28
29 public int getIndex() {
30 return index;
31 }
32
33 public String getTokenSrcID() {
34 return tokenSrcID;
35 }
36
37 public int getBeginLine() {
38 return beginLine;
39 }
40
41 public void setSortCode(int code) {
42 this.sortCode = code;
43 }
44
45 public boolean equals(Object o) {
46 if (o instanceof TokenEntry) {
47 TokenEntry token = (TokenEntry)o;
48 if (this == EOF) {
49 return token == EOF;
50 }
51 if (token.image.length() != image.length()) {
52 return false;
53 }
54 for (int i = 0; i < image.length(); i++) {
55 if (this.chars[i] != token.chars[i]) {
56 return false;
57 }
58 }
59 return true;
60 }
61 return false;
62 }
63 // calculate a hash, as done in Effective Programming in Java.
64 public int hashCode() {
65 int h = hash;
66 if (h == 0) {
67 if ( this == EOF ) {
68 h = -1;
69 } else {
70 for (int i = 0 ; i < image.length(); i++) {
71 h = (37 * h + this.chars[i]);
72 }
73 }
74 hash = h; // single assignment = thread safe hashcode.
75 }
76 return h;
77 }
78 public int compareTo(Object o) {
79 TokenEntry token = (TokenEntry)o;
80 // try to use sort codes if available.
81 if (this == EOF) {
82 if (token == EOF) {
83 return 0;
84 }
85 return -1;
86 }
87 if (this.sortCode > 0 && token.sortCode > 0) {
88 return this.sortCode - token.sortCode;
89 }
90 // otherwise sort lexicographically
91 if (image.length() == token.image.length()) {
92 for (int i = 0; i < image.length() && i < token.image.length(); i++) {
93 char c1 = this.chars[i];
94 char c2 = token.chars[i];
95 if (c1 != c2) {
96 return c1 - c2;
97 }
98 }
99 return 0;
100 }
101 for (int i = 0; i < image.length() && i < token.image.length(); i++) {
102 char c1 = this.chars[i];
103 char c2 = token.chars[i];
104 if (c1 != c2) {
105 return c1 - c2;
106 }
107 }
108 return image.length() - token.image.length();
109 }
110 }
This page was automatically generated by Maven