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.HashSet;
25  import java.util.List;
26  
27  import org.apache.maven.execution.ProjectDependencyGraph;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.project.ProjectSorter;
30  
31  /**
32   * Describes the inter-dependencies between projects in the reactor.
33   * 
34   * @author Benjamin Bentmann
35   */
36  class DefaultProjectDependencyGraph
37      implements ProjectDependencyGraph
38  {
39  
40      private ProjectSorter sorter;
41  
42      /**
43       * Creates a new project dependency graph based on the specified project sorting.
44       * 
45       * @param sorter The project sorter backing the graph, must not be {@code null}.
46       */
47      public DefaultProjectDependencyGraph( ProjectSorter sorter )
48      {
49          if ( sorter == null )
50          {
51              throw new IllegalArgumentException( "project sorter missing" );
52          }
53  
54          this.sorter = sorter;
55      }
56  
57      public List<MavenProject> getSortedProjects()
58      {
59          return new ArrayList<MavenProject>( sorter.getSortedProjects() );
60      }
61  
62      public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
63      {
64          if ( project == null )
65          {
66              throw new IllegalArgumentException( "project missing" );
67          }
68  
69          Collection<String> projectIds = new HashSet<String>();
70  
71          getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
72  
73          return getProjects( projectIds );
74      }
75  
76      private void getDownstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
77      {
78          for ( String id : sorter.getDependents( projectId ) )
79          {
80              if ( projectIds.add( id ) )
81              {
82                  if ( transitive )
83                  {
84                      getDownstreamProjects( id, projectIds, transitive );
85                  }
86              }
87          }
88      }
89  
90      public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
91      {
92          if ( project == null )
93          {
94              throw new IllegalArgumentException( "project missing" );
95          }
96  
97          Collection<String> projectIds = new HashSet<String>();
98  
99          getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
100 
101         return getProjects( projectIds );
102     }
103 
104     private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
105     {
106         for ( String id : sorter.getDependencies( projectId ) )
107         {
108             if ( projectIds.add( id ) )
109             {
110                 if ( transitive )
111                 {
112                     getUpstreamProjects( id, projectIds, transitive );
113                 }
114             }
115         }
116     }
117 
118     private List<MavenProject> getProjects( Collection<String> projectIds )
119     {
120         List<MavenProject> projects = new ArrayList<MavenProject>();
121 
122         for ( MavenProject p : sorter.getSortedProjects() )
123         {
124             if ( projectIds.contains( ProjectSorter.getId( p ) ) )
125             {
126                 projects.add( p );
127             }
128         }
129 
130         return projects;
131     }
132 
133     @Override
134     public String toString()
135     {
136         return sorter.getSortedProjects().toString();
137     }
138 
139 }