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