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 | |
|
39 | |
|
40 | |
public class WalkDepthFirstSearch |
41 | |
implements DependencyGraphWalker |
42 | |
{ |
43 | 0 | private Map<ArtifactReference, Integer> nodeVisitStates = new HashMap<ArtifactReference, Integer>(); |
44 | |
|
45 | |
private Predicate edgePredicate; |
46 | |
|
47 | |
public WalkDepthFirstSearch() |
48 | 0 | { |
49 | 0 | this.edgePredicate = NotPredicate.getInstance( new EdgeDisabledPredicate() ); |
50 | 0 | } |
51 | |
|
52 | |
public Predicate getEdgePredicate() |
53 | |
{ |
54 | 0 | return this.edgePredicate; |
55 | |
} |
56 | |
|
57 | |
public void setEdgePredicate( Predicate edgePredicate ) |
58 | |
{ |
59 | 0 | this.edgePredicate = edgePredicate; |
60 | 0 | } |
61 | |
|
62 | |
public Integer getNodeVisitState( DependencyGraphNode node ) |
63 | |
{ |
64 | 0 | if ( node == null ) |
65 | |
{ |
66 | 0 | return SEEN; |
67 | |
} |
68 | |
|
69 | 0 | return (Integer) nodeVisitStates.get( node.getArtifact() ); |
70 | |
} |
71 | |
|
72 | |
public Integer getNodeVisitState( ArtifactReference artifact ) |
73 | |
{ |
74 | 0 | return (Integer) nodeVisitStates.get( artifact ); |
75 | |
} |
76 | |
|
77 | |
public void setNodeVisitState( DependencyGraphNode node, Integer state ) |
78 | |
{ |
79 | 0 | this.nodeVisitStates.put( node.getArtifact(), state ); |
80 | 0 | } |
81 | |
|
82 | |
public void setNodeVisitState( ArtifactReference artifact, Integer state ) |
83 | |
{ |
84 | 0 | this.nodeVisitStates.put( artifact, state ); |
85 | 0 | } |
86 | |
|
87 | |
private void visitEdge( DependencyGraph graph, DependencyGraphEdge e, DependencyGraphVisitor visitor ) |
88 | |
{ |
89 | 0 | visitor.discoverEdge( e ); |
90 | |
|
91 | 0 | DependencyGraphNode node = graph.getNode( e.getNodeTo() ); |
92 | |
|
93 | 0 | if ( getNodeVisitState( node ) == UNSEEN ) |
94 | |
{ |
95 | 0 | visitNode( graph, node, visitor ); |
96 | |
} |
97 | |
|
98 | 0 | visitor.finishEdge( e ); |
99 | 0 | } |
100 | |
|
101 | |
private void visitNode( DependencyGraph graph, DependencyGraphNode node, DependencyGraphVisitor visitor ) |
102 | |
{ |
103 | 0 | setNodeVisitState( node, PROCESSING ); |
104 | |
|
105 | 0 | visitor.discoverNode( node ); |
106 | |
|
107 | 0 | for ( DependencyGraphEdge e : graph.getEdgesFrom( node ) ) |
108 | |
{ |
109 | 0 | if ( this.edgePredicate.evaluate( e ) ) |
110 | |
{ |
111 | 0 | visitEdge( graph, e, visitor ); |
112 | |
} |
113 | |
} |
114 | |
|
115 | 0 | visitor.finishNode( node ); |
116 | |
|
117 | 0 | setNodeVisitState( node, SEEN ); |
118 | 0 | } |
119 | |
|
120 | |
public void visit( DependencyGraph graph, DependencyGraphVisitor visitor ) |
121 | |
{ |
122 | 0 | visit( graph, graph.getRootNode(), visitor ); |
123 | 0 | } |
124 | |
|
125 | |
public void visit( DependencyGraph graph, DependencyGraphNode startNode, DependencyGraphVisitor visitor ) |
126 | |
{ |
127 | 0 | nodeVisitStates.clear(); |
128 | |
|
129 | 0 | for ( DependencyGraphNode node : graph.getNodes() ) |
130 | |
{ |
131 | 0 | setNodeVisitState( node, UNSEEN ); |
132 | |
} |
133 | |
|
134 | 0 | visitor.discoverGraph( graph ); |
135 | |
|
136 | 0 | visitNode( graph, startNode, visitor ); |
137 | |
|
138 | 0 | visitor.finishGraph( graph ); |
139 | 0 | } |
140 | |
} |