001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd.cpd;
004:
005: import net.sourceforge.pmd.PMD;
006:
007: import java.util.Comparator;
008: import java.util.Iterator;
009: import java.util.Set;
010: import java.util.TreeSet;
011:
012: public class Match implements Comparable<Match> {
013:
014: private int tokenCount;
015: private int lineCount;
016: private Set<TokenEntry> markSet = new TreeSet<TokenEntry>();
017: private TokenEntry[] marks = new TokenEntry[2];
018: private String code;
019: private MatchCode mc;
020: private String label;
021:
022: public static final Comparator<Match> MatchesComparator = new Comparator<Match>() {
023: public int compare(Match ma, Match mb) {
024: return mb.getMarkCount() - ma.getMarkCount();
025: }
026: };
027:
028: public static final Comparator<Match> LinesComparator = new Comparator<Match>() {
029: public int compare(Match ma, Match mb) {
030: return mb.getLineCount() - ma.getLineCount();
031: }
032: };
033:
034: public static final Comparator<Match> LabelComparator = new Comparator<Match>() {
035: public int compare(Match ma, Match mb) {
036: if (ma.getLabel() == null)
037: return 1;
038: if (mb.getLabel() == null)
039: return -1;
040: return mb.getLabel().compareTo(ma.getLabel());
041: }
042: };
043:
044: public static final Comparator<Match> LengthComparator = new Comparator<Match>() {
045: public int compare(Match ma, Match mb) {
046: return mb.getLineCount() - ma.getLineCount();
047: }
048: };
049:
050: public static class MatchCode {
051:
052: private int first;
053: private int second;
054:
055: public MatchCode() {
056: }
057:
058: public MatchCode(TokenEntry m1, TokenEntry m2) {
059: first = m1.getIndex();
060: second = m2.getIndex();
061: }
062:
063: public int hashCode() {
064: return first + 37 * second;
065: }
066:
067: public boolean equals(Object other) {
068: MatchCode mc = (MatchCode) other;
069: return mc.first == first && mc.second == second;
070: }
071:
072: public void setFirst(int first) {
073: this .first = first;
074: }
075:
076: public void setSecond(int second) {
077: this .second = second;
078: }
079:
080: }
081:
082: public Match(int tokenCount, TokenEntry first, TokenEntry second) {
083: markSet.add(first);
084: markSet.add(second);
085: marks[0] = first;
086: marks[1] = second;
087: this .tokenCount = tokenCount;
088: }
089:
090: public int getMarkCount() {
091: return markSet.size();
092: }
093:
094: public void setLineCount(int lineCount) {
095: this .lineCount = lineCount;
096: }
097:
098: public int getLineCount() {
099: return this .lineCount;
100: }
101:
102: public int getTokenCount() {
103: return this .tokenCount;
104: }
105:
106: public String getSourceCodeSlice() {
107: return this .code;
108: }
109:
110: public void setSourceCodeSlice(String code) {
111: this .code = code;
112: }
113:
114: public Iterator<TokenEntry> iterator() {
115: return markSet.iterator();
116: }
117:
118: public int compareTo(Match other) {
119: int diff = other.getTokenCount() - getTokenCount();
120: if (diff != 0) {
121: return diff;
122: }
123: return other.getFirstMark().getIndex()
124: - getFirstMark().getIndex();
125: }
126:
127: public TokenEntry getFirstMark() {
128: return marks[0];
129: }
130:
131: public TokenEntry getSecondMark() {
132: return marks[1];
133: }
134:
135: public String toString() {
136: return "Match: " + PMD.EOL + "tokenCount = " + tokenCount
137: + PMD.EOL + "marks = " + markSet.size();
138: }
139:
140: public Set<TokenEntry> getMarkSet() {
141: return markSet;
142: }
143:
144: public MatchCode getMatchCode() {
145: if (mc == null) {
146: mc = new MatchCode(marks[0], marks[1]);
147: }
148: return mc;
149: }
150:
151: public int getEndIndex() {
152: return marks[1].getIndex() + getTokenCount() - 1;
153: }
154:
155: public void setMarkSet(Set<TokenEntry> markSet) {
156: this .markSet = markSet;
157: }
158:
159: public void setLabel(String aLabel) {
160: label = aLabel;
161: }
162:
163: public String getLabel() {
164: return label;
165: }
166: }
|