View Javadoc
1   package org.apache.maven.graph;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.IdentityHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Objects;
28  
29  import org.apache.maven.execution.ProjectDependencyGraph;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * Provides a sub view of another dependency graph.
34   *
35   * @author Benjamin Bentmann
36   */
37  class FilteredProjectDependencyGraph
38      implements ProjectDependencyGraph
39  {
40  
41      private ProjectDependencyGraph projectDependencyGraph;
42  
43      private Map<MavenProject, ?> whiteList;
44  
45      private List<MavenProject> sortedProjects;
46  
47      /**
48       * Creates a new project dependency graph from the specified graph.
49       *
50       * @param projectDependencyGraph The project dependency graph to create a sub view from, must not be {@code null}.
51       * @param whiteList The projects on which the dependency view should focus, must not be {@code null}.
52       */
53      FilteredProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph,
54                                      Collection<? extends MavenProject> whiteList )
55      {
56          this.projectDependencyGraph =
57                  Objects.requireNonNull( projectDependencyGraph, "projectDependencyGraph cannot be null" );
58  
59          this.whiteList = new IdentityHashMap<MavenProject, Object>();
60  
61          for ( MavenProject project : whiteList )
62          {
63              this.whiteList.put( project, null );
64          }
65      }
66  
67      /**
68       * @since 3.5.0
69       */
70      public List<MavenProject> getAllProjects()
71      {
72          return this.projectDependencyGraph.getAllProjects();
73      }
74  
75      public List<MavenProject> getSortedProjects()
76      {
77          if ( sortedProjects == null )
78          {
79              sortedProjects = applyFilter( projectDependencyGraph.getSortedProjects() );
80          }
81  
82          return new ArrayList<>( sortedProjects );
83      }
84  
85      public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
86      {
87          return applyFilter( projectDependencyGraph.getDownstreamProjects( project, transitive ) );
88      }
89  
90      public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
91      {
92          return applyFilter( projectDependencyGraph.getUpstreamProjects( project, transitive ) );
93      }
94  
95      private List<MavenProject> applyFilter( Collection<? extends MavenProject> projects )
96      {
97          List<MavenProject> filtered = new ArrayList<>( projects.size() );
98  
99          for ( MavenProject project : projects )
100         {
101             if ( whiteList.containsKey( project ) )
102             {
103                 filtered.add( project );
104             }
105         }
106 
107         return filtered;
108     }
109 
110     @Override
111     public String toString()
112     {
113         return getSortedProjects().toString();
114     }
115 
116 }