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.MavenProject;
29 import org.apache.maven.project.ProjectSorter;
30
31
32
33
34
35
36 class DefaultProjectDependencyGraph
37 implements ProjectDependencyGraph
38 {
39
40 private ProjectSorter sorter;
41
42
43
44
45
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 ) && transitive )
109 {
110 getUpstreamProjects( id, projectIds, transitive );
111 }
112 }
113 }
114
115 private List<MavenProject> getProjects( Collection<String> projectIds )
116 {
117 List<MavenProject> projects = new ArrayList<MavenProject>();
118
119 for ( MavenProject p : sorter.getSortedProjects() )
120 {
121 if ( projectIds.contains( ProjectSorter.getId( p ) ) )
122 {
123 projects.add( p );
124 }
125 }
126
127 return projects;
128 }
129
130 @Override
131 public String toString()
132 {
133 return sorter.getSortedProjects().toString();
134 }
135
136 }