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