Bläddra i källkod

Introduce shared default values.

Timur Sağlam 5 år sedan
förälder
incheckning
7c2d4a9f8b

+ 32 - 30
jplag/src/main/java/jplag/CommandLineArgument.java

@@ -1,6 +1,8 @@
 package jplag;
 
-import static jplag.strategy.ComparisonMode.NORMAL;
+import static jplag.options.JPlagOptions.DEFAULT_COMPARISON_MODE;
+import static jplag.options.JPlagOptions.DEFAULT_SIMILARITY_THRESHOLD;
+import static jplag.options.JPlagOptions.DEFAULT_STORED_MATCHES;
 import static net.sourceforge.argparse4j.impl.Arguments.storeTrue;
 
 import java.util.Collection;
@@ -21,16 +23,16 @@ public enum CommandLineArgument {
     ROOT_DIRECTORY("rootDir", String.class, "The root-directory that contains all submissions"),
     LANGUAGE("-l", String.class, "Select the language to parse the submissions", LanguageOption.getDefault().getDisplayName(), LanguageOption.getAllDisplayNames()),
     BASE_CODE("-bc", String.class, "Name of the directory which contains the base code (common framework used in all submissions)"),
-    VERBOSITY("-v", String.class, "Verbosity", "quiet", List.of("parser", "quiet", "long", "details")), // TODO SH: Replace verbosity when integrating  a real logging library
+    VERBOSITY("-v", String.class, "Verbosity", "quiet", List.of("parser", "quiet", "long", "details")), // TODO SH: Replace verbosity when integrating a real logging library
     DEBUG("-d", Boolean.class, "(Debug) parser. Non-parsable files will be stored"),
     SUBDIRECTORY("-S", String.class, "Look in directories <root-dir>/*/<dir> for programs"),
     SUFFIXES("-p", String.class, "comma-separated list of all filename suffixes that are included"),
     EXCLUDE_FILE("-x", String.class, "All files named in this file will be ignored in the comparison (line-separated list)"),
     MIN_TOKEN_MATCH("-t", Integer.class, "Tune the sensitivity of the comparison. A smaller <n> increases the sensitivity"),
-    SIMILARITY_THRESHOLD("-m", Float.class, "Match similarity threshold [0-100]: All matches above this threshold will be saved", 0f), // TODO TS deduplicate default values
-    STORED_MATCHES("-n", Integer.class, "Maximum number of matches that will be saved. If set to -1 all matches will be saved", 30),
+    SIMILARITY_THRESHOLD("-m", Float.class, "Match similarity threshold [0-100]: All matches above this threshold will be saved", DEFAULT_SIMILARITY_THRESHOLD),
+    STORED_MATCHES("-n", Integer.class, "Maximum number of matches that will be saved. If set to -1 all matches will be saved", DEFAULT_STORED_MATCHES),
     RESULT_FOLDER("-r", String.class, "Name of directory in which the comparison results will be stored", "result"),
-    COMPARISON_MODE("-c", String.class, "Comparison mode used to compare the programs", NORMAL.getName(), ComparisonMode.allNames());
+    COMPARISON_MODE("-c", String.class, "Comparison mode used to compare the programs", DEFAULT_COMPARISON_MODE, ComparisonMode.allNames());
 
     private final String flag;
     private final String helptext;
@@ -38,38 +40,31 @@ public enum CommandLineArgument {
     private final Optional<Collection<String>> choices;
     private final Class<?> type;
 
-    private CommandLineArgument(String flag, Class<?> type, String helpText, Optional<Object> defaultValue, Optional<Collection<String>> choices) {
-        this.flag = flag;
-        this.helptext = helpText;
-        this.type = type;
-        this.defaultValue = defaultValue;
-        this.choices = choices;
-    }
-
-    private CommandLineArgument(String flag, Class<?> type, String helpText, Object defaultValue, Collection<String> choices) {
-        this(flag, type, helpText, Optional.of(defaultValue), Optional.of(choices));
+    private CommandLineArgument(String flag, Class<?> type, String helpText) {
+        this(flag, type, helpText, Optional.empty(), Optional.empty());
     }
 
     private CommandLineArgument(String flag, Class<?> type, String helpText, Object defaultValue) {
         this(flag, type, helpText, Optional.of(defaultValue), Optional.empty());
     }
 
-    private CommandLineArgument(String flag, Class<?> type, String helpText) {
-        this(flag, type, helpText, Optional.empty(), Optional.empty());
+    private CommandLineArgument(String flag, Class<?> type, String helpText, Object defaultValue, Collection<String> choices) {
+        this(flag, type, helpText, Optional.of(defaultValue), Optional.of(choices));
+    }
+
+    private CommandLineArgument(String flag, Class<?> type, String helpText, Optional<Object> defaultValue, Optional<Collection<String>> choices) {
+        this.flag = flag;
+        this.helptext = helpText;
+        this.type = type;
+        this.defaultValue = defaultValue;
+        this.choices = choices;
     }
 
     /**
-     * Parses the command line argument with a specific parser.
-     * @param parser is that parser.
+     * @return the flag name of the command line argument without leading dashes.
      */
-    public void parseWith(ArgumentParser parser) {
-        Argument argument = parser.addArgument(flag).help(helptext);
-        choices.ifPresent(it -> argument.choices(it));
-        defaultValue.ifPresent(it -> argument.setDefault(it));
-        argument.type(type);
-        if (type == Boolean.class) {
-            argument.action(storeTrue());
-        }
+    public String flag() {
+        return flag.replace("-", "");
     }
 
     /**
@@ -84,9 +79,16 @@ public enum CommandLineArgument {
     }
 
     /**
-     * @return the flag name of the command line argument without leading dashes.
+     * Parses the command line argument with a specific parser.
+     * @param parser is that parser.
      */
-    public String flag() {
-        return flag.replace("-", "");
+    public void parseWith(ArgumentParser parser) {
+        Argument argument = parser.addArgument(flag).help(helptext);
+        choices.ifPresent(it -> argument.choices(it));
+        defaultValue.ifPresent(it -> argument.setDefault(it));
+        argument.type(type);
+        if (type == Boolean.class) {
+            argument.action(storeTrue());
+        }
     }
 }

+ 7 - 22
jplag/src/main/java/jplag/options/JPlagOptions.java

@@ -7,26 +7,19 @@ import jplag.strategy.ComparisonMode;
 
 public class JPlagOptions {
 
+    public static final ComparisonMode DEFAULT_COMPARISON_MODE = NORMAL;
+    public static final int DEFAULT_SIMILARITY_THRESHOLD = 0;
+    public static final int DEFAULT_STORED_MATCHES = 30;
+
     /**
      * Language used to parse the submissions.
      */
     private Language language;
 
-    /**
-     * TODO PB: Decide what to do with this.
-     * <p>
-     * Note: Previously, this option had two effects:
-     * <ol>
-     * <li>If this option was > 0, it told JPlag to use the 'special' comparison strategy</li>
-     * <li>It specifies the number of submissions to compare each submission to during the 'special' comparison</li>
-     * </ol>
-     */
-    private int numberOfSubmissionsToCompareTo = 0; // 0 = deactivated
-
     /**
      * Determines which strategy to use for the comparison of submissions.
      */
-    private ComparisonMode comparisonMode = NORMAL;
+    private ComparisonMode comparisonMode = DEFAULT_COMPARISON_MODE;
 
     /**
      * If true, submissions that cannot be parsed will be stored in a separate directory.
@@ -42,13 +35,13 @@ public class JPlagOptions {
      * Percentage value (must be between 0 and 100). Matches with a similarity below this threshold will be ignored. The
      * default value of 0 allows all matches to be stored.
      */
-    private float similarityThreshold = 0;
+    private float similarityThreshold = DEFAULT_SIMILARITY_THRESHOLD;
 
     /**
      * The maximum number of matches that will be saved. This does affect the generated report as well as the internally
      * saved comparisons. If set to -1 all matches will be saved.
      */
-    private int maxNumberOfMatches = 30; // TODO TS deduplicate default values
+    private int maxNumberOfMatches = DEFAULT_STORED_MATCHES;
 
     /**
      * TODO PB: Not happy with the name yet.
@@ -170,10 +163,6 @@ public class JPlagOptions {
         return debugParser;
     }
 
-    public int getNumberOfSubmissionsToCompareTo() {
-        return numberOfSubmissionsToCompareTo;
-    }
-
     public float getSimilarityThreshold() {
         return similarityThreshold;
     }
@@ -190,10 +179,6 @@ public class JPlagOptions {
         this.language = language;
     }
 
-    public void setNumberOfSubmissionsToCompareTo(int numberOfSubmissionsToCompareTo) {
-        this.numberOfSubmissionsToCompareTo = numberOfSubmissionsToCompareTo;
-    }
-
     public void setComparisonMode(ComparisonMode comparisonMode) {
         this.comparisonMode = comparisonMode;
     }