KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > cpd > Match


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 JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.TreeSet JavaDoc;
12
13 public class Match implements Comparable JavaDoc {
14
15     private int tokenCount;
16     private int lineCount;
17     private Set JavaDoc markSet = new TreeSet JavaDoc();
18     private TokenEntry[] marks = new TokenEntry[2];
19     private String JavaDoc code;
20     private MatchCode mc;
21     private String JavaDoc label;
22     
23     public static final Comparator JavaDoc MatchesComparator = new Comparator JavaDoc() {
24         public int compare(Object JavaDoc a, Object JavaDoc 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 JavaDoc LinesComparator = new Comparator JavaDoc() {
32         public int compare(Object JavaDoc a, Object JavaDoc 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 JavaDoc LabelComparator = new Comparator JavaDoc() {
41         public int compare(Object JavaDoc a, Object JavaDoc 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 JavaDoc LengthComparator = new Comparator JavaDoc() {
51         public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc 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 JavaDoc getSourceCodeSlice() {
115         return this.code;
116     }
117
118     public void setSourceCodeSlice(String JavaDoc code) {
119         this.code = code;
120     }
121
122     public Iterator JavaDoc iterator() {
123         return markSet.iterator();
124     }
125
126     public int compareTo(Object JavaDoc 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 JavaDoc toString() {
144         return "Match: " + PMD.EOL + "tokenCount = " + tokenCount + PMD.EOL + "marks = " + markSet.size();
145     }
146
147     public Set JavaDoc 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 JavaDoc markSet) {
163         this.markSet = markSet;
164     }
165
166     public void setLabel(String JavaDoc aLabel) {
167         label = aLabel;
168     }
169     
170     public String JavaDoc getLabel() {
171         return label;
172     }
173 }
Popular Tags