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<Match> {
14
15 private int tokenCount;
16 private Set<Mark> markSet = new TreeSet<Mark>();
17 private String label;
18
19 public static final Comparator<Match> MATCHES_COMPARATOR = new Comparator<Match>() {
20 public int compare(Match ma, Match mb) {
21 return mb.getMarkCount() - ma.getMarkCount();
22 }
23 };
24
25 public static final Comparator<Match> LINES_COMPARATOR = new Comparator<Match>() {
26 public int compare(Match ma, Match mb) {
27 return mb.getLineCount() - ma.getLineCount();
28 }
29 };
30
31 public static final Comparator<Match> LABEL_COMPARATOR = new Comparator<Match>() {
32 public int compare(Match ma, Match mb) {
33 if (ma.getLabel() == null) {
34 return 1;
35 }
36 if (mb.getLabel() == null) {
37 return -1;
38 }
39 return mb.getLabel().compareTo(ma.getLabel());
40 }
41 };
42
43 public static final Comparator<Match> LENGTH_COMPARATOR = new Comparator<Match>() {
44 public int compare(Match ma, Match mb) {
45 return mb.getLineCount() - ma.getLineCount();
46 }
47 };
48
49 public Match(int tokenCount, Mark first, Mark second) {
50 markSet.add(first);
51 markSet.add(second);
52 this.tokenCount = tokenCount;
53 }
54
55 public Match(int tokenCount, TokenEntry first, TokenEntry second) {
56 this(tokenCount, new Mark(first), new Mark(second));
57 }
58
59 public int getMarkCount() {
60 return markSet.size();
61 }
62
63 public int getLineCount() {
64 return getMark(0).getLineCount();
65 }
66
67 public int getTokenCount() {
68 return this.tokenCount;
69 }
70
71 public String getSourceCodeSlice() {
72 return this.getMark(0).getSourceCodeSlice();
73 }
74
75 public Iterator<Mark> iterator() {
76 return markSet.iterator();
77 }
78
79 public int compareTo(Match other) {
80 int diff = other.getTokenCount() - getTokenCount();
81 if (diff != 0) {
82 return diff;
83 }
84 return getFirstMark().compareTo(other.getFirstMark());
85 }
86
87 public Mark getFirstMark() {
88 return getMark(0);
89 }
90
91 public Mark getSecondMark() {
92 return getMark(1);
93 }
94
95 public String toString() {
96 return "Match: " + PMD.EOL + "tokenCount = " + tokenCount + PMD.EOL + "marks = " + markSet.size();
97 }
98
99 public Set<Mark> getMarkSet() {
100 return markSet;
101 }
102
103 public int getEndIndex() {
104 return getMark(0).getToken().getIndex() + getTokenCount() - 1;
105 }
106
107 public void setMarkSet(Set<Mark> markSet) {
108 this.markSet = markSet;
109 }
110
111 public void setLabel(String aLabel) {
112 label = aLabel;
113 }
114
115 public String getLabel() {
116 return label;
117 }
118
119 public void addTokenEntry(TokenEntry entry){
120 markSet.add(new Mark(entry));
121 }
122
123 private Mark getMark(int index) {
124 Mark result = null;
125 int i = 0;
126 for (Iterator<Mark> it = markSet.iterator(); it.hasNext() && i < index + 1; ){
127 result = it.next();
128 i++;
129 }
130 return result;
131 }
132 }