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  
28  import org.apache.commons.lang3.Validate;
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      public FilteredProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph,
54                                             Collection<? extends MavenProject> whiteList )
55      {
56          this.projectDependencyGraph =
57              Validate.notNull( 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      public List<MavenProject> getSortedProjects()
68      {
69          if ( sortedProjects == null )
70          {
71              sortedProjects = applyFilter( projectDependencyGraph.getSortedProjects() );
72          }
73  
74          return new ArrayList<>( sortedProjects );
75      }
76  
77      public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
78      {
79          return applyFilter( projectDependencyGraph.getDownstreamProjects( project, transitive ) );
80      }
81  
82      public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
83      {
84          return applyFilter( projectDependencyGraph.getUpstreamProjects( project, transitive ) );
85      }
86  
87      private List<MavenProject> applyFilter( Collection<? extends MavenProject> projects )
88      {
89          List<MavenProject> filtered = new ArrayList<>( projects.size() );
90  
91          for ( MavenProject project : projects )
92          {
93              if ( whiteList.containsKey( project ) )
94              {
95                  filtered.add( project );
96              }
97          }
98  
99          return filtered;
100     }
101 
102     @Override
103     public String toString()
104     {
105         return getSortedProjects().toString();
106     }
107 
108 }