1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.cpd; |
5 |
| |
6 |
| import net.sourceforge.pmd.PMD; |
7 |
| |
8 |
| import java.util.Comparator; |
9 |
| import java.util.Iterator; |
10 |
| import java.util.Set; |
11 |
| import java.util.TreeSet; |
12 |
| |
13 |
| public class Match implements Comparable { |
14 |
| |
15 |
| private int tokenCount; |
16 |
| private int lineCount; |
17 |
| private Set markSet = new TreeSet(); |
18 |
| private TokenEntry[] marks = new TokenEntry[2]; |
19 |
| private String code; |
20 |
| private MatchCode mc; |
21 |
| private String label; |
22 |
| |
23 |
| public static final Comparator MatchesComparator = new Comparator() { |
24 |
0
| public int compare(Object a, Object b) {
|
25 |
0
| Match ma = (Match)a;
|
26 |
0
| Match mb = (Match)b;
|
27 |
0
| return mb.getMarkCount() - ma.getMarkCount();
|
28 |
| } |
29 |
| }; |
30 |
| |
31 |
| public static final Comparator LinesComparator = new Comparator() { |
32 |
0
| public int compare(Object a, Object b) {
|
33 |
0
| Match ma = (Match)a;
|
34 |
0
| Match mb = (Match)b;
|
35 |
| |
36 |
0
| return mb.getLineCount() - ma.getLineCount();
|
37 |
| } |
38 |
| }; |
39 |
| |
40 |
| public static final Comparator LabelComparator = new Comparator() { |
41 |
0
| public int compare(Object a, Object b) {
|
42 |
0
| Match ma = (Match)a;
|
43 |
0
| Match mb = (Match)b;
|
44 |
0
| if (ma.getLabel() == null) return 1;
|
45 |
0
| if (mb.getLabel() == null) return -1;
|
46 |
0
| return mb.getLabel().compareTo(ma.getLabel());
|
47 |
| } |
48 |
| }; |
49 |
| |
50 |
| public static final Comparator LengthComparator = new Comparator() { |
51 |
0
| public int compare(Object o1, Object o2) {
|
52 |
0
| Match m1 = (Match) o1;
|
53 |
0
| Match m2 = (Match) o2;
|
54 |
0
| return m2.getLineCount() - m1.getLineCount();
|
55 |
| } |
56 |
| }; |
57 |
| |
58 |
| public static class MatchCode { |
59 |
| |
60 |
| private int first; |
61 |
| private int second; |
62 |
| |
63 |
2
| public MatchCode() {
|
64 |
| } |
65 |
| |
66 |
4
| public MatchCode(TokenEntry m1, TokenEntry m2) {
|
67 |
4
| first = m1.getIndex();
|
68 |
4
| second = m2.getIndex();
|
69 |
| } |
70 |
| |
71 |
10
| public int hashCode() {
|
72 |
10
| return first + 37 * second;
|
73 |
| } |
74 |
| |
75 |
3
| public boolean equals(Object other) {
|
76 |
3
| MatchCode mc = (MatchCode) other;
|
77 |
3
| return mc.first == first && mc.second == second;
|
78 |
| } |
79 |
| |
80 |
3
| public void setFirst(int first) {
|
81 |
3
| this.first = first;
|
82 |
| } |
83 |
| |
84 |
3
| public void setSecond(int second) {
|
85 |
3
| this.second = second;
|
86 |
| } |
87 |
| |
88 |
| } |
89 |
| |
90 |
10
| public Match(int tokenCount, TokenEntry first, TokenEntry second) {
|
91 |
10
| markSet.add(first);
|
92 |
10
| markSet.add(second);
|
93 |
10
| marks[0] = first;
|
94 |
10
| marks[1] = second;
|
95 |
10
| this.tokenCount = tokenCount;
|
96 |
| } |
97 |
| |
98 |
0
| public int getMarkCount() {
|
99 |
0
| return markSet.size();
|
100 |
| } |
101 |
| |
102 |
9
| public void setLineCount(int lineCount) {
|
103 |
9
| this.lineCount = lineCount;
|
104 |
| } |
105 |
| |
106 |
6
| public int getLineCount() {
|
107 |
6
| return this.lineCount;
|
108 |
| } |
109 |
| |
110 |
27
| public int getTokenCount() {
|
111 |
27
| return this.tokenCount;
|
112 |
| } |
113 |
| |
114 |
5
| public String getSourceCodeSlice() {
|
115 |
5
| return this.code;
|
116 |
| } |
117 |
| |
118 |
6
| public void setSourceCodeSlice(String code) {
|
119 |
6
| this.code = code;
|
120 |
| } |
121 |
| |
122 |
8
| public Iterator iterator() {
|
123 |
8
| return markSet.iterator();
|
124 |
| } |
125 |
| |
126 |
4
| public int compareTo(Object o) {
|
127 |
4
| Match other = (Match) o;
|
128 |
4
| int diff = other.getTokenCount() - getTokenCount();
|
129 |
4
| if (diff != 0) {
|
130 |
1
| return diff;
|
131 |
| } |
132 |
3
| return other.getFirstMark().getIndex() - getFirstMark().getIndex();
|
133 |
| } |
134 |
| |
135 |
9
| public TokenEntry getFirstMark() {
|
136 |
9
| return marks[0];
|
137 |
| } |
138 |
| |
139 |
1
| public TokenEntry getSecondMark() {
|
140 |
1
| return marks[1];
|
141 |
| } |
142 |
| |
143 |
0
| public String toString() {
|
144 |
0
| return "Match: " + PMD.EOL + "tokenCount = " + tokenCount + PMD.EOL + "marks = " + markSet.size();
|
145 |
| } |
146 |
| |
147 |
9
| public Set getMarkSet() {
|
148 |
9
| return markSet;
|
149 |
| } |
150 |
| |
151 |
7
| public MatchCode getMatchCode() {
|
152 |
7
| if (mc == null) {
|
153 |
4
| mc = new MatchCode(marks[0], marks[1]);
|
154 |
| } |
155 |
7
| return mc;
|
156 |
| } |
157 |
| |
158 |
1
| public int getEndIndex() {
|
159 |
1
| return marks[1].getIndex() + getTokenCount() - 1;
|
160 |
| } |
161 |
| |
162 |
0
| public void setMarkSet(Set markSet) {
|
163 |
0
| this.markSet = markSet;
|
164 |
| } |
165 |
| |
166 |
0
| public void setLabel(String aLabel) {
|
167 |
0
| label = aLabel;
|
168 |
| } |
169 |
| |
170 |
0
| public String getLabel() {
|
171 |
0
| return label;
|
172 |
| } |
173 |
| } |