001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 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 com.google.common.annotations.Beta; 027 028 import java.util.HashMap; 029 import java.util.Map; 030 031 /** 032 * @since 2.2 033 */ 034 public class TokenEntry implements Comparable<TokenEntry> { 035 036 private static final Map<String, Integer> TOKENS = new HashMap<String, Integer>(); 037 private static int tokenCount = 0; 038 039 /** 040 * Shared instance of end-of-file token. 041 * 042 * <p>Not intended to be used by clients - {@link #getEOF()} should be used instead.</p> 043 */ 044 public static final TokenEntry EOF = new TokenEntry(); 045 046 private String tokenSrcID; 047 private int beginLine; 048 private int index; 049 private int identifier; 050 private int hashCode; 051 052 private final String value; 053 054 private TokenEntry() { 055 this.identifier = 0; 056 this.tokenSrcID = "EOFMarker"; 057 this.value = ""; 058 } 059 060 /** 061 * @param image string representation of token 062 * @param tokenSrcID within Sonar Ecosystem - absolute path to file, otherwise current implementation of sonar-cpd-plugin will not work 063 * @param beginLine number of line 064 */ 065 public TokenEntry(String image, String tokenSrcID, int beginLine) { 066 Integer i = TOKENS.get(image); 067 if (i == null) { 068 i = TOKENS.size() + 1; 069 TOKENS.put(image, i); 070 } 071 this.identifier = i.intValue(); 072 this.tokenSrcID = tokenSrcID; 073 this.beginLine = beginLine; 074 this.index = tokenCount++; 075 this.value = image; 076 } 077 078 /** 079 * For internal use only. 080 * 081 * @since 2.14 082 */ 083 @Beta 084 public String getValue() { 085 return value; 086 } 087 088 /** 089 * End-of-file token. 090 */ 091 public static TokenEntry getEOF() { 092 tokenCount++; 093 return EOF; 094 } 095 096 public static void clearImages() { 097 TOKENS.clear(); 098 tokenCount = 0; 099 } 100 101 public String getTokenSrcID() { 102 return tokenSrcID; 103 } 104 105 public int getBeginLine() { 106 return beginLine; 107 } 108 109 public int getIdentifier() { 110 return this.identifier; 111 } 112 113 public int getIndex() { 114 return this.index; 115 } 116 117 public int hashCode() { 118 return hashCode; 119 } 120 121 public void setHashCode(int hashCode) { 122 this.hashCode = hashCode; 123 } 124 125 public boolean equals(Object o) { 126 if (!(o instanceof TokenEntry)) { 127 return false; 128 } 129 TokenEntry other = (TokenEntry) o; 130 return other.hashCode == hashCode; 131 } 132 133 public int compareTo(TokenEntry other) { 134 return getIndex() - other.getIndex(); 135 } 136 }