diff --git a/pmd/regress/test/net/sourceforge/pmd/cpd/MarkComparatorTest.java b/pmd/regress/test/net/sourceforge/pmd/cpd/MarkComparatorTest.java index 540e34155e..7548d6aaae 100644 --- a/pmd/regress/test/net/sourceforge/pmd/cpd/MarkComparatorTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/cpd/MarkComparatorTest.java @@ -25,32 +25,32 @@ public class MarkComparatorTest extends TestCase { public void testEqualMarksAreEqual() { List code = getCode(); - MarkComparator comp = new MarkComparator(new CPDNullListener(), code.size()); - Mark mark1 = new Mark(code, code.size(), "/var/Foo.java", 0); - Mark mark6 = new Mark(code, code.size(), "/var/Foo.java", 5); + MarkComparator comp = new MarkComparator(new CPDNullListener(), code); + Mark mark1 = new Mark(code.size(), "/var/Foo.java", 0); + Mark mark6 = new Mark(code.size(), "/var/Foo.java", 5); assertEquals(0, comp.compare(mark1, mark6)); } public void testSameMarkIsEqual() { List code = getCode(); - MarkComparator comp = new MarkComparator(new CPDNullListener(), code.size()); - Mark mark1 = new Mark(code, code.size(), "/var/Foo.java", 0); + MarkComparator comp = new MarkComparator(new CPDNullListener(), code); + Mark mark1 = new Mark(code.size(), "/var/Foo.java", 0); assertEquals(0, comp.compare(mark1, mark1)); } public void testUnuequalMarksAreUnequal() { List code = getCode(); - MarkComparator comp = new MarkComparator(new CPDNullListener(), code.size()); - Mark mark1 = new Mark(code, 0, "/var/Foo.java", 0); - Mark mark5 = new Mark(code, 4, "/var/Foo.java", 4); + MarkComparator comp = new MarkComparator(new CPDNullListener(), code); + Mark mark1 = new Mark(0, "/var/Foo.java", 0); + Mark mark5 = new Mark(4, "/var/Foo.java", 4); assertFalse(0 == comp.compare(mark1, mark5)); } public void testcomparisonCountCallback() { gotCallback = false; List code = getCode(); - MarkComparator comp = new MarkComparator(new MyListener(), code.size(), 3); - Mark mark1 = new Mark(code, code.size(), "/var/Foo.java", 0); - Mark mark2 = new Mark(code, code.size(), "/var/Foo.java", 1); + MarkComparator comp = new MarkComparator(new MyListener(), code, 3); + Mark mark1 = new Mark(code.size(), "/var/Foo.java", 0); + Mark mark2 = new Mark(code.size(), "/var/Foo.java", 1); comp.compare(mark1, mark2); comp.compare(mark1, mark2); comp.compare(mark1, mark2); diff --git a/pmd/regress/test/net/sourceforge/pmd/cpd/MarkTest.java b/pmd/regress/test/net/sourceforge/pmd/cpd/MarkTest.java index 2f94959566..ac81db5f08 100644 --- a/pmd/regress/test/net/sourceforge/pmd/cpd/MarkTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/cpd/MarkTest.java @@ -8,7 +8,7 @@ import java.util.ArrayList; public class MarkTest extends TestCase { public void testSimple() { - Mark mark = new Mark(new ArrayList(), 0, "/var/Foo.java", 10); + Mark mark = new Mark(0, "/var/Foo.java", 10); assertEquals(mark.getIndexIntoFile(), 10); } } diff --git a/pmd/src/net/sourceforge/pmd/cpd/Mark.java b/pmd/src/net/sourceforge/pmd/cpd/Mark.java index e41f317eaf..79e19c4cfb 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/Mark.java +++ b/pmd/src/net/sourceforge/pmd/cpd/Mark.java @@ -6,11 +6,9 @@ public class Mark { private int indexIntoFile; private int indexIntoTokenArray; - private List code; private String file; - public Mark(List code, int offset, String file, int index) { - this.code = code; + public Mark(int offset, String file, int index) { this.indexIntoTokenArray = offset; this.indexIntoFile = index; this.file = file; @@ -27,9 +25,4 @@ public class Mark { public int getIndexIntoTokenArray() { return indexIntoTokenArray; } - - public TokenEntry tokenAt(int index) { - return (TokenEntry)code.get((index + indexIntoTokenArray) % code.size()); - } - } diff --git a/pmd/src/net/sourceforge/pmd/cpd/MarkComparator.java b/pmd/src/net/sourceforge/pmd/cpd/MarkComparator.java index 37d2f35e0c..c276186fc6 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/MarkComparator.java +++ b/pmd/src/net/sourceforge/pmd/cpd/MarkComparator.java @@ -1,19 +1,20 @@ package net.sourceforge.pmd.cpd; import java.util.Comparator; +import java.util.List; public class MarkComparator implements Comparator { private final int comparisonUpdateInterval; private CPDListener l; private long comparisons; - private int code; + private List code; - public MarkComparator(CPDListener l, int code) { + public MarkComparator(CPDListener l, List code) { this(l, code, 10000); } - public MarkComparator(CPDListener l, int code, int comparisonUpdateInterval) { + public MarkComparator(CPDListener l, List code, int comparisonUpdateInterval) { this.l = l; this.code = code; this.comparisonUpdateInterval = comparisonUpdateInterval; @@ -27,8 +28,8 @@ public class MarkComparator implements Comparator { Mark mark1 = (Mark)o1; Mark mark2 = (Mark)o2; - for (int i = 1; i < code; i++) { - int cmp = mark2.tokenAt(i).compareTo(mark1.tokenAt(i)); + for (int i = 1; i < code.size(); i++) { + int cmp = tokenAt(i, mark1).compareTo(tokenAt(i, mark2)); if (cmp != 0) { return cmp; } @@ -36,4 +37,7 @@ public class MarkComparator implements Comparator { return 0; } + public TokenEntry tokenAt(int index, Mark mark) { + return (TokenEntry)code.get((index + mark.getIndexIntoTokenArray()) % code.size()); + } } diff --git a/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java b/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java index 0355f5e3f4..72799441dc 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java +++ b/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java @@ -22,7 +22,7 @@ public class MatchAlgorithm { pool.put(token, token); } code.add(pool.get(token)); - marks.add(new Mark(code, code.size(), token.getTokenSrcID(), token.getIndex())); + marks.add(new Mark(code.size(), token.getTokenSrcID(), token.getIndex())); this.cpdListener = cpdListener; } @@ -37,7 +37,8 @@ public class MatchAlgorithm { token.setSortCode(count++); } - Collections.sort(marks, new MarkComparator(cpdListener, code.size())); + MarkComparator mc = new MarkComparator(cpdListener, code); + Collections.sort(marks, mc); Set soFar = new HashSet(); for (int i = 1; i < marks.size(); i++) { @@ -45,8 +46,8 @@ public class MatchAlgorithm { Mark mark1 = (Mark)marks.get(i); Mark mark2 = (Mark)marks.get(i - 1); for (int j = 0; j < code.size(); j++) { - TokenEntry token1 = mark1.tokenAt(j); - TokenEntry token2 = mark2.tokenAt(j); + TokenEntry token1 = mc.tokenAt(j, mark1); + TokenEntry token2 = mc.tokenAt(j, mark2); if (!token1.equals(token2) || token1 == TokenEntry.EOF || token2 == TokenEntry.EOF) { break; }