Skip to content

Commit

Permalink
Cleanup Code
Browse files Browse the repository at this point in the history
  • Loading branch information
nestabentum committed Aug 19, 2022
1 parent a0eeccd commit bcd843b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import de.jplag.reporting.reportobject.model.ComparisonReport;
import de.jplag.reporting.reportobject.model.Match;

/**
* Writes {@link ComparisonReport}s of given {@link JPlagResult} to the disk under the specified path. Instantiated with
* a function that associates a submission to its id.
*/
public class ComparisonReportWriter {

private static final FileWriter FILE_WRITER = new FileWriter();
Expand Down Expand Up @@ -54,19 +58,14 @@ private void addToLookUp(String firstSubmissionId, String secondSubmissionId, St
}

private void writeToMap(String id1, String id2, String comparisonFileName) {
if (submissionIdToComparisonFileName.containsKey(id1)) {
submissionIdToComparisonFileName.get(id1).put(id2, comparisonFileName);
} else {
HashMap<String, String> map = new HashMap<>();
map.put(id2, comparisonFileName);
submissionIdToComparisonFileName.put(id1, map);
}
submissionIdToComparisonFileName.putIfAbsent(id1, new HashMap<>());
submissionIdToComparisonFileName.get(id1).put(id2, comparisonFileName);
}

private String generateComparisonName(String firstSubmissionId, String secondSubmissionId) {
String name = concatenate(firstSubmissionId, secondSubmissionId);
String finalName = name;
var timesNameAlreadyExists = submissionIdToComparisonFileName.values().stream().filter(map -> map.containsValue(finalName)).count();
long timesNameAlreadyExists = submissionIdToComparisonFileName.values().stream().filter(map -> map.containsValue(finalName)).count();
if (timesNameAlreadyExists > 0) {
name = concatenate(firstSubmissionId, secondSubmissionId, timesNameAlreadyExists + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Provides Methods for creating directories.
*/
public class DirectoryCreator {
private static final Logger logger = LoggerFactory.getLogger(DirectoryCreator.class);

/**
* Creates a directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ private void copySubmissionFilesToReport(String path, JPlagResult result) {
List<JPlagComparison> comparisons = result.getComparisons(result.getOptions().getMaximumNumberOfComparisons());
var submissions = getSubmissions(comparisons);
var submissionsPath = createSubmissionsDirectory(path);
if (submissionsPath == null)
if (submissionsPath == null) {
return;
}
Language language = result.getOptions().getLanguage();
for (var submission : submissions) {
File directory = createSubmissionDirectory(path, submissionsPath, submission);
if (directory == null)
if (directory == null) {
continue;
}
for (var file : submission.getFiles()) {
var fileToCopy = language.useViewFiles() ? new File(file.getPath() + language.viewFileSuffix()) : file;
try {
Expand All @@ -89,25 +91,21 @@ private void copySubmissionFilesToReport(String path, JPlagResult result) {
}

private File createSubmissionDirectory(String path, File submissionsPath, Submission submission) {
File directory;
try {
directory = createDirectory(submissionsPath.getPath(), submissionToIdFunction.apply(submission));
return createDirectory(submissionsPath.getPath(), submissionToIdFunction.apply(submission));
} catch (IOException e) {
logger.error("Could not create directory " + path + " for report viewer generation", e);
return null;
}
return directory;
}

private File createSubmissionsDirectory(String path) {
File submissionsPath;
try {
submissionsPath = createDirectory(path, SUBMISSIONS_FOLDER);
return createDirectory(path, SUBMISSIONS_FOLDER);
} catch (IOException e) {
logger.error("Could not create directory " + path + " for report viewer generation", e);
return null;
}
return submissionsPath;
}

private void writeComparisons(JPlagResult result, String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
import de.jplag.JPlagResult;
import de.jplag.Submission;

/**
* Responsible for creating a mapping of all Submissions to their respective report viewer ids. This mapping is achieved
* by associating the {@link Submission#getName()} (JPlag's internal Submission uid) to a report viewer id. Currently,
* the sanitized version of the {@link Submission#getName()} serves as report viewer id.
*/
public class SubmissionNameToIdMapper {

private static final String FILE_SEPARATOR_REPLACEMENT = "_";

/**
* Builds a map that associates a Submission by its JPlag Id ({@link Submission#getName()}) to its report viewer id.
* @return A Map of containing an entry [name of submission -> report viewer id of submission] for each submission of
Expand All @@ -25,7 +33,7 @@ public static Map<String, String> buildSubmissionNameToIdMap(JPlagResult result)
}

private static String sanitizeNameOf(Submission comparison) {
return comparison.getName().replace(File.separator, "_");
return comparison.getName().replace(File.separator, FILE_SEPARATOR_REPLACEMENT);
}

private static List<JPlagComparison> getComparisons(JPlagResult result) {
Expand Down

0 comments on commit bcd843b

Please sign in to comment.