Skip to content

Modernize codebase with Java improvements - Use modern Java collections API (toList() instead of Collectors.toList()) #2287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.Service;
Expand Down Expand Up @@ -231,7 +230,7 @@ private List<Artifact> resolveExtension(
return result.getArtifactResults().stream()
.filter(ArtifactResult::isResolved)
.map(ArtifactResult::getArtifact)
.collect(Collectors.toList());
.toList();
} catch (PluginResolutionException | InterpolatorException e) {
throw new ExtensionResolutionException(extension, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
Expand Down Expand Up @@ -171,14 +170,14 @@ public static Dependency toDependency(

List<Exclusion> excl = Optional.ofNullable(exclusions).orElse(Collections.emptyList()).stream()
.map(RepositoryUtils::toExclusion)
.collect(Collectors.toList());
.toList();
return new Dependency(result, artifact.getScope(), artifact.isOptional(), excl);
}

public static List<RemoteRepository> toRepos(List<ArtifactRepository> repos) {
return Optional.ofNullable(repos).orElse(Collections.emptyList()).stream()
.map(RepositoryUtils::toRepo)
.collect(Collectors.toList());
.toList();
}

public static RemoteRepository toRepo(ArtifactRepository repo) {
Expand Down Expand Up @@ -294,7 +293,7 @@ public static Dependency toDependency(

List<Exclusion> exclusions = dependency.getExclusions().stream()
.map(RepositoryUtils::toExclusion)
.collect(Collectors.toList());
.toList();

return new Dependency(
artifact,
Expand Down Expand Up @@ -326,7 +325,7 @@ public ArtifactType get(String stereotypeId) {
}

public static Collection<Artifact> toArtifacts(Collection<org.apache.maven.artifact.Artifact> artifactsToConvert) {
return artifactsToConvert.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toList());
return artifactsToConvert.stream().map(RepositoryUtils::toArtifact).toList();
}

public static WorkspaceRepository getWorkspace(RepositorySystemSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.nio.file.PathMatcher;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Exclusion;
Expand All @@ -40,7 +39,7 @@ public class ExclusionArtifactFilter implements ArtifactFilter {
public ExclusionArtifactFilter(List<Exclusion> exclusions) {
this.exclusions = exclusions;
this.predicates =
exclusions.stream().map(ExclusionArtifactFilter::toPredicate).collect(Collectors.toList());
exclusions.stream().map(ExclusionArtifactFilter::toPredicate).toList();
}

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

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

import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -56,7 +55,7 @@ public Optional<BuildResumptionData> determineBuildResumptionData(final MavenExe
.filter(project -> result.getBuildSummary(project) == null
|| result.getBuildSummary(project) instanceof BuildFailure)
.map(project -> project.getGroupId() + ":" + project.getArtifactId())
.collect(Collectors.toList());
.toList();

if (remainingProjects.isEmpty()) {
LOGGER.info("No remaining projects found, resuming the build would not make sense.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

import org.apache.maven.api.Session;
import org.apache.maven.artifact.repository.ArtifactRepository;
Expand Down Expand Up @@ -423,13 +422,13 @@ private static Settings adaptSettings(MavenExecutionRequest request) {
.localRepository(localRepo != null ? localRepo.getAbsolutePath() : null)
.interactiveMode(request.isInteractiveMode())
.offline(request.isOffline())
.proxies(request.getProxies().stream().map(Proxy::getDelegate).collect(Collectors.toList()))
.servers(request.getServers().stream().map(Server::getDelegate).collect(Collectors.toList()))
.mirrors(request.getMirrors().stream().map(Mirror::getDelegate).collect(Collectors.toList()))
.proxies(request.getProxies().stream().map(Proxy::getDelegate).toList())
.servers(request.getServers().stream().map(Server::getDelegate).toList())
.mirrors(request.getMirrors().stream().map(Mirror::getDelegate).toList())
.profiles(request.getProfiles().stream()
.map(Profile::getDelegate)
.map(SettingsUtilsV4::convertToSettingsProfile)
.collect(Collectors.toList()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is api

.toList())
.activeProfiles(request.getActiveProfiles())
.pluginGroups(request.getPluginGroups())
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.execution.ProjectDependencyGraph;
import org.apache.maven.project.CycleDetectedException;
Expand Down Expand Up @@ -153,7 +152,7 @@ private List<MavenProject> getSortedProjects(Set<String> projectIds) {
return projectIds.stream()
.map(projects::get)
.sorted(Comparator.comparingInt(order::get))
.collect(Collectors.toList());
.toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public SessionBuilder newRepositorySessionBuilder(MavenExecutionRequest request)
XmlNode dom = server.getDelegate().getConfiguration();
List<XmlNode> children = dom.children().stream()
.filter(c -> !"wagonProvider".equals(c.name()))
.collect(Collectors.toList());
.toList();
dom = XmlNode.newInstance(dom.name(), children);
PlexusConfiguration config = XmlPlexusConfiguration.toPlexusConfiguration(dom);
configProps.put("aether.transport.wagon.config." + server.getId(), config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

class CoreUtils {

Expand All @@ -37,6 +36,6 @@ public static <T> T cast(Class<T> clazz, Object o, String name) {
}

public static <U, V> List<V> map(Collection<U> list, Function<U, V> mapper) {
return list.stream().map(mapper).filter(Objects::nonNull).collect(Collectors.toList());
return list.stream().map(mapper).filter(Objects::nonNull).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.DependencyScope;
Expand Down Expand Up @@ -143,7 +142,7 @@ public List<String> computePhases(Lifecycle lifecycle) {
List<String> allPhases = graph.visitAll();
Collections.reverse(allPhases);
List<String> computed =
allPhases.stream().filter(s -> !s.startsWith("$")).collect(Collectors.toList());
allPhases.stream().filter(s -> !s.startsWith("$")).toList();
return computed;
}

Expand Down Expand Up @@ -211,7 +210,7 @@ public Collection<Lifecycle> provides() {
&& !Lifecycle.DEFAULT.equals(id)
&& !Lifecycle.SITE.equals(id))
.map(id -> wrap(all.get(id)))
.collect(Collectors.toList());
.toList();
} catch (ComponentLookupException e) {
throw new LookupException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private static List<Profile> prune(List<Profile> profiles) {
return builder.build(null).build();
})
.filter(p -> !isEmpty(p))
.collect(Collectors.toList());
.toList();
}

private static boolean isEmpty(Profile profile) {
Expand Down Expand Up @@ -324,6 +324,6 @@ private static <T extends ModelBase.Builder> T prune(T builder, ModelBase model)
private static List<Repository> pruneRepositories(List<Repository> repositories) {
return repositories.stream()
.filter(r -> !org.apache.maven.api.Repository.CENTRAL_ID.equals(r.getId()))
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public List<Lifecycle> getLifeCycles() {
return lifecyclesMap.values().stream()
.peek(l -> Objects.requireNonNull(l.getId(), "A lifecycle must have an id."))
.sorted(Comparator.comparing(Lifecycle::getId, comparator))
.collect(Collectors.toList());
.toList();
}

private Map<String, Lifecycle> lookupLifecycles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

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

import org.apache.maven.api.Lifecycle;
Expand Down Expand Up @@ -84,7 +83,7 @@ public List<TaskSegment> calculateTaskSegments(MavenSession session)
&& !rootProject.getDefaultGoal().isEmpty())) {
tasks = Stream.of(rootProject.getDefaultGoal().split("\\s+"))
.filter(g -> !g.isEmpty())
.collect(Collectors.toList());
.toList();
}

return calculateTaskSegments(session, tasks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.services.MessageBuilderFactory;
Expand Down Expand Up @@ -96,7 +95,7 @@ public static List<MavenProject> getProjects(MavenProject project, MavenSession
projectAndSubmodules.add(project);
return session.getProjects().stream() // sorted all
.filter(projectAndSubmodules::contains)
.collect(Collectors.toList()); // sorted and filtered to what we need
.toList(); // sorted and filtered to what we need
} else {
return Collections.singletonList(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -102,7 +101,7 @@ public static XmlNode convert(org.apache.maven.api.plugin.descriptor.MojoDescrip
: null,
null,
null))
.collect(Collectors.toList());
.toList();
return XmlNode.newInstance("configuration", children);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ProjectBuildList(List<ProjectSegment> items) {
*/
public ProjectBuildList getByTaskSegment(TaskSegment taskSegment) {
return new ProjectBuildList(
items.stream().filter(pb -> taskSegment == pb.getTaskSegment()).collect(Collectors.toList()));
items.stream().filter(pb -> taskSegment == pb.getTaskSegment()).toList());
}

public Map<MavenProject, ProjectSegment> selectSegment(TaskSegment taskSegment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

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

import org.apache.maven.api.Lifecycle;
Expand Down Expand Up @@ -128,7 +127,7 @@ public List<TaskSegment> calculateTaskSegments(MavenSession session) throws Exce
&& !rootProject.getDefaultGoal().isEmpty())) {
tasks = Stream.of(rootProject.getDefaultGoal().split("\\s+"))
.filter(g -> !g.isEmpty())
.collect(Collectors.toList());
.toList();
}

return calculateTaskSegments(session, tasks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.Lifecycle;
import org.apache.maven.api.model.Plugin;
import org.apache.maven.api.model.PluginExecution;
import org.apache.maven.plugin.descriptor.PluginDescriptor;

class PluginLifecycle implements Lifecycle {
Expand All @@ -47,7 +47,7 @@ public String id() {
@Override
public Collection<Phase> phases() {
return lifecycleOverlay.getPhases().stream()
.map(phase -> new Phase() {
.<Phase>map(phase -> new Phase() {
@Override
public String name() {
return phase.getId();
Expand All @@ -61,11 +61,11 @@ public List<Plugin> plugins() {
.version(pluginDescriptor.getVersion())
.configuration(phase.getConfiguration())
.executions(phase.getExecutions().stream()
.map(exec -> org.apache.maven.api.model.PluginExecution.newBuilder()
.map(exec -> PluginExecution.newBuilder()
.goals(exec.getGoals())
.configuration(exec.getConfiguration())
.build())
.collect(Collectors.toList()))
.toList())
.build());
}

Expand All @@ -84,7 +84,7 @@ public Stream<Phase> allPhases() {
return Stream.concat(Stream.of(this), phases().stream().flatMap(Phase::allPhases));
}
})
.collect(Collectors.toList());
.toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void set(String goals) {

if (goals != null && !goals.isEmpty()) {
String[] mojoGoals = goals.split(",");
mojos = Arrays.stream(mojoGoals).map(fromGoalIntoLifecycleMojo).collect(Collectors.toList());
mojos = Arrays.stream(mojoGoals).map(fromGoalIntoLifecycleMojo).toList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.RepositoryUtils;
Expand Down Expand Up @@ -240,7 +239,7 @@ private DependencyResult resolveInternal(
e.getResult().getArtifactResults().stream()
.filter(r -> !r.isResolved())
.flatMap(r -> r.getExceptions().stream()))
.collect(Collectors.toList());
.toList();
throw new PluginResolutionException(plugin, exceptions, e);
} finally {
RequestTraceHelper.exit(trace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import org.apache.maven.api.Constants;
import org.apache.maven.eventspy.AbstractEventSpy;
Expand Down Expand Up @@ -109,7 +108,7 @@ private List<String> parsePluginExcludes(RepositorySystemSession session) {
return Arrays.stream(excludes.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
.toList();
}

private ValidationReportLevel validationReportLevel(RepositorySystemSession session) {
Expand Down
Loading
Loading