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   * @author Benjamin Bentmann
35   */
36  class FilteredProjectDependencyGraph implements ProjectDependencyGraph {
37  
38      private ProjectDependencyGraph projectDependencyGraph;
39  
40      private Map<MavenProject, ?> whiteList;
41  
42      private List<MavenProject> sortedProjects;
43  
44      /**
45       * Creates a new project dependency graph from the specified graph.
46       *
47       * @param projectDependencyGraph The project dependency graph to create a sub view from, must not be {@code null}.
48       * @param whiteList The projects on which the dependency view should focus, must not be {@code null}.
49       */
50      FilteredProjectDependencyGraph(
51              ProjectDependencyGraph projectDependencyGraph, Collection<? extends MavenProject> whiteList) {
52          this.projectDependencyGraph =
53                  Objects.requireNonNull(projectDependencyGraph, "projectDependencyGraph cannot be null");
54  
55          this.whiteList = new IdentityHashMap<MavenProject, Object>();
56  
57          for (MavenProject project : whiteList) {
58              this.whiteList.put(project, null);
59          }
60      }
61  
62      /**
63       * @since 3.5.0
64       */
65      public List<MavenProject> getAllProjects() {
66          return this.projectDependencyGraph.getAllProjects();
67      }
68  
69      public List<MavenProject> getSortedProjects() {
70          if (sortedProjects == null) {
71              sortedProjects = applyFilter(projectDependencyGraph.getSortedProjects());
72          }
73  
74          return new ArrayList<>(sortedProjects);
75      }
76  
77      public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) {
78          return applyFilter(projectDependencyGraph.getDownstreamProjects(project, transitive));
79      }
80  
81      public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) {
82          return applyFilter(projectDependencyGraph.getUpstreamProjects(project, transitive));
83      }
84  
85      private List<MavenProject> applyFilter(Collection<? extends MavenProject> projects) {
86          List<MavenProject> filtered = new ArrayList<>(projects.size());
87  
88          for (MavenProject project : projects) {
89              if (whiteList.containsKey(project)) {
90                  filtered.add(project);
91              }
92          }
93  
94          return filtered;
95      }
96  
97      @Override
98      public String toString() {
99          return getSortedProjects().toString();
100     }
101 }