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