2424import java .util .List ;
2525import java .util .Map ;
2626import java .util .Objects ;
27+ import java .util .concurrent .ConcurrentHashMap ;
2728import java .util .stream .Collectors ;
2829
2930import org .apache .maven .execution .ProjectDependencyGraph ;
@@ -42,6 +43,34 @@ class FilteredProjectDependencyGraph implements ProjectDependencyGraph {
4243
4344 private final List <MavenProject > sortedProjects ;
4445
46+ private final Map <Key , List <MavenProject >> cache = new ConcurrentHashMap <>();
47+
48+ private static class Key {
49+ private final MavenProject project ;
50+ private final boolean transitive ;
51+ private final boolean upstream ;
52+
53+ private Key (MavenProject project , boolean transitive , boolean upstream ) {
54+ this .project = project ;
55+ this .transitive = transitive ;
56+ this .upstream = upstream ;
57+ }
58+
59+ @ Override
60+ public boolean equals (Object o ) {
61+ if (o == null || getClass () != o .getClass ()) {
62+ return false ;
63+ }
64+ Key key = (Key ) o ;
65+ return Objects .equals (project , key .project ) && transitive == key .transitive && upstream == key .upstream ;
66+ }
67+
68+ @ Override
69+ public int hashCode () {
70+ return Objects .hash (project , transitive , upstream );
71+ }
72+ }
73+
4574 /**
4675 * Creates a new project dependency graph from the specified graph.
4776 *
@@ -76,12 +105,28 @@ public List<MavenProject> getSortedProjects() {
76105
77106 @ Override
78107 public List <MavenProject > getDownstreamProjects (MavenProject project , boolean transitive ) {
79- return applyFilter (projectDependencyGraph .getDownstreamProjects (project , transitive ), transitive , false );
108+ Key key = new Key (project , transitive , false );
109+ // Do not use computeIfAbsent here, as the computation is recursive
110+ // and this is not supported by computeIfAbsent.
111+ List <MavenProject > list = cache .get (key );
112+ if (list == null ) {
113+ list = applyFilter (projectDependencyGraph .getDownstreamProjects (project , transitive ), transitive , false );
114+ cache .put (key , list );
115+ }
116+ return list ;
80117 }
81118
82119 @ Override
83120 public List <MavenProject > getUpstreamProjects (MavenProject project , boolean transitive ) {
84- return applyFilter (projectDependencyGraph .getUpstreamProjects (project , transitive ), transitive , true );
121+ Key key = new Key (project , transitive , true );
122+ // Do not use computeIfAbsent here, as the computation is recursive
123+ // and this is not supported by computeIfAbsent.
124+ List <MavenProject > list = cache .get (key );
125+ if (list == null ) {
126+ list = applyFilter (projectDependencyGraph .getUpstreamProjects (project , transitive ), transitive , true );
127+ cache .put (key , list );
128+ }
129+ return list ;
85130 }
86131
87132 /**
0 commit comments