1 package org.apache.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.DuplicateProjectException;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.project.ProjectSorter;
31 import org.codehaus.plexus.util.dag.CycleDetectedException;
32
33
34
35
36
37
38 class DefaultProjectDependencyGraph
39 implements ProjectDependencyGraph
40 {
41
42 private ProjectSorter sorter;
43
44
45
46
47
48
49
50
51 public DefaultProjectDependencyGraph( Collection<MavenProject> projects ) throws CycleDetectedException, DuplicateProjectException
52 {
53 this.sorter = new ProjectSorter( projects );
54 }
55
56 public List<MavenProject> getSortedProjects()
57 {
58 return new ArrayList<MavenProject>( sorter.getSortedProjects() );
59 }
60
61 public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
62 {
63 if ( project == null )
64 {
65 throw new IllegalArgumentException( "project missing" );
66 }
67
68 Collection<String> projectIds = new HashSet<String>();
69
70 getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
71
72 return getProjects( projectIds );
73 }
74
75 private void getDownstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
76 {
77 for ( String id : sorter.getDependents( projectId ) )
78 {
79 if ( projectIds.add( id ) && transitive )
80 {
81 getDownstreamProjects( id, projectIds, transitive );
82 }
83 }
84 }
85
86 public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
87 {
88 if ( project == null )
89 {
90 throw new IllegalArgumentException( "project missing" );
91 }
92
93 Collection<String> projectIds = new HashSet<String>();
94
95 getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
96
97 return getProjects( projectIds );
98 }
99
100 private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
101 {
102 for ( String id : sorter.getDependencies( projectId ) )
103 {
104 if ( projectIds.add( id ) && transitive )
105 {
106 getUpstreamProjects( id, projectIds, transitive );
107 }
108 }
109 }
110
111 private List<MavenProject> getProjects( Collection<String> projectIds )
112 {
113 List<MavenProject> projects = new ArrayList<MavenProject>( projectIds.size() );
114
115 for ( String projectId : projectIds )
116 {
117 MavenProject project = sorter.getProjectMap().get( projectId );
118
119 if ( project != null )
120 {
121 projects.add( project );
122 }
123 }
124
125 return projects;
126 }
127
128 @Override
129 public String toString()
130 {
131 return sorter.getSortedProjects().toString();
132 }
133
134 }