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

This page was automatically generated by Maven