View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Provides a sub view of another dependency graph.
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       * Creates a new project dependency graph from the specified graph.
45       *
46       * @param projectDependencyGraph The project dependency graph to create a sub view from, must not be {@code null}.
47       * @param whiteList The projects on which the dependency view should focus, must not be {@code null}.
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       * @since 3.5.0
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 }