GH-74 - Bean references in Application Module Canvas now get de-duplicated.

This commit is contained in:
Oliver Drotbohm
2022-11-18 00:13:05 +01:00
parent 9d95d8715d
commit 22993f43d9
2 changed files with 22 additions and 1 deletions

View File

@@ -17,7 +17,9 @@ package org.springframework.modulith.model;
import lombok.RequiredArgsConstructor;
import java.util.HashSet;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.springframework.util.Assert;
@@ -73,6 +75,23 @@ public class ApplicationModuleDependencies {
return dependencies.stream();
}
/**
* Return all {@link ApplicationModuleDependency} instances unique by the value extracted using the given
* {@link Function}.
*
* @param extractor will never be {@literal null}.
* @return will never be {@literal null}.
*/
public Stream<ApplicationModuleDependency> uniqueStream(Function<ApplicationModuleDependency, Object> extractor) {
Assert.notNull(extractor, "Extractor function must not be null!");
var seenTargets = new HashSet<>();
return dependencies.stream()
.filter(it -> seenTargets.add(extractor.apply(it)));
}
public ApplicationModuleDependencies withType(DependencyType type) {
var filtered = dependencies.stream()

View File

@@ -29,6 +29,7 @@ import org.springframework.modulith.docs.ConfigurationProperties.ModuleProperty;
import org.springframework.modulith.docs.Documenter.CanvasOptions;
import org.springframework.modulith.docs.Documenter.CanvasOptions.Groupings;
import org.springframework.modulith.model.ApplicationModule;
import org.springframework.modulith.model.ApplicationModuleDependency;
import org.springframework.modulith.model.ApplicationModules;
import org.springframework.modulith.model.ArchitecturallyEvidentType;
import org.springframework.modulith.model.DependencyType;
@@ -363,7 +364,8 @@ class Asciidoctor {
*/
public String renderBeanReferences(ApplicationModule module) {
var bullets = module.getDependencies(modules, DependencyType.USES_COMPONENT).stream()
var bullets = module.getDependencies(modules, DependencyType.USES_COMPONENT)
.uniqueStream(ApplicationModuleDependency::getTargetType)
.map(it -> "%s (in %s)".formatted(toInlineCode(it.getTargetType()), it.getTargetModule().getDisplayName()))
.map(this::toBulletPoint)
.collect(Collectors.joining("\n"));