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   import java.util.HashMap;
    7   import java.util.Map;
    8
    9   public class TokenEntry implements Comparable {
    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 Tokens = new HashMap();
    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 = (Integer) Tokens.get(image);
    29          if (i == null) {
    30              i = new Integer(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          TokenEntry other = (TokenEntry) o;
    75          return other.hashCode == hashCode;
    76      }
    77
    78      public int compareTo(Object o) {
    79          TokenEntry other = (TokenEntry) o;
    80          return getIndex() - other.getIndex();
    81      }
    82  }