Skip to content

Commit

Permalink
Minor code quality fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaglam committed Jun 7, 2021
1 parent 409007b commit f6e9064
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 46 deletions.
1 change: 1 addition & 0 deletions jplag/src/main/java/jplag/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;

import jplag.options.JPlagOptions;
import jplag.options.LanguageOption;
import jplag.options.Verbosity;
import jplag.reporting.Report;
Expand Down
1 change: 1 addition & 0 deletions jplag/src/main/java/jplag/JPlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Vector;
import java.util.stream.Collectors;

import jplag.options.JPlagOptions;
import jplag.options.LanguageOption;
import jplag.strategy.ComparisonMode;
import jplag.strategy.ComparisonStrategy;
Expand Down
2 changes: 2 additions & 0 deletions jplag/src/main/java/jplag/JPlagResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import jplag.options.JPlagOptions;

/**
* Encapsulates the results of a comparison of a set of source code submissions.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package jplag;
package jplag.options;

import static jplag.strategy.ComparisonMode.NORMAL;

import jplag.options.LanguageOption;
import jplag.options.SimilarityMetric;
import jplag.options.Verbosity;
import jplag.Language;
import jplag.strategy.ComparisonMode;

public class JPlagOptions {
Expand Down Expand Up @@ -115,7 +113,7 @@ public JPlagOptions(String rootDirName, LanguageOption languageOption) {
* not set by the user.
* @param language - initialized language instance
*/
void setLanguageDefaults(Language language) {
public void setLanguageDefaults(Language language) {
if (!this.hasMinTokenMatch()) {
this.minTokenMatch = language.min_token_match();
}
Expand Down Expand Up @@ -203,7 +201,7 @@ public SimilarityMetric getSimilarityMetric() {
return similarityMetric;
}

void setLanguage(Language language) {
public void setLanguage(Language language) {
this.language = language;
}

Expand Down
5 changes: 2 additions & 3 deletions jplag/src/main/java/jplag/reporting/BufferedCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.io.IOException;
import java.io.Writer;

/*
* This class counts the number of printed characters.
/**
* A buffered writer that counts the number of printed characters.
*/
public class BufferedCounter extends BufferedWriter {

Expand All @@ -16,7 +16,6 @@ public BufferedCounter(Writer out) {
count = 0;
}


@Override
public void write(int c) throws IOException {
super.write(c);
Expand Down
18 changes: 7 additions & 11 deletions jplag/src/main/java/jplag/reporting/HTMLFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jplag.reporting;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -10,25 +9,22 @@

public class HTMLFile extends PrintWriter {

private BufferedCounter bc;
private BufferedCounter counter;

/**
* Static factory method to instantiate an HTMLFile objects.
*/
public static HTMLFile fromFile(File file) throws IOException {
BufferedCounter bc = new BufferedCounter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));

HTMLFile htmlFile = new HTMLFile(bc);
htmlFile.bc = bc;

return htmlFile;
BufferedCounter counter = new BufferedCounter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
return new HTMLFile(counter);
}

private HTMLFile(BufferedWriter writer) {
super(writer);
private HTMLFile(BufferedCounter counter) {
super(counter);
this.counter = counter;
}

public int bytesWritten() {
return bc.bytesWritten();
return counter.bytesWritten();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import jplag.GreedyStringTiling;
import jplag.JPlagBaseCodeComparison;
import jplag.JPlagComparison;
import jplag.JPlagOptions;
import jplag.Submission;
import jplag.options.JPlagOptions;
import jplag.options.SimilarityMetric;

public abstract class AbstractComparisonStrategy implements ComparisonStrategy {
Expand All @@ -16,28 +16,28 @@ public abstract class AbstractComparisonStrategy implements ComparisonStrategy {
// Hashtable that maps the name of a submissions to its matches with the provided base code.
protected Hashtable<String, JPlagBaseCodeComparison> baseCodeMatches = new Hashtable<>(30);

protected GreedyStringTiling gSTiling;
protected GreedyStringTiling greedyStringTiling;

protected JPlagOptions options;

public AbstractComparisonStrategy(JPlagOptions options, GreedyStringTiling gSTiling) {
this.gSTiling = gSTiling;
public AbstractComparisonStrategy(JPlagOptions options, GreedyStringTiling greedyStringTiling) {
this.greedyStringTiling = greedyStringTiling;
this.options = options;
}

protected void compareSubmissionsToBaseCode(Vector<Submission> submissions, Submission baseCodeSubmission) {
int numberOfSubmissions = submissions.size();

JPlagBaseCodeComparison bcMatch;
JPlagBaseCodeComparison baseCodeMatch;
Submission currentSubmission;

for (int i = 0; i < (numberOfSubmissions); i++) {
currentSubmission = submissions.elementAt(i);

bcMatch = this.gSTiling.compareWithBaseCode(currentSubmission, baseCodeSubmission);
baseCodeMatches.put(currentSubmission.name, bcMatch);
baseCodeMatch = greedyStringTiling.compareWithBaseCode(currentSubmission, baseCodeSubmission);
baseCodeMatches.put(currentSubmission.name, baseCodeMatch);

this.gSTiling.resetBaseSubmission(baseCodeSubmission);
greedyStringTiling.resetBaseSubmission(baseCodeSubmission);
}
}

Expand Down
26 changes: 8 additions & 18 deletions jplag/src/main/java/jplag/strategy/NormalComparisonStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,50 @@

import jplag.GreedyStringTiling;
import jplag.JPlagComparison;
import jplag.JPlagOptions;
import jplag.JPlagResult;
import jplag.Submission;
import jplag.options.JPlagOptions;

public class NormalComparisonStrategy extends AbstractComparisonStrategy {

public NormalComparisonStrategy(JPlagOptions options, GreedyStringTiling gSTiling) {
super(options, gSTiling);
public NormalComparisonStrategy(JPlagOptions options, GreedyStringTiling greedyStringTiling) {
super(options, greedyStringTiling);
}

@Override
public JPlagResult compareSubmissions(Vector<Submission> submissions, Submission baseCodeSubmission) {
if (baseCodeSubmission != null) {
compareSubmissionsToBaseCode(submissions, baseCodeSubmission);
}

long timeBeforeStartInMillis = System.currentTimeMillis();
int i, j, numberOfSubmissions = submissions.size();
Submission first, second;
JPlagComparison comparison;

List<JPlagComparison> comparisons = new ArrayList<>();

JPlagComparison comparison;

for (i = 0; i < (numberOfSubmissions - 1); i++) {
first = submissions.elementAt(i);

if (first.tokenList == null) {
continue;
}

for (j = (i + 1); j < numberOfSubmissions; j++) {
second = submissions.elementAt(j);

if (second.tokenList == null) {
continue;
}

comparison = this.gSTiling.compare(first, second);

comparison = greedyStringTiling.compare(first, second);
System.out.println("Comparing " + first.name + "-" + second.name + ": " + comparison.percent());

if (baseCodeSubmission != null) {
comparison.bcMatchesA = baseCodeMatches.get(comparison.firstSubmission.name);
comparison.bcMatchesB = baseCodeMatches.get(comparison.secondSubmission.name);
}

if (isAboveSimilarityThreshold(comparison)) {
comparisons.add(comparison);
}
}
}

long durationInMillis = System.currentTimeMillis() - timeBeforeStartInMillis;

return new JPlagResult(comparisons, durationInMillis, numberOfSubmissions, options);
}

Expand Down

0 comments on commit f6e9064

Please sign in to comment.