Преглед изворни кода

Use TokenList::stream where applicable, fix code smells

Robin Maisch пре 4 година
родитељ
комит
a8b089daee

+ 3 - 4
jplag.frontend-testutils/src/test/java/de/jplag/testutils/TokenUtils.java

@@ -1,7 +1,6 @@
 package de.jplag.testutils;
 
 import java.util.List;
-import java.util.stream.StreamSupport;
 
 import de.jplag.Token;
 import de.jplag.TokenList;
@@ -14,7 +13,7 @@ public final class TokenUtils {
 
     /**
      * Returns the type of all tokens in a {@link TokenList} that belong to a file.
-     * @param tokens is the {@link TokenList}.
+     * @param tokenList is the {@link TokenList}.
      * @param name is the name of the target file.
      * @return the immutable list of token types.
      */
@@ -25,12 +24,12 @@ public final class TokenUtils {
 
     /**
      * Returns the tokens in a {@link TokenList} that belong to a file.
-     * @param tokens is the {@link TokenList}.
+     * @param tokenList is the {@link TokenList}.
      * @param name is the name of the target file.
      * @return the immutable list of tokens.
      */
     public static List<Token> tokensByFile(TokenList tokenList, String name) {
-        var tokens = StreamSupport.stream(tokenList.allTokens().spliterator(), false);
+        var tokens = tokenList.stream();
         return tokens.filter(it -> it.getFile().startsWith(name)).toList();
     }
 

+ 1 - 3
jplag.frontend-utils/src/main/java/de/jplag/TokenList.java

@@ -1,7 +1,5 @@
 package de.jplag;
 
-import static java.util.stream.Collectors.toList;
-
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Stream;
@@ -78,7 +76,7 @@ public class TokenList {
     @Override
     public final String toString() {
         try {
-            List<String> tokenStrings = tokens.stream().map(Token::toString).collect(toList());
+            List<String> tokenStrings = tokens.stream().map(Token::toString).toList();
             return String.join(System.lineSeparator(), tokenStrings);
         } catch (OutOfMemoryError exception) {
             return "Token list to large for output: " + tokens.size() + " Tokens";

+ 15 - 17
jplag.frontend-utils/src/main/java/de/jplag/TokenPrinter.java

@@ -190,9 +190,7 @@ public final class TokenPrinter {
      * @return the shortened file name
      */
     private static String restoreTokenFileName(String fileName, Optional<String> suffix) {
-        if (suffix.isEmpty()) {
-            return fileName;
-        } else if (!fileName.endsWith(suffix.get())) {
+        if (suffix.isEmpty() || !fileName.endsWith(suffix.get())) {
             return fileName;
         }
 
@@ -214,18 +212,6 @@ public final class TokenPrinter {
         return Collections.emptyList();
     }
 
-    /**
-     * Returns the number of digits (including a minus) of the given number.
-     */
-    private static int digitCount(int number) {
-        if (number == 0) {
-            return 1;
-        }
-        int minusLength = number < 0 ? 1 : 0;
-        // The 'log10' variant is supposedly faster than the 'toString' variant.
-        return (int) Math.log10(Math.abs(number)) + minusLength + 1;
-    }
-
     /**
      * This contains all data concerning a line of code in a file and the tokens found in that line.
      * @param file the file
@@ -276,6 +262,18 @@ public final class TokenPrinter {
         private int lineNumber;
         private int trailingLineSeparators = 0;
 
+        /**
+         * Returns the number of digits (including a minus) of the given number.
+         */
+        private static int digitCount(int number) {
+            if (number == 0) {
+                return 1;
+            }
+            int minusLength = number < 0 ? 1 : 0;
+            // The 'log10' variant is supposedly faster than the 'toString' variant.
+            return (int) Math.log10(Math.abs(number)) + minusLength + 1;
+        }
+
         private void resetLinePosition() {
             columnIndex = 1;
         }
@@ -360,8 +358,8 @@ public final class TokenPrinter {
             }
 
             // The replacement operation preserves TAB characters, which is essential for correct alignment
-            String padding = currentLine.substring(columnIndex - 1, targetPosition - 1).replaceAll(TAB, REPLACE_TABS ? TAB_REPLACEMENT : TAB)
-                    .replaceAll(NON_WHITESPACE, SPACE);
+            String padding = currentLine.substring(columnIndex - 1, targetPosition - 1).replace(TAB, REPLACE_TABS ? TAB_REPLACEMENT : TAB)
+                    .replace(NON_WHITESPACE, SPACE);
             append(padding);
 
             return this;

+ 1 - 2
jplag.frontend.csharp-6/src/test/java/de/jplag/csharp/MinimalCSharpFrontendTest.java

@@ -9,7 +9,6 @@ import java.io.File;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.List;
-import java.util.stream.StreamSupport;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -51,7 +50,7 @@ class MinimalCSharpFrontendTest {
 
         // Compare parsed tokens:
         assertEquals(expectedToken.size(), result.size());
-        List<Integer> actualToken = StreamSupport.stream(result.allTokens().spliterator(), false).map(Token::getType).collect(toList());
+        List<Integer> actualToken = result.stream().map(Token::getType).collect(toList());
         assertEquals(expectedToken, actualToken);
     }
 

+ 1 - 2
jplag.frontend.golang/src/test/java/de/jplag/golang/GoFrontendTest.java

@@ -13,7 +13,6 @@ import java.util.Objects;
 import java.util.OptionalInt;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
-import java.util.stream.StreamSupport;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -158,7 +157,7 @@ class GoFrontendTest {
      * @param fileName The file name of the complete code example
      */
     private void testTokenCoverage(TokenList tokens, String fileName) {
-        var foundTokens = StreamSupport.stream(tokens.allTokens().spliterator(), true).mapToInt(Token::getType).sorted().distinct().boxed().toList();
+        var foundTokens = tokens.stream().parallel().mapToInt(Token::getType).sorted().distinct().boxed().toList();
 
         // Exclude SEPARATOR_TOKEN, as it does not occur
         var missingTokenTypes = IntStream.range(0, GoTokenConstants.NUM_DIFF_TOKENS).filter(i -> i != TokenConstants.SEPARATOR_TOKEN).boxed()

+ 1 - 2
jplag.frontend.kotlin/src/test/java/de/jplag/kotlin/KotlinFrontendTest.java

@@ -10,7 +10,6 @@ import java.nio.file.Path;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
-import java.util.stream.StreamSupport;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -154,7 +153,7 @@ class KotlinFrontendTest {
      * @param fileName The file name of the complete code example
      */
     private void testTokenCoverage(TokenList tokens, String fileName) {
-        var foundTokens = StreamSupport.stream(tokens.allTokens().spliterator(), true).mapToInt(Token::getType).sorted().distinct().toArray();
+        var foundTokens = tokens.stream().parallel().mapToInt(Token::getType).sorted().distinct().toArray();
         // Exclude SEPARATOR_TOKEN, as it does not occur
         var allTokens = IntStream.range(0, KotlinTokenConstants.NUMBER_DIFF_TOKENS).filter(i -> i != TokenConstants.SEPARATOR_TOKEN).toArray();
 

+ 3 - 3
jplag.frontend.rlang/src/test/java/de/jplag/rlang/RFrontendTest.java

@@ -3,12 +3,12 @@ package de.jplag.rlang;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.fail;
 
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.List;
 import java.util.stream.IntStream;
-import java.util.stream.StreamSupport;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -92,7 +92,7 @@ class RFrontendTest {
      * @param fileName The file name of the complete code example
      */
     private void testTokenCoverage(TokenList tokens, String fileName) {
-        var foundTokens = StreamSupport.stream(tokens.allTokens().spliterator(), true).mapToInt(Token::getType).sorted().distinct().toArray();
+        var foundTokens = tokens.stream().parallel().mapToInt(Token::getType).sorted().distinct().toArray();
         // Exclude SEPARATOR_TOKEN, as it does not occur
         var allTokens = IntStream.range(0, RTokenConstants.NUM_DIFF_TOKENS).filter(i -> i != TokenConstants.SEPARATOR_TOKEN).toArray();
 

+ 1 - 2
jplag.frontend.scala/src/test/java/de/jplag/scala/ScalaFrontendTest.java

@@ -10,7 +10,6 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
-import java.util.stream.StreamSupport;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -162,7 +161,7 @@ class ScalaFrontendTest {
      * @param fileName The file name of the complete code example
      */
     private void testTokenCoverage(TokenList tokens, String fileName) {
-        var foundTokens = StreamSupport.stream(tokens.allTokens().spliterator(), true).mapToInt(Token::getType).distinct().boxed().toList();
+        var foundTokens = tokens.stream().parallel().mapToInt(Token::getType).distinct().boxed().toList();
         var allTokens = IntStream.range(0, ScalaTokenConstants.numberOfTokens()).boxed().toList();
         allTokens = new ArrayList<>(allTokens);
 

+ 1 - 1
jplag/src/main/java/de/jplag/SubmissionSet.java

@@ -178,7 +178,7 @@ public class SubmissionSet {
         }
 
         long duration = System.currentTimeMillis() - startTime;
-        String timePerSubmission = submissions.size() > 0 ? Long.toString(duration / submissions.size()) : "n/a";
+        String timePerSubmission = !submissions.isEmpty() ? Long.toString(duration / submissions.size()) : "n/a";
         logger.trace("Total time for parsing: " + TimeUtil.formatDuration(duration));
         logger.trace("Time per parsed submission: " + timePerSubmission + " msec");
     }

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

@@ -4,7 +4,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.function.Function;
-import java.util.stream.Collectors;
 
 import org.apache.commons.math3.linear.Array2DRowRealMatrix;
 import org.apache.commons.math3.linear.RealMatrix;
@@ -59,9 +58,9 @@ public class ClusteringAdapter {
         Collection<Collection<Integer>> intResult = algorithm.cluster(similarityMatrix);
         ClusteringResult<Integer> modularityClusterResult = ClusteringResult.fromIntegerCollections(new ArrayList<>(intResult), similarityMatrix);
         List<Cluster<Submission>> mappedClusters = modularityClusterResult.getClusters().stream()
-                .map(unmappedCluster -> new Cluster<>(unmappedCluster.getMembers().stream().map(mapping::unmap).collect(Collectors.toList()),
+                .map(unmappedCluster -> new Cluster<>(unmappedCluster.getMembers().stream().map(mapping::unmap).toList(),
                         unmappedCluster.getCommunityStrength(), unmappedCluster.getAverageSimilarity()))
-                .collect(Collectors.toList());
+                .toList();
         return new ClusteringResult<>(mappedClusters, modularityClusterResult.getCommunityStrength());
     }
 

+ 3 - 6
jplag/src/main/java/de/jplag/clustering/IntegerMapping.java

@@ -24,14 +24,11 @@ public class IntegerMapping<T> {
      * @return the associated integer
      */
     public int map(T value) {
-        Integer result = mapping.get(value);
-        if (result == null) {
+        return mapping.computeIfAbsent(value, val -> {
             int newIndex = size++;
-            mapping.put(value, newIndex);
-            backMapping.add(value);
+            backMapping.add(val);
             return newIndex;
-        }
-        return result;
+        });
     }
 
     /**

+ 1 - 3
jplag/src/main/java/de/jplag/options/LanguageOption.java

@@ -1,7 +1,5 @@
 package de.jplag.options;
 
-import static java.util.stream.Collectors.toList;
-
 import java.util.Arrays;
 import java.util.Collection;
 
@@ -45,7 +43,7 @@ public enum LanguageOption {
     }
 
     public static Collection<String> getAllDisplayNames() {
-        return Arrays.stream(LanguageOption.values()).map(languageOption -> languageOption.displayName).collect(toList());
+        return Arrays.stream(LanguageOption.values()).map(languageOption -> languageOption.displayName).toList();
     }
 
     public static LanguageOption getDefault() {