Browse Source

Cleanup

Add Docs, comments, remove unused code, fix FileAlreadyExistsException
Nesta Bentum 4 years ago
parent
commit
e8737d8a99

+ 7 - 4
jplag/src/main/java/de/jplag/JPlagResult.java

@@ -136,11 +136,14 @@ public class JPlagResult {
 
     private int[] calculateDistributionFor(List<JPlagComparison> comparisons, Function<JPlagComparison, Float> similarityExtractor) {
         int[] similarityDistribution = new int[SIMILARITY_DISTRIBUTION_SIZE];
+        int similarityDistributionBucketSize = 100 / SIMILARITY_DISTRIBUTION_SIZE;
         for (JPlagComparison comparison : comparisons) {
-            Float similarity = similarityExtractor.apply(comparison);
-            int index = (int) (similarity / SIMILARITY_DISTRIBUTION_SIZE);
-            index = index == SIMILARITY_DISTRIBUTION_SIZE ? SIMILARITY_DISTRIBUTION_SIZE - 1 : index;
-            similarityDistribution[SIMILARITY_DISTRIBUTION_SIZE - 1 - index]++;
+            float similarity = similarityExtractor.apply(comparison); // extract similarity in percent: 0f <= similarity <= 100f
+            int index = (int) (similarity / similarityDistributionBucketSize); // divide similarity by bucket size to find index of correct bucket.
+            if (index == SIMILARITY_DISTRIBUTION_SIZE)
+                index--; // index is out of bounds when similarity is 100%. decrease by one to count towards the highest value bucket
+            similarityDistribution[SIMILARITY_DISTRIBUTION_SIZE - 1 - index]++; // count comparison towards its determined bucket. bucket order is
+                                                                                // reversed, so that the highest value bucket has the lowest index
         }
         return similarityDistribution;
     }

+ 0 - 37
jplag/src/main/java/de/jplag/reporting/jsonfactory/JsonFactory.java

@@ -1,37 +0,0 @@
-package de.jplag.reporting.jsonfactory;
-
-import java.io.IOException;
-import java.nio.file.Path;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-import de.jplag.reporting.reportobject.model.ComparisonReport;
-import de.jplag.reporting.reportobject.model.JPlagReport;
-
-/**
- * Factory class, responsible for creating Json strings and writing them to files.
- */
-public class JsonFactory {
-
-    private static final Logger logger = LoggerFactory.getLogger(JsonFactory.class);
-
-    /**
-     * Creates Json Files for the given JPlagReport and saves them in the given folder.
-     */
-    public static void saveJsonFiles(JPlagReport jPlagReport, String folderPath) {
-        try {
-            ObjectMapper mapper = new ObjectMapper();
-            mapper.writeValue(Path.of(folderPath, "overview.json").toFile(), jPlagReport.overviewReport());
-            mapper.writeValue(Path.of(folderPath, "lookUpTable.json").toFile(), jPlagReport.lineLookUpTable());
-            for (ComparisonReport report : jPlagReport.comparisons()) {
-                String name = report.firstSubmissionId().concat("-").concat(report.secondSubmissionId()).concat(".json");
-                mapper.writeValue(Path.of(folderPath, name).toFile(), report);
-            }
-        } catch (IOException e) {
-            logger.error("Failed to save json files: " + e.getMessage(), e);
-        }
-    }
-}

+ 2 - 1
jplag/src/main/java/de/jplag/reporting/reportobject/ReportObjectFactory.java

@@ -3,6 +3,7 @@ package de.jplag.reporting.reportobject;
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -93,7 +94,7 @@ public class ReportObjectFactory {
             File directory = createDirectory(submissionsPath.getPath(), submission.getName());
             for (var file : submission.getFiles()) {
                 try {
-                    Files.copy(file.toPath(), (new File(directory, file.getName())).toPath());
+                    Files.copy(file.toPath(), (new File(directory, file.getName())).toPath(), StandardCopyOption.REPLACE_EXISTING);
                 } catch (IOException e) {
                     logger.error("Could not save submission file " + file, e);
                 }

+ 7 - 6
jplag/src/main/java/de/jplag/reporting/reportobject/model/ComparisonReport.java

@@ -4,14 +4,15 @@ import java.util.List;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
 
+/**
+ * ReportViewer DTO for the comparison of two submissions.
+ * @param firstSubmissionId id of the first submission
+ * @param secondSubmissionId id of the second submission
+ * @param matchPercentage similarity in percent. between 0f and 100f.
+ * @param matches the list of matches found in the comparison of the two submissions
+ */
 public record ComparisonReport(@JsonProperty("first_submission_id") String firstSubmissionId,
         @JsonProperty("second_submission_id") String secondSubmissionId, @JsonProperty("match_percentage") float matchPercentage,
         @JsonProperty("matches") List<Match> matches) {
 
-    public ComparisonReport(String firstSubmissionId, String secondSubmissionId, float matchPercentage, List<Match> matches) {
-        this.firstSubmissionId = firstSubmissionId;
-        this.secondSubmissionId = secondSubmissionId;
-        this.matchPercentage = matchPercentage;
-        this.matches = matches;
-    }
 }

+ 0 - 7
jplag/src/main/java/de/jplag/reporting/reportobject/model/JPlagReport.java

@@ -1,7 +0,0 @@
-package de.jplag.reporting.reportobject.model;
-
-import java.util.List;
-import java.util.Map;
-
-public record JPlagReport(OverviewReport overviewReport, List<ComparisonReport> comparisons, Map<Long, String> lineLookUpTable) {
-}