diff --git a/core/src/main/java/de/jplag/merging/MatchMerging.java b/core/src/main/java/de/jplag/merging/MatchMerging.java index c8756e9ea..5d2e352e2 100644 --- a/core/src/main/java/de/jplag/merging/MatchMerging.java +++ b/core/src/main/java/de/jplag/merging/MatchMerging.java @@ -23,8 +23,6 @@ public class MatchMerging { private Submission firstSubmission; private Submission secondSubmission; - private List globalMatches; - private List> neighbors; private JPlagResult result; private List comparisons; private JPlagOptions options; @@ -50,11 +48,9 @@ public JPlagResult run() { for (int i = 0; i < comparisons.size(); i++) { firstSubmission = comparisons.get(i).firstSubmission().copy(); secondSubmission = comparisons.get(i).secondSubmission().copy(); - globalMatches = new ArrayList<>(comparisons.get(i).matches()); + List globalMatches = new ArrayList<>(comparisons.get(i).matches()); globalMatches.addAll(comparisons.get(i).ignoredMatches()); - computeNeighbors(); - mergeNeighbors(); - removeBuffer(); + globalMatches = removeBuffer(mergeNeighbors(globalMatches)); comparisons.set(i, new JPlagComparison(firstSubmission, secondSubmission, globalMatches, new ArrayList<>())); } @@ -62,8 +58,8 @@ public JPlagResult run() { return new JPlagResult(comparisons, result.getSubmissions(), result.getDuration() + durationInMillis, options); } - private void computeNeighbors() { - neighbors = new ArrayList<>(); + private List> computeNeighbors(List globalMatches) { + List> neighbors = new ArrayList<>(); List sortedByFirst = new ArrayList<>(globalMatches); Collections.sort(sortedByFirst, (m1, m2) -> m1.startOfFirst() - m2.startOfFirst()); List sortedBySecond = new ArrayList<>(globalMatches); @@ -73,10 +69,12 @@ private void computeNeighbors() { neighbors.add(Arrays.asList(sortedByFirst.get(i), sortedByFirst.get(i + 1))); } } + return neighbors; } - private void mergeNeighbors() { + private List mergeNeighbors(List globalMatches) { int i = 0; + List> neighbors = computeNeighbors(globalMatches); while (i < neighbors.size()) { int lengthUpper = neighbors.get(i).get(0).length(); int lengthLower = neighbors.get(i).get(1).length(); @@ -88,17 +86,19 @@ private void mergeNeighbors() { globalMatches.removeAll(neighbors.get(i)); globalMatches .add(new Match(neighbors.get(i).get(0).startOfFirst(), neighbors.get(i).get(0).startOfSecond(), lengthUpper + lengthLower)); - removeToken(neighbors.get(i).get(0).startOfFirst(), neighbors.get(i).get(0).startOfSecond(), lengthUpper, seperatingFirst, - seperatingSecond); - computeNeighbors(); + removeToken(globalMatches, neighbors.get(i).get(0).startOfFirst(), neighbors.get(i).get(0).startOfSecond(), lengthUpper, + seperatingFirst, seperatingSecond); + neighbors = computeNeighbors(globalMatches); i = 0; } else { i++; } } + return globalMatches; } - private void removeToken(int startFirst, int startSecond, int lengthUpper, int seperatingFirst, int seperatingSecond) { + private List removeToken(List globalMatches, int startFirst, int startSecond, int lengthUpper, int seperatingFirst, + int seperatingSecond) { List tokenFirst = new ArrayList<>(firstSubmission.getTokenList()); List tokenSecond = new ArrayList<>(secondSubmission.getTokenList()); tokenFirst.subList(startFirst + lengthUpper, startFirst + lengthUpper + seperatingFirst).clear(); @@ -118,9 +118,10 @@ private void removeToken(int startFirst, int startSecond, int lengthUpper, int s globalMatches.set(i, alteredMatch); } } + return globalMatches; } - private void removeBuffer() { + private List removeBuffer(List globalMatches) { List toRemove = new ArrayList<>(); for (Match match : globalMatches) { if (match.length() < options.minimumTokenMatch()) { @@ -128,5 +129,6 @@ private void removeBuffer() { } } globalMatches.removeAll(toRemove); + return globalMatches; } } \ No newline at end of file