01: package org.acm.seguin.pmd.cpd;
02:
03: import java.util.ArrayList;
04: import java.util.HashSet;
05: import java.util.List;
06: import java.util.Set;
07:
08: public class MatchCollector {
09:
10: private List marks;
11: private MarkComparator markComparator;
12:
13: public MatchCollector(List marks, MarkComparator mc) {
14: this .marks = marks;
15: this .markComparator = mc;
16: }
17:
18: public List collect(int minimumLength) {
19: List matches = new ArrayList();
20: Set filesUsedSoFar = new HashSet();
21: for (int i = 1; i < marks.size(); i++) {
22: Mark mark1 = (Mark) marks.get(i);
23: Mark mark2 = (Mark) marks.get(i - 1);
24: if (!filesUsedSoFar.contains(mark1.getTokenSrcID())
25: && !filesUsedSoFar.contains(mark2.getTokenSrcID())) {
26: int dupes = countDuplicateTokens(mark1, mark2);
27: if (dupes >= minimumLength) {
28: filesUsedSoFar.add(mark1.getTokenSrcID());
29: filesUsedSoFar.add(mark2.getTokenSrcID());
30: Match match = new Match(dupes, mark1, mark2);
31: matches.add(match);
32: }
33: }
34: }
35: return matches;
36: }
37:
38: private int countDuplicateTokens(Mark mark1, Mark mark2) {
39: int index = 0;
40: while (!matchEnded(markComparator.tokenAt(index, mark1),
41: markComparator.tokenAt(index, mark2))) {
42: index++;
43: }
44: return index;
45: }
46:
47: private boolean matchEnded(TokenEntry token1, TokenEntry token2) {
48: return !token1.equals(token2) || token1 == TokenEntry.EOF
49: || token2 == TokenEntry.EOF;
50: }
51: }
|