Skip to content

Commit cd0ea3a

Browse files
committed
Resolves #1042: Optimisation - removed the n^2 visits of the tree.
1 parent b3c643d commit cd0ea3a

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -413,22 +413,31 @@ public void execute() throws MojoExecutionException, MojoFailureException {
413413
}
414414

415415
static Map<Pair<String, String>, Set<Model>> computeChildren(Map<File, Model> reactor) {
416-
Map<Pair<String, String>, Set<Model>> result = new HashMap<>();
417-
for (Map.Entry<File, Model> entry : reactor.entrySet()) {
418-
Optional.ofNullable(entry.getValue().getParent())
419-
.map(p -> new ImmutablePair<>(p.getGroupId(), p.getArtifactId()))
420-
.ifPresent(parent -> result.compute(parent, (f1, s) -> Optional.ofNullable(s)
421-
.map(children -> {
422-
children.add(entry.getValue());
423-
return children;
424-
})
425-
.orElse(new HashSet<Model>() {
426-
{
427-
add(entry.getValue());
428-
}
429-
})));
430-
}
431-
return result;
416+
return reactor.values().stream()
417+
.filter(v -> v.getParent() != null)
418+
.reduce(
419+
new HashMap<>(),
420+
(map, child) -> {
421+
map.compute(
422+
new ImmutablePair<>(
423+
child.getParent().getGroupId(),
424+
child.getParent().getArtifactId()),
425+
(pair, set) -> Optional.ofNullable(set)
426+
.map(children -> {
427+
children.add(child);
428+
return children;
429+
})
430+
.orElse(new HashSet<Model>() {
431+
{
432+
add(child);
433+
}
434+
}));
435+
return map;
436+
},
437+
(m1, m2) -> {
438+
m1.putAll(m2);
439+
return m1;
440+
});
432441
}
433442

434443
/**

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ public void testComputeChildren() {
252252
});
253253
}
254254
});
255-
put(new File("parent2"), new Model() {
255+
put(new File("child2"), new Model() {
256256
{
257-
setArtifactId("parent2");
257+
setArtifactId("child2");
258258
setParent(new Parent() {
259259
{
260260
setGroupId("default");

0 commit comments

Comments
 (0)