Skip to content

Commit

Permalink
Fixed Anti Pattern of overusing fields instead of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
uuqjz committed Aug 4, 2023
1 parent 0692c4f commit 00256b6
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions core/src/main/java/de/jplag/merging/MatchMerging.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
public class MatchMerging {
private Submission firstSubmission;
private Submission secondSubmission;
private List<Match> globalMatches;
private List<List<Match>> neighbors;
private JPlagResult result;
private List<JPlagComparison> comparisons;
private JPlagOptions options;
Expand All @@ -50,20 +48,18 @@ 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<Match> 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<>()));

}
long durationInMillis = System.currentTimeMillis() - timeBeforeStartInMillis;
return new JPlagResult(comparisons, result.getSubmissions(), result.getDuration() + durationInMillis, options);
}

private void computeNeighbors() {
neighbors = new ArrayList<>();
private List<List<Match>> computeNeighbors(List<Match> globalMatches) {
List<List<Match>> neighbors = new ArrayList<>();
List<Match> sortedByFirst = new ArrayList<>(globalMatches);
Collections.sort(sortedByFirst, (m1, m2) -> m1.startOfFirst() - m2.startOfFirst());
List<Match> sortedBySecond = new ArrayList<>(globalMatches);
Expand All @@ -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<Match> mergeNeighbors(List<Match> globalMatches) {
int i = 0;
List<List<Match>> neighbors = computeNeighbors(globalMatches);
while (i < neighbors.size()) {
int lengthUpper = neighbors.get(i).get(0).length();
int lengthLower = neighbors.get(i).get(1).length();
Expand All @@ -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<Match> removeToken(List<Match> globalMatches, int startFirst, int startSecond, int lengthUpper, int seperatingFirst,
int seperatingSecond) {
List<Token> tokenFirst = new ArrayList<>(firstSubmission.getTokenList());
List<Token> tokenSecond = new ArrayList<>(secondSubmission.getTokenList());
tokenFirst.subList(startFirst + lengthUpper, startFirst + lengthUpper + seperatingFirst).clear();
Expand All @@ -118,15 +118,17 @@ private void removeToken(int startFirst, int startSecond, int lengthUpper, int s
globalMatches.set(i, alteredMatch);
}
}
return globalMatches;
}

private void removeBuffer() {
private List<Match> removeBuffer(List<Match> globalMatches) {
List<Match> toRemove = new ArrayList<>();
for (Match match : globalMatches) {
if (match.length() < options.minimumTokenMatch()) {
toRemove.add(match);
}
}
globalMatches.removeAll(toRemove);
return globalMatches;
}
}

0 comments on commit 00256b6

Please sign in to comment.