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 ) )
80 {
81 if ( transitive )
82 {
83 getDownstreamProjects( id, projectIds, transitive );
84 }
85 }
86 }
87 }
88
89 public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
90 {
91 if ( project == null )
92 {
93 throw new IllegalArgumentException( "project missing" );
94 }
95
96 Collection<String> projectIds = new HashSet<String>();
97
98 getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
99
100 return getProjects( projectIds );
101 }
102
103 private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
104 {
105 for ( String id : sorter.getDependencies( projectId ) )
106 {
107 if ( projectIds.add( id ) && transitive )
108 {
109 getUpstreamProjects( id, projectIds, transitive );
110 }
111 }
112 }
113
114 private List<MavenProject> getProjects( Collection<String> projectIds )
115 {
116 List<MavenProject> projects = new ArrayList<MavenProject>( projectIds.size() );
117
118 for ( String projectId : projectIds )
119 {
120 MavenProject project = sorter.getProjectMap().get( projectId );
121
122 if ( project != null )
123 {
124 projects.add( project );
125 }
126 }
127
128 return projects;
129 }
130
131 @Override
132 public String toString()
133 {
134 return sorter.getSortedProjects().toString();
135 }
136
137 }