Skip to content

Commit

Permalink
Use Stream#toList() instead of collect() (#311)
Browse files Browse the repository at this point in the history
toList was added in Java 16, and some streams can perform a more optimized operation compared to Collectors.toList().
  • Loading branch information
MariusVolkhart committed Jan 8, 2024
1 parent 0c83398 commit 08c5015
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public Collection<Path> generate(JteConfig config, Set<TemplateDescription> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 -> {
Expand Down
6 changes: 3 additions & 3 deletions jte/src/main/java/gg/jte/compiler/TemplateCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void cleanAll() {
@Override
public List<String> generateAll() {
LinkedHashSet<ClassDefinition> classDefinitions = generate(codeResolver.resolveAllTemplateNames(), false);
return classDefinitions.stream().map(ClassDefinition::getSourceFileName).collect(Collectors.toList());
return classDefinitions.stream().map(ClassDefinition::getSourceFileName).toList();
}

@Override
Expand Down Expand Up @@ -127,7 +127,7 @@ private List<String> precompileClasses(LinkedHashSet<ClassDefinition> 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<String> getClassPath() {
Expand Down Expand Up @@ -205,7 +205,7 @@ private LinkedHashSet<ClassDefinition> generate(List<String> names, boolean trac

List<JteExtension> 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);
}
Expand Down
3 changes: 1 addition & 2 deletions jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -66,7 +65,7 @@ public List<String> 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);
}
Expand Down
3 changes: 1 addition & 2 deletions jte/src/test/java/gg/jte/migrate/MigrateV1To2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,7 +26,7 @@ void name(@TempDir Path tempDir) throws IOException {
}

private void copyTemplatesTo(Path tempDir) throws IOException {
List<Path> templates = Files.walk(sourceDirectory).filter(p -> p != sourceDirectory).collect(Collectors.toList());
List<Path> templates = Files.walk(sourceDirectory).filter(p -> p != sourceDirectory).toList();
for (Path template : templates) {
Files.copy(template, tempDir.resolve(template.getFileName()));
}
Expand Down

0 comments on commit 08c5015

Please sign in to comment.