24
24
import java .util .List ;
25
25
import java .util .Map ;
26
26
import java .util .Objects ;
27
+ import java .util .concurrent .ConcurrentHashMap ;
27
28
import java .util .stream .Collectors ;
28
29
29
30
import org .apache .maven .execution .ProjectDependencyGraph ;
@@ -42,6 +43,34 @@ class FilteredProjectDependencyGraph implements ProjectDependencyGraph {
42
43
43
44
private final List <MavenProject > sortedProjects ;
44
45
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
+
45
74
/**
46
75
* Creates a new project dependency graph from the specified graph.
47
76
*
@@ -76,12 +105,28 @@ public List<MavenProject> getSortedProjects() {
76
105
77
106
@ Override
78
107
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 ;
80
117
}
81
118
82
119
@ Override
83
120
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 ;
85
130
}
86
131
87
132
/**
0 commit comments