Skip to content

Commit

Permalink
Merge pull request #42904 from Shadow-Devil/use-stream-tolist-v2
Browse files Browse the repository at this point in the history
[Refactoring] Replace `collect(Collectors.toList())` with `toList()`
  • Loading branch information
gimantha authored Sep 20, 2024
2 parents 383e482 + 02c4210 commit 78ff304
Show file tree
Hide file tree
Showing 200 changed files with 421 additions and 582 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ private Collection<GlobalVariableSymbol> globalVariableSymbols(Project project,
return compilation.getSemanticModel(moduleId).moduleSymbols().stream()
.filter(s -> s instanceof VariableSymbol || s instanceof FunctionSymbol)
.map(GlobalVariableSymbol::fromSymbol)
.collect(Collectors.toList());
.toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Imports that were stored to be able to search with the prefix.
Expand Down Expand Up @@ -364,7 +363,7 @@ private static List<String> getBallerinaKeywords() {
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
.toList();
} catch (ClassNotFoundException e) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.ballerina.shell.utils.QuotedImport;

import java.util.List;
import java.util.stream.Collectors;

/**
* Snippet that represent a import statement.
Expand Down Expand Up @@ -59,7 +58,7 @@ public Identifier getPrefix() {
*/
public QuotedImport getImportedModule() {
List<String> moduleNames = rootNode.moduleName().stream().map(IdentifierToken::text)
.collect(Collectors.toList());
.toList();
if (rootNode.orgName().isPresent()) {
String orgName = rootNode.orgName().get().orgName().text();
return new QuotedImport(orgName, moduleNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Support class to add required import statements.
Expand Down Expand Up @@ -113,7 +112,7 @@ public List<String> undefinedModules(Collection<Diagnostic> diagnostics) {
}
}

return moduleErrors.stream().distinct().collect(Collectors.toList());
return moduleErrors.stream().distinct().toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
* An import that has quoted orgname and module names.
Expand All @@ -36,7 +35,7 @@ public QuotedImport(String orgName, List<String> moduleNames) {
this.orgName = orgName;
this.moduleNames = moduleNames.stream()
.map(Identifier::new)
.collect(Collectors.toList());
.toList();
}

public QuotedImport(List<String> moduleNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Misc functionality testing of evaluator.
Expand Down Expand Up @@ -169,6 +168,6 @@ private List<String> filterAvailableVariables(Evaluator evaluator) {
return evaluator.availableVariables().stream().filter(s -> !s.equals("Variable declarations")
&& !s.equals("Final variable declarations")
&& !s.equals("Constant Variable Declarations"))
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.concurrent.TimeUnit;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand Down Expand Up @@ -236,7 +235,7 @@ private void modifyJar() throws InterruptedException, IOException {
final File userDirectory = new File(System.getProperty("user.dir")); // Get the user directory
listAllFiles(userDirectory); // List all files in the user directory and its subdirectories
// Get a list of the directories containing instrumented files
List<String> changedDirectories = instrumentedFiles.stream().distinct().collect(Collectors.toList());
List<String> changedDirectories = instrumentedFiles.stream().distinct().toList();
loadDirectories(changedDirectories);
} finally {
for (String instrumentedFilePath : instrumentedPaths) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,12 +883,12 @@ public static List<String> getTemplates() {
.map(directory -> directory.getFileName())
.filter(Objects::nonNull)
.map(Path::toString)
.collect(Collectors.toList());
.toList();

if (null != jarFs) {
return templates.stream().map(t -> t
.replace(jarFs.getSeparator(), ""))
.collect(Collectors.toList());
.toList();
} else {
return templates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.ballerina.cli.cmd.Constants.ADD_COMMAND;
Expand Down Expand Up @@ -189,7 +188,7 @@ private static List<File> getToolCommandJarAndDependencyJars(String commandName)
.resolve(TOOL).resolve(LIBS)
.toFile()))
.flatMap(List::stream)
.collect(Collectors.toList());
.toList();
}

Optional<BalToolsManifest.Tool> toolOpt = balToolsManifest.getActiveTool(commandName);
Expand Down Expand Up @@ -280,7 +279,7 @@ public static void updateOldBalToolsToml() {
} catch (ProjectException ignore) {
return false;
}
})).map(File::getName).collect(Collectors.toList());
})).map(File::getName).toList();

