From 2bee4d73af1c1953c62e20006bf39d468fa74a46 Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Sun, 7 Jan 2024 10:38:56 -0500 Subject: [PATCH] Use Stream#toList() instead of collect() toList was added in Java 16, and some streams can perform a more optimized operation compared to Collectors.toList(). --- .../main/java/gg/jte/models/generator/ModelExtension.java | 2 +- .../java/gg/jte/models/generator/TestModelExtension.java | 3 +-- jte/src/main/java/gg/jte/compiler/TemplateCompiler.java | 6 +++--- jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java | 3 +-- jte/src/test/java/gg/jte/migrate/MigrateV1To2Test.java | 3 +-- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java b/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java index 7ce46dcb..60310adb 100644 --- a/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java +++ b/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java @@ -48,6 +48,6 @@ public Collection generate(JteConfig config, Set temp new ModelGenerator(engine, "statictemplates", "StaticTemplates", "Templates", language), new ModelGenerator(engine, "dynamictemplates", "DynamicTemplates", "Templates", language) ).map(g -> g.generate(config, templateDescriptionsFiltered, modelConfig)) - .collect(Collectors.toList()); + .toList(); } } diff --git a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java index 0a2ab768..425884ce 100644 --- a/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java +++ b/jte-models/src/test/java/gg/jte/models/generator/TestModelExtension.java @@ -19,7 +19,6 @@ import java.util.Map; import java.util.Optional; import java.util.regex.Pattern; -import java.util.stream.Collectors; import static gg.jte.extension.api.mocks.MockConfig.mockConfig; import static gg.jte.extension.api.mocks.MockParamDescription.mockParamDescription; @@ -188,7 +187,7 @@ public void generatesKotlinFacadesWhenParamHasDefaultValue() { DYNAMIC_SOURCE_FILE.matcher(path.toString()).find() || STATIC_SOURCE_FILE.matcher(path.toString()).find() ) - .collect(Collectors.toList()); + .toList(); assertThat(implementationsPaths).isNotEmpty(); implementationsPaths.forEach(path -> { diff --git a/jte/src/main/java/gg/jte/compiler/TemplateCompiler.java b/jte/src/main/java/gg/jte/compiler/TemplateCompiler.java index c3fc45c2..32ee157e 100644 --- a/jte/src/main/java/gg/jte/compiler/TemplateCompiler.java +++ b/jte/src/main/java/gg/jte/compiler/TemplateCompiler.java @@ -81,7 +81,7 @@ public void cleanAll() { @Override public List generateAll() { LinkedHashSet classDefinitions = generate(codeResolver.resolveAllTemplateNames(), false); - return classDefinitions.stream().map(ClassDefinition::getSourceFileName).collect(Collectors.toList()); + return classDefinitions.stream().map(ClassDefinition::getSourceFileName).toList(); } @Override @@ -127,7 +127,7 @@ private List precompileClasses(LinkedHashSet classDefin javaCompiler.compile(javaFiles, javaCompilerClassPath, config, classDirectory, templateByClassName); } - return classDefinitions.stream().map(ClassDefinition::getSourceFileName).collect(Collectors.toList()); + return classDefinitions.stream().map(ClassDefinition::getSourceFileName).toList(); } private List getClassPath() { @@ -205,7 +205,7 @@ private LinkedHashSet generate(List names, boolean trac List generatorExtensions = config.extensionClasses.entrySet().stream() .map(this::loadExtension) - .collect(Collectors.toList()); + .toList(); if (DEBUG) { System.out.printf("extensionClasses=%s generatorExtensions=%s%n", config.extensionClasses, generatorExtensions); } diff --git a/jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java b/jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java index 1648dc36..cf7db577 100644 --- a/jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java +++ b/jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java @@ -9,7 +9,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -66,7 +65,7 @@ public List resolveAllTemplateNames() { .filter(p -> !Files.isDirectory(p)) .map(p -> root.relativize(p).toString().replace('\\', '/')) .filter(IoUtils::isTemplateFile) - .collect(Collectors.toList()); + .toList(); } catch (IOException e) { throw new UncheckedIOException("Failed to resolve all templates in " + root, e); } diff --git a/jte/src/test/java/gg/jte/migrate/MigrateV1To2Test.java b/jte/src/test/java/gg/jte/migrate/MigrateV1To2Test.java index 9e586310..a011ab4a 100644 --- a/jte/src/test/java/gg/jte/migrate/MigrateV1To2Test.java +++ b/jte/src/test/java/gg/jte/migrate/MigrateV1To2Test.java @@ -8,7 +8,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; -import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; @@ -27,7 +26,7 @@ void name(@TempDir Path tempDir) throws IOException { } private void copyTemplatesTo(Path tempDir) throws IOException { - List templates = Files.walk(sourceDirectory).filter(p -> p != sourceDirectory).collect(Collectors.toList()); + List templates = Files.walk(sourceDirectory).filter(p -> p != sourceDirectory).toList(); for (Path template : templates) { Files.copy(template, tempDir.resolve(template.getFileName())); }