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