From b1ac64c7e2f585675273d09fd53db56ff015e465 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 24 Jul 2023 16:03:19 +0800 Subject: [PATCH] Harmonize Stream.collect() usage use Stream.toList() or collect(Collectors.toSet()) where possible. See gh-36509 --- .../bom/bomr/StandardLibraryUpdateResolver.java | 13 +++---------- .../sonatype/SonatypeServiceTests.java | 5 ++--- .../org/springframework/boot/util/Instantiator.java | 6 +----- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/StandardLibraryUpdateResolver.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/StandardLibraryUpdateResolver.java index bcfb1406c5..62c8aa9e86 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/StandardLibraryUpdateResolver.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/StandardLibraryUpdateResolver.java @@ -19,12 +19,10 @@ package org.springframework.boot.build.bom.bomr; import java.time.Duration; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.SortedSet; -import java.util.stream.Collectors; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.slf4j.Logger; @@ -98,19 +96,14 @@ class StandardLibraryUpdateResolver implements LibraryUpdateResolver { getLaterVersionsForModule(group.getId(), plugin, libraryVersion)); } } - List allVersions = moduleVersions.values() + return moduleVersions.values() .stream() .flatMap(SortedSet::stream) .distinct() .filter((dependencyVersion) -> isPermitted(dependencyVersion, library.getProhibitedVersions())) - .toList(); - if (allVersions.isEmpty()) { - return Collections.emptyList(); - } - return allVersions.stream() - .map((version) -> new VersionOption.ResolvedVersionOption(version, + .map((version) -> (VersionOption) new VersionOption.ResolvedVersionOption(version, getMissingModules(moduleVersions, version))) - .collect(Collectors.toList()); + .toList(); } private boolean isPermitted(DependencyVersion dependencyVersion, List prohibitedVersions) { diff --git a/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/sonatype/SonatypeServiceTests.java b/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/sonatype/SonatypeServiceTests.java index 6c9133c7c6..ef949ac8b0 100644 --- a/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/sonatype/SonatypeServiceTests.java +++ b/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/sonatype/SonatypeServiceTests.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; -import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.stream.Collectors; @@ -105,7 +104,7 @@ class SonatypeServiceTests { .filter((artifact) -> !artifact.startsWith("build-info.json")) .map((artifact) -> requestTo( "/service/local/staging/deployByRepositoryId/example-6789/" + artifact.toString())) - .collect(Collectors.toCollection(HashSet::new)); + .collect(Collectors.toSet()); AnyOfRequestMatcher uploadRequestsMatcher = anyOf(uploads); assertThat(uploadRequestsMatcher.candidates).hasSize(150); this.server.expect(ExpectedCount.times(150), uploadRequestsMatcher).andExpect(method(HttpMethod.PUT)) @@ -157,7 +156,7 @@ class SonatypeServiceTests { .filter((artifact) -> !"build-info.json".equals(artifact.toString())) .map((artifact) -> requestTo( "/service/local/staging/deployByRepositoryId/example-6789/" + artifact.toString())) - .collect(Collectors.toCollection(HashSet::new)); + .collect(Collectors.toSet()); AnyOfRequestMatcher uploadRequestsMatcher = anyOf(uploads); assertThat(uploadRequestsMatcher.candidates).hasSize(150); this.server.expect(ExpectedCount.times(150), uploadRequestsMatcher).andExpect(method(HttpMethod.PUT)) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java index af23e11115..05eb183cc6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java @@ -17,7 +17,6 @@ package org.springframework.boot.util; import java.lang.reflect.Constructor; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -28,7 +27,6 @@ import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.springframework.core.annotation.AnnotationAwareOrderComparator; @@ -139,9 +137,7 @@ public class Instantiator { } private List instantiate(Stream typeSuppliers) { - List instances = typeSuppliers.map(this::instantiate).collect(Collectors.toCollection(ArrayList::new)); - AnnotationAwareOrderComparator.sort(instances); - return Collections.unmodifiableList(instances); + return typeSuppliers.map(this::instantiate).sorted(AnnotationAwareOrderComparator.INSTANCE).toList(); } private T instantiate(TypeSupplier typeSupplier) {