1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.graph;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.IdentityHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27
28 import org.apache.maven.execution.ProjectDependencyGraph;
29 import org.apache.maven.project.MavenProject;
30
31
32
33
34
35 class FilteredProjectDependencyGraph implements ProjectDependencyGraph {
36
37 private ProjectDependencyGraph projectDependencyGraph;
38
39 private Map<MavenProject, ?> whiteList;
40
41 private List<MavenProject> sortedProjects;
42
43
44
45
46
47
48
49 FilteredProjectDependencyGraph(
50 ProjectDependencyGraph projectDependencyGraph, Collection<? extends MavenProject> whiteList) {
51 this.projectDependencyGraph =
52 Objects.requireNonNull(projectDependencyGraph, "projectDependencyGraph cannot be null");
53
54 this.whiteList = new IdentityHashMap<>();
55
56 for (MavenProject project : whiteList) {
57 this.whiteList.put(project, null);
58 }
59 }
60
61
62
63
64 public List<MavenProject> getAllProjects() {
65 return this.projectDependencyGraph.getAllProjects();
66 }
67
68 public List<MavenProject> getSortedProjects() {
69 if (sortedProjects == null) {
70 sortedProjects = applyFilter(projectDependencyGraph.getSortedProjects());
71 }
72
73 return new ArrayList<>(sortedProjects);
74 }
75
76 public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) {
77 return applyFilter(projectDependencyGraph.getDownstreamProjects(project, transitive));
78 }
79
80 public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) {
81 return applyFilter(projectDependencyGraph.getUpstreamProjects(project, transitive));
82 }
83
84 private List<MavenProject> applyFilter(Collection<? extends MavenProject> projects) {
85 List<MavenProject> filtered = new ArrayList<>(projects.size());
86
87 for (MavenProject project : projects) {
88 if (whiteList.containsKey(project)) {
89 filtered.add(project);
90 }
91 }
92
93 return filtered;
94 }
95
96 @Override
97 public String toString() {
98 return getSortedProjects().toString();
99 }
100 }