Explorar o código

Overhaul match class: Adjust field visibility, remove set method, make fields final, add comments.

Timur Sağlam %!s(int64=5) %!d(string=hai) anos
pai
achega
d955fd8306

+ 4 - 4
jplag/src/main/java/jplag/GreedyStringTiling.java

@@ -182,11 +182,11 @@ public class GreedyStringTiling implements TokenConstants {
                 }
             }
             for (int i = matches.size() - 1; i >= 0; i--) {
-                int x = matches.get(i).startA;  // Beginning of/in sequence A
-                int y = matches.get(i).startB;  // Beginning of/in sequence B
-                comparison.addMatch(x, y, matches.get(i).length);
+                int x = matches.get(i).getStartA();  // Beginning of/in sequence A
+                int y = matches.get(i).getStartB();  // Beginning of/in sequence B
+                comparison.addMatch(x, y, matches.get(i).getLength());
                 // in order that "Match" will be newly build (because reusing)
-                for (int j = matches.get(i).length; j > 0; j--) {
+                for (int j = matches.get(i).getLength(); j > 0; j--) {
                     first.getToken(x).marked = second.getToken(y).marked = true; // mark all Tokens!
                     if (withBaseCode) {
                         first.getToken(x).basecode = second.getToken(y).basecode = true;

+ 12 - 12
jplag/src/main/java/jplag/JPlagComparison.java

@@ -49,7 +49,7 @@ public class JPlagComparison implements Comparator<JPlagComparison> {
         if (s == 0) {     // submission A
             for (i = 1; i < size; i++) {
                 for (j = 0; j < (size - i); j++) {
-                    if (matches.get(perm[j]).startA > matches.get(perm[j + 1]).startA) {
+                    if (matches.get(perm[j]).getStartA() > matches.get(perm[j + 1]).getStartA()) {
                         tmp = perm[j];
                         perm[j] = perm[j + 1];
                         perm[j + 1] = tmp;
@@ -59,7 +59,7 @@ public class JPlagComparison implements Comparator<JPlagComparison> {
         } else {        // submission B
             for (i = 1; i < size; i++) {
                 for (j = 0; j < (size - i); j++) {
-                    if (matches.get(perm[j]).startB > matches.get(perm[j + 1]).startB) {
+                    if (matches.get(perm[j]).getStartB() > matches.get(perm[j + 1]).getStartB()) {
                         tmp = perm[j];
                         perm[j] = perm[j + 1];
                         perm[j + 1] = tmp;
@@ -81,22 +81,22 @@ public class JPlagComparison implements Comparator<JPlagComparison> {
         int numberOfMatchedTokens = 0;
 
         for (Match match : matches) {
-            numberOfMatchedTokens += match.length;
+            numberOfMatchedTokens += match.getLength();
         }
 
         return numberOfMatchedTokens;
     }
 
     private int biggestMatch() {
-        int erg = 0;
+        int maximalMatchLenght = 0;
 
         for (Match match : matches) {
-            if (match.length > erg) {
-                erg = match.length;
+            if (match.getLength() > maximalMatchLenght) {
+                maximalMatchLenght = match.getLength();
             }
         }
 
-        return erg;
+        return maximalMatchLenght;
     }
 
     public final float roundedPercent() {
@@ -188,9 +188,9 @@ public class JPlagComparison implements Comparator<JPlagComparison> {
         int i, h, starti, starth, count = 1;
 
         o1: for (i = 1; i < matches.size(); i++) {
-            starti = (j == 0 ? matches.get(i).startA : matches.get(i).startB);
+            starti = (j == 0 ? matches.get(i).getStartA() : matches.get(i).getStartB());
             for (h = 0; h < i; h++) {
-                starth = (j == 0 ? matches.get(h).startA : matches.get(h).startB);
+                starth = (j == 0 ? matches.get(h).getStartA() : matches.get(h).getStartB());
                 if (tokenList.getToken(starti).file.equals(tokenList.getToken(starth).file)) {
                     continue o1;
                 }
@@ -199,13 +199,13 @@ public class JPlagComparison implements Comparator<JPlagComparison> {
         }
 
         String[] res = new String[count];
-        res[0] = tokenList.getToken((j == 0 ? matches.get(0).startA : matches.get(0).startB)).file;
+        res[0] = tokenList.getToken((j == 0 ? matches.get(0).getStartA() : matches.get(0).getStartB())).file;
         count = 1;
 
         o2: for (i = 1; i < matches.size(); i++) {
-            starti = (j == 0 ? matches.get(i).startA : matches.get(i).startB);
+            starti = (j == 0 ? matches.get(i).getStartA() : matches.get(i).getStartB());
             for (h = 0; h < i; h++) {
-                starth = (j == 0 ? matches.get(h).startA : matches.get(h).startB);
+                starth = (j == 0 ? matches.get(h).getStartA() : matches.get(h).getStartB());
                 if (tokenList.getToken(starti).file.equals(tokenList.getToken(starth).file)) {
                     continue o2;
                 }

+ 35 - 7
jplag/src/main/java/jplag/Match.java

@@ -1,23 +1,51 @@
 package jplag;
 
+/**
+ * Represents two sections of two submissions A and B that are similar.
+ */
 public class Match {
 
-    public int startA;
-    public int startB;
-    public int length;
+    private final int startA;
+    private final int startB;
+    private final int length;
 
+    /**
+     * Creates a match.
+     * @param startA is the starting index in the submission A.
+     * @param startB is the starting index in the submission B.
+     * @param length is the length of these similar sections.
+     */
     public Match(int startA, int startB, int length) {
         this.startA = startA;
         this.startB = startB;
         this.length = length;
     }
 
-    public void set(int startA, int startB, int length) {
-        this.startA = startA;
-        this.startB = startB;
-        this.length = length;
+    /**
+     * @return the starting index in the submission A.
+     */
+    public int getStartA() {
+        return startA;
+    }
+
+    /**
+     * @return the starting index in the submission B.
+     */
+    public int getStartB() {
+        return startB;
+    }
+
+    /**
+     * @return the length of the similar sections.
+     */
+    public int getLength() {
+        return length;
     }
 
+    /**
+     * Checks if two matches overlap.
+     * @return true if they do.
+     */
     public final boolean overlap(int oStartA, int oStartB, int oLength) {
         if (startA < oStartA) {
             if ((oStartA - startA) < length) {

+ 15 - 15
jplag/src/main/java/jplag/reporting/Report.java

@@ -144,10 +144,10 @@ public class Report {
         for (int i = 0; i < comparison.matches.size(); i++) {
             match = comparison.matches.get(i);
 
-            Token startA = tokensA.getToken(match.startA);
-            Token endA = tokensA.getToken(match.startA + match.length - 1);
-            Token startB = tokensB.getToken(match.startB);
-            Token endB = tokensB.getToken(match.startB + match.length - 1);
+            Token startA = tokensA.getToken(match.getStartA());
+            Token endA = tokensA.getToken(match.getStartA() + match.getLength() - 1);
+            Token startB = tokensB.getToken(match.getStartB());
+            Token endB = tokensB.getToken(match.getStartB() + match.getLength() - 1);
 
             String col = Color.getHexadecimalValue(i);
 
@@ -172,7 +172,7 @@ public class Report {
                 htmlFile.print("(" + startB.getLine() + "-" + endB.getLine());
             }
 
-            htmlFile.println(")</A><TD ALIGN=center>" + "<FONT COLOR=\"" + comparison.color(match.length) + "\">" + match.length + "</FONT>");
+            htmlFile.println(")</A><TD ALIGN=center>" + "<FONT COLOR=\"" + comparison.color(match.getLength()) + "\">" + match.getLength() + "</FONT>");
         }
 
         if (result.getOptions().hasBaseCode()) {
@@ -280,8 +280,8 @@ public class Report {
         for (int x = 0; x < comparison.matches.size(); x++) {
             Match onematch = comparison.matches.get(x);
 
-            Token start = tokens.getToken(j == 0 ? onematch.startA : onematch.startB);
-            Token end = tokens.getToken((j == 0 ? onematch.startA : onematch.startB) + onematch.length - 1);
+            Token start = tokens.getToken(j == 0 ? onematch.getStartA() : onematch.getStartB());
+            Token end = tokens.getToken((j == 0 ? onematch.getStartA() : onematch.getStartB()) + onematch.getLength() - 1);
             for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
                 if (start.file.equals(files[fileIndex]) && text[fileIndex] != null) {
                     String tmp = "<FONT color=\"" + Color.getHexadecimalValue(x) + "\">" + (j == 1 ? "<div style=\"position:absolute;left:0\">" : "")
@@ -305,8 +305,8 @@ public class Report {
 
             for (int x = 0; x < baseCodeComparison.matches.size(); x++) {
                 Match onematch = baseCodeComparison.matches.get(x);
-                Token start = tokens.getToken(onematch.startA);
-                Token end = tokens.getToken(onematch.startA + onematch.length - 1);
+                Token start = tokens.getToken(onematch.getStartA());
+                Token end = tokens.getToken(onematch.getStartA() + onematch.getLength() - 1);
 
                 for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
                     if (start.file.equals(files[fileIndex]) && text[fileIndex] != null) {
@@ -504,8 +504,8 @@ public class Report {
                 if (onematch == null) {
                     if (index < comparison.matches.size()) {
                         onematch = comparison.matches.get(perm[index]);
-                        start = tokens.getToken(j == 0 ? onematch.startA : onematch.startB);
-                        end = tokens.getToken((j == 0 ? onematch.startA : onematch.startB) + onematch.length - 1);
+                        start = tokens.getToken(j == 0 ? onematch.getStartA() : onematch.getStartB());
+                        end = tokens.getToken((j == 0 ? onematch.getStartA() : onematch.getStartB()) + onematch.getLength() - 1);
                         index++;
                     } else {
                         start = end = null;
@@ -690,8 +690,8 @@ public class Report {
         for (int x = 0; x < comparison.matches.size(); x++) {
             currentMatch = comparison.matches.get(x);
 
-            Token start = tokens.getToken(j == 0 ? currentMatch.startA : currentMatch.startB);
-            Token ende = tokens.getToken((j == 0 ? currentMatch.startA : currentMatch.startB) + currentMatch.length - 1);
+            Token start = tokens.getToken(j == 0 ? currentMatch.getStartA() : currentMatch.getStartB());
+            Token ende = tokens.getToken((j == 0 ? currentMatch.getStartA() : currentMatch.getStartB()) + currentMatch.getLength() - 1);
 
             for (int y = 0; y < files.length; y++) {
                 if (start.file.equals(files[y]) && text[y] != null) {
@@ -724,8 +724,8 @@ public class Report {
 
             for (int x = 0; x < baseCodeComparison.matches.size(); x++) {
                 currentMatch = baseCodeComparison.matches.get(x);
-                Token start = tokens.getToken(currentMatch.startA);
-                Token ende = tokens.getToken(currentMatch.startA + currentMatch.length - 1);
+                Token start = tokens.getToken(currentMatch.getStartA());
+                Token ende = tokens.getToken(currentMatch.getStartA() + currentMatch.getLength() - 1);
 
                 for (int y = 0; y < files.length; y++) {
                     if (start.file.equals(files[y]) && text[y] != null) {

+ 3 - 3
jplag/src/main/java/jplag/strategy/AbstractComparisonStrategy.java

@@ -14,9 +14,9 @@ public abstract class AbstractComparisonStrategy implements ComparisonStrategy {
 
     // TODO PB: I think it's better to make each submission store its own matches with the base code.
     // Hashtable that maps the name of a submissions to its matches with the provided base code.
-    protected Hashtable<String, JPlagComparison> baseCodeMatches = new Hashtable<>(30);
+    private Hashtable<String, JPlagComparison> baseCodeMatches = new Hashtable<>(30);
 
-    protected GreedyStringTiling greedyStringTiling;
+    private GreedyStringTiling greedyStringTiling;
 
     protected JPlagOptions options;
 
@@ -49,7 +49,7 @@ public abstract class AbstractComparisonStrategy implements ComparisonStrategy {
         return Optional.empty();
     }
 
-    protected boolean isAboveSimilarityThreshold(JPlagComparison comparison) {
+    private boolean isAboveSimilarityThreshold(JPlagComparison comparison) {
         float similarityThreshold = this.options.getSimilarityThreshold();
         SimilarityMetric similarityMetric = this.options.getSimilarityMetric();
 

+ 1 - 1
jplag/src/test/java/jplag/NewJavaFeaturesTest.java

@@ -21,7 +21,7 @@ public class NewJavaFeaturesTest extends TestBase {
         // Five matches that each start at the same position:
         assertEquals(4, comparison.matches.size());
         for (Match match : comparison.matches) {
-            assertEquals(match.startA, match.startB);
+            assertEquals(match.getStartA(), match.getStartB());
         }
     }