1 package org.apache.maven.graph;
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.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.apache.commons.lang3.Validate;
30 import org.apache.maven.execution.ProjectDependencyGraph;
31 import org.apache.maven.project.DuplicateProjectException;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.project.ProjectSorter;
34 import org.codehaus.plexus.util.dag.CycleDetectedException;
35
36
37
38
39
40
41 public class DefaultProjectDependencyGraph
42 implements ProjectDependencyGraph
43 {
44
45 private ProjectSorter sorter;
46
47 private List<MavenProject> allProjects;
48
49
50
51
52
53
54
55
56 public DefaultProjectDependencyGraph( Collection<MavenProject> projects )
57 throws CycleDetectedException, DuplicateProjectException
58 {
59 super();
60 this.allProjects = Collections.unmodifiableList( new ArrayList<>( projects ) );
61 this.sorter = new ProjectSorter( projects );
62 }
63
64
65
66
67
68
69
70
71
72
73
74 public DefaultProjectDependencyGraph( final List<MavenProject> allProjects,
75 final Collection<MavenProject> projects )
76 throws CycleDetectedException, DuplicateProjectException
77 {
78 super();
79 this.allProjects = Collections.unmodifiableList( new ArrayList<>( allProjects ) );
80 this.sorter = new ProjectSorter( projects );
81 }
82
83
84
85
86 public List<MavenProject> getAllProjects()
87 {
88 return this.allProjects;
89 }
90
91 public List<MavenProject> getSortedProjects()
92 {
93 return new ArrayList<>( sorter.getSortedProjects() );
94 }
95
96 public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
97 {
98 Validate.notNull( project, "project cannot be null" );
99
100 Set<String> projectIds = new HashSet<>();
101
102 getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
103
104 return getSortedProjects( projectIds );
105 }
106
107 private void getDownstreamProjects( String projectId, Set<String> projectIds, boolean transitive )
108 {
109 for ( String id : sorter.getDependents( projectId ) )
110 {
111 if ( projectIds.add( id ) && transitive )
112 {
113 getDownstreamProjects( id, projectIds, transitive );
114 }
115 }
116 }
117
118 public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
119 {
120 Validate.notNull( project, "project cannot be null" );
121
122 Set<String> projectIds = new HashSet<>();
123
124 getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
125
126 return getSortedProjects( projectIds );
127 }
128
129 private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
130 {
131 for ( String id : sorter.getDependencies( projectId ) )
132 {
133 if ( projectIds.add( id ) && transitive )
134 {
135 getUpstreamProjects( id, projectIds, transitive );
136 }
137 }
138 }
139
140 private List<MavenProject> getSortedProjects( Set<String> projectIds )
141 {
142 List<MavenProject> result = new ArrayList<>( projectIds.size() );
143
144 for ( MavenProject mavenProject : sorter.getSortedProjects() )
145 {
146 if ( projectIds.contains( ProjectSorter.getId( mavenProject ) ) )
147 {
148 result.add( mavenProject );
149 }
150 }
151
152 return result;
153 }
154
155 @Override
156 public String toString()
157 {
158 return sorter.getSortedProjects().toString();
159 }
160
161 }