فهرست منبع

Merge pull request #566 from jplag/fix-warnings

Fix warnings of Sonarcloud
Timur Sağlam 4 سال پیش
والد
کامیت
0c569ae8af

+ 1 - 3
jplag.frontend.cpp/src/main/javacc/CPP.jj

@@ -19,10 +19,8 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.InputStream;
 
 
-import de.jplag.cpp.NewlineStream;
-
 public class CPPScanner implements CPPTokenConstants {
 public class CPPScanner implements CPPTokenConstants {
-    private static Scanner delegatingScanner;
+    private Scanner delegatingScanner;
 
 
     public static boolean scanFile(File dir, String fileName, Scanner delegatingScanner) {
     public static boolean scanFile(File dir, String fileName, Scanner delegatingScanner) {
         CPPScanner scanner;
         CPPScanner scanner;

+ 13 - 10
jplag/src/main/java/de/jplag/JPlagComparison.java

@@ -1,13 +1,13 @@
 package de.jplag;
 package de.jplag;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
-import java.util.Comparator;
 import java.util.List;
 import java.util.List;
+import java.util.Objects;
 
 
 /**
 /**
  * This method represents the whole result of a comparison between two submissions.
  * This method represents the whole result of a comparison between two submissions.
  */
  */
-public class JPlagComparison implements Comparator<JPlagComparison> { // FIXME TS: contains a lot of code duplication
+public class JPlagComparison { // FIXME TS: contains a lot of code duplication
 
 
     private static final int ROUNDING_FACTOR = 10;
     private static final int ROUNDING_FACTOR = 10;
 
 
@@ -35,18 +35,21 @@ public class JPlagComparison implements Comparator<JPlagComparison> { // FIXME T
         matches.add(new Match(startOfFirst, startOfSecond, length));
         matches.add(new Match(startOfFirst, startOfSecond, length));
     }
     }
 
 
-    @Override
-    public int compare(JPlagComparison comparison1, JPlagComparison comparison2) {
-        return Float.compare(comparison2.similarity(), comparison1.similarity()); // comparison2 first!
-    }
-
-    // TODO DF: hashCode is not implemented!!
     @Override
     @Override
     public boolean equals(Object other) {
     public boolean equals(Object other) {
-        if (!(other instanceof JPlagComparison)) {
+        if (other == this) {
+            return true;
+        }
+        if (!(other instanceof JPlagComparison otherComparison)) {
             return false;
             return false;
         }
         }
-        return (compare(this, (JPlagComparison) other) == 0);
+        return firstSubmission.equals(otherComparison.getFirstSubmission()) && secondSubmission.equals(otherComparison.getSecondSubmission())
+                && matches.equals(otherComparison.matches);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(firstSubmission, secondSubmission, matches);
     }
     }
 
 
     /**
     /**

+ 17 - 1
jplag/src/main/java/de/jplag/Submission.java

@@ -5,6 +5,7 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Path;
 import java.util.Collection;
 import java.util.Collection;
+import java.util.Objects;
 
 
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
@@ -75,10 +76,25 @@ public class Submission implements Comparable<Submission> {
 
 
     @Override
     @Override
     public int compareTo(Submission other) {
     public int compareTo(Submission other) {
-        // TODO DF: "equals(Object obj)" should be overridden along with the "compareTo(T obj)" method
         return name.compareTo(other.name);
         return name.compareTo(other.name);
     }
     }
 
 
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (!(obj instanceof Submission otherSubmission)) {
+            return false;
+        }
+        return otherSubmission.getName().equals(name);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(name);
+    }
+
     /**
     /**
      * @return base code comparison
      * @return base code comparison
      */
      */

+ 20 - 20
jplag/src/main/java/de/jplag/SubmissionSetBuilder.java

@@ -129,27 +129,27 @@ public class SubmissionSetBuilder {
 
 
     private Optional<Submission> loadBaseCode(Set<File> submissionDirectories, Set<File> oldSubmissionDirectories,
     private Optional<Submission> loadBaseCode(Set<File> submissionDirectories, Set<File> oldSubmissionDirectories,
             Map<File, Submission> foundSubmissions) throws ExitException {
             Map<File, Submission> foundSubmissions) throws ExitException {
-        // Extract the basecode submission if necessary.
-        Optional<Submission> baseCodeSubmission = Optional.empty();
-        if (options.hasBaseCode()) {
-            String baseCodeName = options.getBaseCodeSubmissionName().orElseThrow();
-            Submission baseCode = loadBaseCodeAsPath(baseCodeName);
-            if (baseCode == null) {
-                int numberOfRootDirectories = submissionDirectories.size() + oldSubmissionDirectories.size();
-                if (numberOfRootDirectories > 1) {
-                    throw new BasecodeException("The base code submission needs to be specified by path instead of by name!");
-                }
-
-                // There is one root directory, and the submissionDirectories variable has been checked to be non-empty.
-                // That set thus contains the one and only root directory.
-                File rootDirectory = submissionDirectories.iterator().next();
-
-                // Single root-directory, try the legacy way of specifying basecode.
-                baseCode = loadBaseCodeViaName(baseCodeName, rootDirectory, foundSubmissions);
+        if (!options.hasBaseCode()) {
+            return Optional.empty();
+        }
+
+        String baseCodeName = options.getBaseCodeSubmissionName().orElseThrow();
+        Submission baseCode = loadBaseCodeAsPath(baseCodeName);
+        if (baseCode == null) {
+            int numberOfRootDirectories = submissionDirectories.size() + oldSubmissionDirectories.size();
+            if (numberOfRootDirectories > 1) {
+                throw new BasecodeException("The base code submission needs to be specified by path instead of by name!");
             }
             }
-            // TODO DF: Here the method assumes that baseCode can be null. later, this is not assumed anymore
-            baseCodeSubmission = Optional.ofNullable(baseCode);
 
 
+            // There is one root directory, and the submissionDirectories variable has been checked to be non-empty.
+            // That set thus contains the one and only root directory.
+            File rootDirectory = submissionDirectories.iterator().next();
+
+            // Single root-directory, try the legacy way of specifying basecode.
+            baseCode = loadBaseCodeViaName(baseCodeName, rootDirectory, foundSubmissions);
+        }
+
+        if (baseCode != null) {
             logger.info("Basecode directory \"{}\" will be used.", baseCode.getName());
             logger.info("Basecode directory \"{}\" will be used.", baseCode.getName());
 
 
             // Basecode may also be registered as a user submission. If so, remove the latter.
             // Basecode may also be registered as a user submission. If so, remove the latter.
@@ -158,7 +158,7 @@ public class SubmissionSetBuilder {
                 logger.info("Submission \"{}\" is the specified basecode, it will be skipped during comparison.", removed.getName());
                 logger.info("Submission \"{}\" is the specified basecode, it will be skipped during comparison.", removed.getName());
             }
             }
         }
         }
-        return baseCodeSubmission;
+        return Optional.ofNullable(baseCode);
     }
     }
 
 
     /**
     /**

+ 3 - 2
jplag/src/main/java/de/jplag/clustering/algorithm/BayesianOptimization.java

@@ -45,6 +45,9 @@ public class BayesianOptimization {
      * @param lengthScale width parameter for the matern kernel
      * @param lengthScale width parameter for the matern kernel
      */
      */
     public BayesianOptimization(RealVector minima, RealVector maxima, int initPoints, int maxEvaluations, double noise, RealVector lengthScale) {
     public BayesianOptimization(RealVector minima, RealVector maxima, int initPoints, int maxEvaluations, double noise, RealVector lengthScale) {
+        if (minima.getDimension() == 0) {
+            throw new IllegalArgumentException("explored parameters must at least have one dimension");
+        }
         if (minima.getDimension() != maxima.getDimension()) {
         if (minima.getDimension() != maxima.getDimension()) {
             throw new DimensionMismatchException(minima.getDimension(), maxima.getDimension());
             throw new DimensionMismatchException(minima.getDimension(), maxima.getDimension());
         }
         }
@@ -162,7 +165,6 @@ public class BayesianOptimization {
                 if (debug && logger.isDebugEnabled()) {
                 if (debug && logger.isDebugEnabled()) {
                     logger.debug(gpr.toString(minima, maxima, 100, 25, 0));
                     logger.debug(gpr.toString(minima, maxima, 100, 25, 0));
                 }
                 }
-                // TODO Check that best is not null here
                 coordinates = maxAcq(gpr, best.score, poiSampler, zeroAcquisitionsCounter);
                 coordinates = maxAcq(gpr, best.score, poiSampler, zeroAcquisitionsCounter);
                 testedCoordinates.add(coordinates);
                 testedCoordinates.add(coordinates);
             }
             }
@@ -173,7 +175,6 @@ public class BayesianOptimization {
                 best = result;
                 best = result;
             }
             }
         }
         }
-
         return best;
         return best;
     }
     }