[MNG-7764] Small code improvements (#1088)

This commit is contained in:
Karl Heinz Marbaise 2023-06-16 10:54:09 +02:00 committed by GitHub
parent c9f0c237e0
commit 5ebbb8ff50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 74 deletions

View File

@ -25,8 +25,6 @@ import javax.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.services.BuilderProblem;
@ -55,7 +53,6 @@ public class DefaultToolchainsBuilder implements ToolchainsBuilder {
@Override
public ToolchainsBuilderResult build(ToolchainsBuilderRequest request)
throws ToolchainsBuilderException, IllegalArgumentException {
DefaultSession session = (DefaultSession) request.getSession();
try {
DefaultToolchainsBuildingRequest req = new DefaultToolchainsBuildingRequest();
if (request.getGlobalToolchainsSource().isPresent()) {
@ -89,12 +86,6 @@ public class DefaultToolchainsBuilder implements ToolchainsBuilder {
}
}
private Properties toProperties(Map<String, String> map) {
Properties properties = new Properties();
properties.putAll(map);
return properties;
}
private static class MappedToolchainsSource implements org.apache.maven.building.Source {
private final Source source;

View File

@ -38,6 +38,7 @@ import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -173,7 +174,7 @@ public class DefaultMojoExecutionConfigurator implements MojoExecutionConfigurat
private Set<String> getUnknownParameters(MojoExecution mojoExecution, Set<String> parameters) {
return stream(mojoExecution.getConfiguration().getChildren())
.map(x -> x.getName())
.map(Xpp3Dom::getName)
.filter(name -> !parameters.contains(name))
.collect(Collectors.toSet());
}

View File

@ -18,14 +18,14 @@
*/
package org.apache.maven.lifecycle.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenSession;
@ -55,23 +55,14 @@ public class ProjectBuildList implements Iterable<ProjectSegment> {
* @return a project build list for the supplied task segment
*/
public ProjectBuildList getByTaskSegment(TaskSegment taskSegment) {
List<ProjectSegment> currentSegment = new ArrayList<>();
for (ProjectSegment projectBuild : items) {
if (taskSegment == projectBuild.getTaskSegment()) { // NOTE: There's no notion of taskSegment equality.
currentSegment.add(projectBuild);
}
}
return new ProjectBuildList(currentSegment);
return new ProjectBuildList(
items.stream().filter(pb -> taskSegment == pb.getTaskSegment()).collect(Collectors.toList()));
}
public Map<MavenProject, ProjectSegment> selectSegment(TaskSegment taskSegment) {
Map<MavenProject, ProjectSegment> result = new HashMap<>();
for (ProjectSegment projectBuild : items) {
if (taskSegment == projectBuild.getTaskSegment()) { // NOTE: There's no notion of taskSegment equality.
result.put(projectBuild.getProject(), projectBuild);
}
}
return result;
return items.stream()
.filter(pb -> taskSegment == pb.getTaskSegment())
.collect(Collectors.toMap(ProjectSegment::getProject, Function.identity()));
}
/**
@ -80,12 +71,10 @@ public class ProjectBuildList implements Iterable<ProjectSegment> {
* @return The projectSegment or null.
*/
public ProjectSegment findByMavenProject(MavenProject mavenProject) {
for (ProjectSegment projectBuild : items) {
if (mavenProject.equals(projectBuild.getProject())) {
return projectBuild;
}
}
return null;
return items.stream()
.filter(pb -> mavenProject.equals(pb.getProject()))
.findFirst()
.orElse(null);
}
public Iterator<ProjectSegment> iterator() {
@ -125,11 +114,6 @@ public class ProjectBuildList implements Iterable<ProjectSegment> {
* @return a set of all the projects managed by the build
*/
public Set<MavenProject> getProjects() {
Set<MavenProject> projects = new HashSet<>();
for (ProjectSegment s : items) {
projects.add(s.getProject());
}
return projects;
return items.stream().map(ProjectSegment::getProject).collect(Collectors.toSet());
}
}

View File

@ -19,10 +19,14 @@
package org.apache.maven.lifecycle.mapping;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.codehaus.plexus.util.StringUtils;
@ -54,31 +58,21 @@ public class LifecyclePhase {
if (goals != null && !goals.isEmpty()) {
String[] mojoGoals = StringUtils.split(goals, ",");
mojos = Arrays.stream(mojoGoals).map(fromGoalIntoLifecycleMojo).collect(Collectors.toList());
}
}
for (String mojoGoal : mojoGoals) {
private final Function<String, LifecycleMojo> fromGoalIntoLifecycleMojo = s -> {
LifecycleMojo lifecycleMojo = new LifecycleMojo();
lifecycleMojo.setGoal(mojoGoal.trim());
mojos.add(lifecycleMojo);
}
}
}
lifecycleMojo.setGoal(s.trim());
return lifecycleMojo;
};
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
boolean first = true;
List<LifecycleMojo> mojos = getMojos();
if (mojos != null) {
for (LifecycleMojo mojo : mojos) {
if (first) {
first = false;
} else {
sb.append(',');
}
sb.append(mojo.getGoal());
}
}
return sb.toString();
return Optional.ofNullable(getMojos()).orElse(Collections.emptyList()).stream()
.map(LifecycleMojo::getGoal)
.collect(Collectors.joining(","));
}
@Deprecated

View File

@ -1054,11 +1054,8 @@ public class MavenCli {
String referenceKey = "";
if (StringUtils.isNotEmpty(summary.getReference())) {
referenceKey = references.get(summary.getReference());
if (referenceKey == null) {
referenceKey = "[Help " + (references.size() + 1) + "]";
references.put(summary.getReference(), referenceKey);
}
referenceKey =
references.computeIfAbsent(summary.getReference(), k -> "[Help " + (references.size() + 1) + "]");
}
String msg = summary.getMessage();

View File

@ -53,15 +53,4 @@ class DeveloperTest {
void testToStringNullSafe() {
assertNotNull(new Developer().toString());
}
public void testToStringNotNonsense() {
Developer dev = new Developer();
dev.setName("Maven Tester");
dev.setEmail("tester@acme.localdomain");
dev.setId("20220118");
String s = dev.toString();
assert "Developer {id=20220118, Contributor {name=Maven Tester, email=tester@acme.localdomain}}".equals(s) : s;
}
}