001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020
021 /**
022 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
023 */
024 package net.sourceforge.pmd.cpd;
025
026 import java.util.HashMap;
027 import java.util.Map;
028
029 public class TokenEntry implements Comparable<TokenEntry> {
030
031 public static final TokenEntry EOF = new TokenEntry();
032
033 private String tokenSrcID;
034 private int beginLine;
035 private int index;
036 private int identifier;
037 private int hashCode;
038
039 private final static Map<String, Integer> Tokens = new HashMap<String, Integer>();
040 private static int TokenCount = 0;
041
042 private TokenEntry() {
043 this.identifier = 0;
044 this.tokenSrcID = "EOFMarker";
045 }
046
047 public TokenEntry(String image, String tokenSrcID, int beginLine) {
048 Integer i = Tokens.get(image);
049 if (i == null) {
050 i = Tokens.size() + 1;
051 Tokens.put(image, i);
052 }
053 this.identifier = i.intValue();
054 this.tokenSrcID = tokenSrcID;
055 this.beginLine = beginLine;
056 this.index = TokenCount++;
057 }
058
059 public static TokenEntry getEOF() {
060 TokenCount++;
061 return EOF;
062 }
063
064 public static void clearImages() {
065 Tokens.clear();
066 TokenCount = 0;
067 }
068
069 public String getTokenSrcID() {
070 return tokenSrcID;
071 }
072
073 public int getBeginLine() {
074 return beginLine;
075 }
076
077 public int getIdentifier() {
078 return this.identifier;
079 }
080
081 public int getIndex() {
082 return this.index;
083 }
084
085 public int hashCode() {
086 return hashCode;
087 }
088
089 public void setHashCode(int hashCode) {
090 this.hashCode = hashCode;
091 }
092
093 public boolean equals(Object o) {
094 if (!(o instanceof TokenEntry)) {
095 return false;
096 }
097 TokenEntry other = (TokenEntry) o;
098 return other.hashCode == hashCode;
099 }
100
101 public int compareTo(TokenEntry other) {
102 return getIndex() - other.getIndex();
103 }
104 }