Optional<String> latestVersion = getLatestVersion(versions);
versions.stream().forEach(version -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,6 @@ private static List<File> getToolCommandJarAndDependencyJars(List<BuildTool> res
.resolve(TOOL).resolve(LIBS)
.toFile()))
.flatMap(List::stream)
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException;
Expand Down Expand Up @@ -362,7 +361,7 @@ private List<Path> getAllSourceFilePaths(String projectRootString) throws IOExce
private static List<Path> filterPathStream(Stream<Path> pathStream, String combinedPattern) {
return pathStream.filter(
FileSystems.getDefault().getPathMatcher("glob:" + combinedPattern)::matches)
.collect(Collectors.toList());
.toList();
}

private void getclassFromSourceFilePath(List<String> sourcePatternList, Package currentPackage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import java.util.Map;
import java.util.StringJoiner;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand Down Expand Up @@ -603,9 +602,8 @@ public static String getClassPath(Map<String, TestSuite> testSuiteMap) {
dependencies.addAll(testSuiteEntry.getValue().getTestExecutionDependencies());

}
dependencies = dependencies.stream().distinct().collect(Collectors.toList());
dependencies = dependencies.stream().map((x) -> convertWinPathToUnixFormat(addQuotationMarkToString(x)))
.collect(Collectors.toList());
dependencies = dependencies.stream().distinct()
.map((x) -> convertWinPathToUnixFormat(addQuotationMarkToString(x))).toList();

StringJoiner classPath = new StringJoiner(File.pathSeparator);
dependencies.forEach(classPath::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException;
import static org.ballerinalang.test.runtime.util.TesterinaConstants.COVERAGE_DIR;
Expand Down Expand Up @@ -482,11 +482,10 @@ private static String getFunctionToMockClassName(String id) {
*/
public static String getClassPath(JBallerinaBackend jBallerinaBackend, Package currentPackage) {
JarResolver jarResolver = jBallerinaBackend.jarResolver();
Set<Path> jars = new HashSet<>(getModuleJarPaths(jBallerinaBackend, currentPackage));

List<Path> dependencies = getTestDependencyPaths(currentPackage, jarResolver);

List<Path> jarList = getModuleJarPaths(jBallerinaBackend, currentPackage);
dependencies.removeAll(jarList);
List<Path> dependencies = getTestDependencyPaths(currentPackage, jarResolver)
.stream().filter(dependency -> !jars.contains(dependency)).toList();

StringJoiner classPath = joinClassPaths(dependencies);
return classPath.toString();
Expand Down Expand Up @@ -522,7 +521,7 @@ public static List<Path> getTestDependencyPaths(Package currentPackage, JarResol
}
}
}
return dependencies.stream().distinct().collect(Collectors.toList());
return dependencies.stream().distinct().toList();
}

/**
Expand All @@ -549,7 +548,7 @@ public static List<Path> getModuleJarPaths(JBallerinaBackend jBallerinaBackend,
}
}

return moduleJarPaths.stream().distinct().collect(Collectors.toList());
return moduleJarPaths.stream().distinct().toList();
}

private static PlatformLibrary getCodeGeneratedTestLibrary(JBallerinaBackend jBallerinaBackend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Provides the set of Ballerina Reserved Keywords to be used at the symbol factory.
Expand Down Expand Up @@ -55,7 +54,7 @@ private static List<String> getBallerinaKeywords() {
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
.toList();
} catch (ClassNotFoundException e) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static io.ballerina.compiler.api.impl.PositionUtil.isPosWithinOpenCloseLineRanges;
import static io.ballerina.compiler.api.impl.PositionUtil.isPosWithinRange;
Expand Down Expand Up @@ -1062,7 +1061,7 @@ private List<Symbol> getModuleSymbols(QualifiedNameReferenceNode qNameRef,
Optional<ModuleSymbol> module = searchModuleForAlias(alias);
return module.map(moduleSymbol -> moduleSymbol.allSymbols().stream()
.filter(predicate)
.collect(Collectors.toList()))
.toList())
.orElseGet(ArrayList::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL;

Expand Down Expand Up @@ -265,10 +264,10 @@ public void visit(BLangPackage pkgNode) {
find(pkgNode.typeDefinitions);
find(pkgNode.classDefinitions.stream()
.filter(c -> !isGeneratedClassDefForService(c))
.collect(Collectors.toList()));
.toList());
find(pkgNode.functions.stream()
.filter(f -> !f.flagSet.contains(Flag.LAMBDA))
.collect(Collectors.toList()));
.toList());

if (!(pkgNode instanceof BLangTestablePackage)) {
find(pkgNode.getTestablePkg());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Responsible for loading and maintaining engaged compiler plugins.
Expand Down Expand Up @@ -171,7 +170,7 @@ private static CompilerPluginInfo loadCompilerPlugin(CompilerPluginDescriptor pl
List<Path> jarLibraryPaths = pluginDescriptor.getCompilerPluginDependencies()
.stream()
.map(Paths::get)
.collect(Collectors.toList());
.toList();

CompilerPlugin compilerPlugin;
try {
Expand All @@ -192,7 +191,7 @@ private static List<Package> getDirectDependencies(ResolvedPackageDependency roo
return dependencyGraph.getDirectDependencies(rootPkgNode)
.stream()
.map(ResolvedPackageDependency::packageInstance)
.collect(Collectors.toList());
.toList();
}

private static List<CompilerPluginContextIml> initializePlugins(List<CompilerPluginInfo> compilerPlugins,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Manages interaction with completion providers via compiler plugins.
Expand Down Expand Up @@ -161,7 +160,7 @@ private List<ModuleSymbol> getModulesOfActiveListeners(CompletionContext context
}
return Optional.of(listenerType);
}).filter(listenerType -> listenerType.isPresent() && listenerType.get().getModule().isPresent())
.map(listenerType -> listenerType.get().getModule().get()).collect(Collectors.toList());
.map(listenerType -> listenerType.get().getModule().get()).toList();
}

private TypeSymbol getRawType(TypeSymbol typeDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Util class to read configurable variables defined in a package.
Expand Down Expand Up @@ -303,16 +302,13 @@ private static Node getVariableNode(int position, Map<Document, SyntaxTree> synt
for (Map.Entry<Document, SyntaxTree> syntaxTreeEntry : syntaxTreeMap.entrySet()) {
if (syntaxTreeEntry.getValue().containsModulePart()) {
ModulePartNode modulePartNode = syntaxTreeMap.get(syntaxTreeEntry.getKey()).rootNode();
List<Node> filteredVarNodes = modulePartNode.members().stream()
.filter(node -> node.kind() == SyntaxKind.MODULE_VAR_DECL &&
node instanceof ModuleVariableDeclarationNode)
.collect(Collectors.toList());
for (Node node : filteredVarNodes) {
if (node.location().lineRange().startLine().line() <= position &&
node.location().lineRange().endLine().line() >= position) {
return node;
}
}
return modulePartNode.members().stream()
.filter(node -> node.kind() == SyntaxKind.MODULE_VAR_DECL &&
node instanceof ModuleVariableDeclarationNode &&
node.location().lineRange().startLine().line() <= position &&
node.location().lineRange().endLine().line() >= position)
.findFirst()
.orElse(null);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.Collection;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* This class contains various static methods that operate on {@code Diagnostic} instances.
Expand Down Expand Up @@ -72,7 +71,7 @@ private static Collection<Diagnostic> filterDiagnostics(Collection<Diagnostic> d
Predicate<Diagnostic> predicate) {
return diagnostics.stream()
.filter(predicate)
.collect(Collectors.toList());
.toList();
}

private static boolean hasDiagnosticsWithSeverity(Collection<Diagnostic> diagnostics,
Expand Down
Loading

0 comments on commit 78ff304

Please sign in to comment.