Skip to content

Commit

Permalink
Resolves mojohaus#1042: Optimisation - removed the n^2 visits of the …
Browse files Browse the repository at this point in the history
…tree.
  • Loading branch information
andrzejj0 committed Feb 18, 2024
1 parent b3c643d commit cd0ea3a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,22 +413,31 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

static Map<Pair<String, String>, Set<Model>> computeChildren(Map<File, Model> reactor) {
Map<Pair<String, String>, Set<Model>> result = new HashMap<>();
for (Map.Entry<File, Model> entry : reactor.entrySet()) {
Optional.ofNullable(entry.getValue().getParent())
.map(p -> new ImmutablePair<>(p.getGroupId(), p.getArtifactId()))
.ifPresent(parent -> result.compute(parent, (f1, s) -> Optional.ofNullable(s)
.map(children -> {
children.add(entry.getValue());
return children;
})
.orElse(new HashSet<Model>() {
{
add(entry.getValue());
}
})));
}
return result;
return reactor.values().stream()
.filter(v -> v.getParent() != null)
.reduce(
new HashMap<>(),
(map, child) -> {
map.compute(
new ImmutablePair<>(
child.getParent().getGroupId(),
child.getParent().getArtifactId()),
(pair, set) -> Optional.ofNullable(set)
.map(children -> {
children.add(child);
return children;
})
.orElse(new HashSet<Model>() {
{
add(child);
}
}));
return map;
},
(m1, m2) -> {
m1.putAll(m2);
return m1;
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ public void testComputeChildren() {
});
}
});
put(new File("parent2"), new Model() {
put(new File("child2"), new Model() {
{
setArtifactId("parent2");
setArtifactId("child2");
setParent(new Parent() {
{
setGroupId("default");
Expand Down

0 comments on commit cd0ea3a

Please sign in to comment.