Skip to content

Commit

Permalink
[MNG-7764] - followup
Browse files Browse the repository at this point in the history
  • Loading branch information
khmarbaise committed Apr 29, 2023
1 parent 6074600 commit 40f0cd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
package org.apache.maven.lifecycle.internal;

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;
Expand Down Expand Up @@ -60,13 +60,9 @@ public ProjectBuildList getByTaskSegment(TaskSegment taskSegment) {
}

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()));
}

/**
Expand All @@ -75,12 +71,10 @@ public Map<MavenProject, ProjectSegment> selectSegment(TaskSegment taskSegment)
* @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() {
Expand Down Expand Up @@ -120,11 +114,6 @@ public boolean isEmpty() {
* @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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,31 +58,21 @@ public void set(String goals) {

if (StringUtils.isNotEmpty(goals)) {
String[] mojoGoals = StringUtils.split(goals, ",");

for (String mojoGoal : mojoGoals) {
LifecycleMojo lifecycleMojo = new LifecycleMojo();
lifecycleMojo.setGoal(mojoGoal.trim());
mojos.add(lifecycleMojo);
}
mojos = Arrays.stream(mojoGoals).map(fromGoalIntoLifecycleMojo).collect(Collectors.toList());
}
}

private final Function<String, LifecycleMojo> fromGoalIntoLifecycleMojo = s -> {
LifecycleMojo lifecycleMojo = new 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,4 @@ void testEqualsIdentity() {
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;
}
}

0 comments on commit 40f0cd0

Please sign in to comment.