1 | |
package org.apache.maven.archiva.dependency.graph.walk; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.HashMap; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import org.apache.commons.collections.Predicate; |
26 | |
import org.apache.commons.collections.functors.NotPredicate; |
27 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraph; |
28 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraphEdge; |
29 | |
import org.apache.maven.archiva.dependency.graph.DependencyGraphNode; |
30 | |
import org.apache.maven.archiva.dependency.graph.functors.EdgeDisabledPredicate; |
31 | |
import org.apache.maven.archiva.model.ArtifactReference; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class WalkBreadthFirstSearch |
39 | |
implements DependencyGraphWalker |
40 | |
{ |
41 | 0 | private Map<ArtifactReference, Integer> nodeVisitStates = new HashMap<ArtifactReference, Integer>(); |
42 | |
|
43 | |
private Predicate edgePredicate; |
44 | |
|
45 | |
public WalkBreadthFirstSearch() |
46 | 0 | { |
47 | 0 | this.edgePredicate = NotPredicate.getInstance( new EdgeDisabledPredicate() ); |
48 | 0 | } |
49 | |
|
50 | |
public Predicate getEdgePredicate() |
51 | |
{ |
52 | 0 | return this.edgePredicate; |
53 | |
} |
54 | |
|
55 | |
public void setEdgePredicate( Predicate edgePredicate ) |
56 | |
{ |
57 | 0 | this.edgePredicate = edgePredicate; |
58 | 0 | } |
59 | |
|
60 | |
public Integer getNodeVisitState( DependencyGraphNode node ) |
61 | |
{ |
62 | 0 | return (Integer) nodeVisitStates.get( node.getArtifact() ); |
63 | |
} |
64 | |
|
65 | |
public Integer getNodeVisitState( ArtifactReference artifact ) |
66 | |
{ |
67 | 0 | return (Integer) nodeVisitStates.get( artifact ); |
68 | |
} |
69 | |
|
70 | |
public void setNodeVisitState( DependencyGraphNode node, Integer state ) |
71 | |
{ |
72 | 0 | this.nodeVisitStates.put( node.getArtifact(), state ); |
73 | 0 | } |
74 | |
|
75 | |
public void setNodeVisitState( ArtifactReference artifact, Integer state ) |
76 | |
{ |
77 | 0 | this.nodeVisitStates.put( artifact, state ); |
78 | 0 | } |
79 | |
|
80 | |
private void visitEdge( DependencyGraph graph, DependencyGraphEdge e, DependencyGraphVisitor visitor ) |
81 | |
{ |
82 | 0 | visitor.discoverEdge( e ); |
83 | |
|
84 | 0 | DependencyGraphNode node = graph.getNode( e.getNodeTo() ); |
85 | |
|
86 | 0 | if ( getNodeVisitState( node ) == UNSEEN ) |
87 | |
{ |
88 | 0 | setNodeVisitState( node, PROCESSING ); |
89 | |
} |
90 | |
|
91 | 0 | visitor.finishEdge( e ); |
92 | 0 | } |
93 | |
|
94 | |
private void visitNode( DependencyGraph graph, DependencyGraphNode node, DependencyGraphVisitor visitor ) |
95 | |
{ |
96 | 0 | setNodeVisitState( node, PROCESSING ); |
97 | |
|
98 | 0 | visitor.discoverNode( node ); |
99 | |
|
100 | |
|
101 | 0 | for ( DependencyGraphEdge e : graph.getEdgesFrom( node ) ) |
102 | |
{ |
103 | 0 | if ( this.edgePredicate.evaluate( e ) ) |
104 | |
{ |
105 | 0 | visitEdge( graph, e, visitor ); |
106 | |
} |
107 | |
} |
108 | |
|
109 | |
|
110 | 0 | for ( DependencyGraphEdge e : graph.getEdgesFrom( node ) ) |
111 | |
{ |
112 | 0 | if ( this.edgePredicate.evaluate( e ) ) |
113 | |
{ |
114 | 0 | DependencyGraphNode nodeTo = graph.getNode( e.getNodeTo() ); |
115 | 0 | Integer state = getNodeVisitState( nodeTo ); |
116 | 0 | if ( ( state == UNSEEN ) || ( state == PROCESSING ) ) |
117 | |
{ |
118 | 0 | visitNode( graph, nodeTo, visitor ); |
119 | |
} |
120 | 0 | } |
121 | |
} |
122 | |
|
123 | 0 | visitor.finishNode( node ); |
124 | |
|
125 | 0 | setNodeVisitState( node, SEEN ); |
126 | 0 | } |
127 | |
|
128 | |
public void visit( DependencyGraph graph, DependencyGraphVisitor visitor ) |
129 | |
{ |
130 | 0 | visit( graph, graph.getRootNode(), visitor ); |
131 | 0 | } |
132 | |
|
133 | |
public void visit( DependencyGraph graph, DependencyGraphNode startNode, DependencyGraphVisitor visitor ) |
134 | |
{ |
135 | 0 | nodeVisitStates.clear(); |
136 | |
|
137 | 0 | for ( DependencyGraphNode node : graph.getNodes() ) |
138 | |
{ |
139 | 0 | setNodeVisitState( node, UNSEEN ); |
140 | |
} |
141 | |
|
142 | 0 | visitor.discoverGraph( graph ); |
143 | |
|
144 | 0 | visitNode( graph, startNode, visitor ); |
145 | |
|
146 | 0 | visitor.finishGraph( graph ); |
147 | 0 | } |
148 | |
} |