01: package org.acm.seguin.pmd.cpd;
02:
03: import java.util.Comparator;
04: import java.util.List;
05:
06: public class MarkComparator implements Comparator {
07:
08: private final int comparisonUpdateInterval;
09: private CPDListener l;
10: private long comparisons;
11: private List tokens;
12:
13: public MarkComparator(CPDListener l, List tokens) {
14: this (l, tokens, 10000);
15: }
16:
17: public MarkComparator(CPDListener l, List tokens,
18: int comparisonUpdateInterval) {
19: this .l = l;
20: this .tokens = tokens;
21: this .comparisonUpdateInterval = comparisonUpdateInterval;
22: }
23:
24: public int compare(Object o1, Object o2) {
25: comparisons++;
26: if (comparisons % comparisonUpdateInterval == 0) {
27: l.comparisonCountUpdate(comparisons);
28: }
29:
30: Mark mark1 = (Mark) o1;
31: Mark mark2 = (Mark) o2;
32: for (int i = 1; i < tokens.size(); i++) {
33: int cmp = tokenAt(i, mark1).compareTo(tokenAt(i, mark2));
34: if (cmp != 0) {
35: return cmp;
36: }
37: }
38: return 0;
39: }
40:
41: public TokenEntry tokenAt(int index, Mark mark) {
42: return (TokenEntry) tokens.get((index + mark
43: .getIndexIntoTokenArray())
44: % tokens.size());
45: }
46: }
|