1 package org.apache.maven.archiva.dependency.graph.walk;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.collections.Predicate;
23 import org.apache.commons.collections.functors.NotPredicate;
24 import org.apache.maven.archiva.dependency.graph.DependencyGraph;
25 import org.apache.maven.archiva.dependency.graph.DependencyGraphEdge;
26 import org.apache.maven.archiva.dependency.graph.DependencyGraphNode;
27 import org.apache.maven.archiva.dependency.graph.functors.EdgeDisabledPredicate;
28 import org.apache.maven.archiva.model.ArtifactReference;
30 import java.util.HashMap;
31 import java.util.Iterator;
35 * Perform a walk of the graph using the DepthFirstSearch algorithm.
37 * NOTE: Default edgePredicate is to NOT traverse disabled edges.
41 public class WalkDepthFirstSearch
42 implements DependencyGraphWalker
44 private Map nodeVisitStates = new HashMap();
46 private Predicate edgePredicate;
48 public WalkDepthFirstSearch()
50 this.edgePredicate = NotPredicate.getInstance( new EdgeDisabledPredicate() );
53 public Predicate getEdgePredicate()
55 return this.edgePredicate;
58 public void setEdgePredicate( Predicate edgePredicate )
60 this.edgePredicate = edgePredicate;
63 public Integer getNodeVisitState( DependencyGraphNode node )
70 return (Integer) nodeVisitStates.get( node.getArtifact() );
73 public Integer getNodeVisitState( ArtifactReference artifact )
75 return (Integer) nodeVisitStates.get( artifact );
78 public void setNodeVisitState( DependencyGraphNode node, Integer state )
80 this.nodeVisitStates.put( node.getArtifact(), state );
83 public void setNodeVisitState( ArtifactReference artifact, Integer state )
85 this.nodeVisitStates.put( artifact, state );
88 private void visitEdge( DependencyGraph graph, DependencyGraphEdge e, DependencyGraphVisitor visitor )
90 visitor.discoverEdge( e );
92 DependencyGraphNode node = graph.getNode( e.getNodeTo() );
94 if ( getNodeVisitState( node ) == UNSEEN )
96 visitNode( graph, node, visitor );
99 visitor.finishEdge( e );
102 private void visitNode( DependencyGraph graph, DependencyGraphNode node, DependencyGraphVisitor visitor )
104 setNodeVisitState( node, PROCESSING );
106 visitor.discoverNode( node );
108 Iterator edges = graph.getEdgesFrom( node ).iterator();
109 while ( edges.hasNext() )
111 DependencyGraphEdge e = (DependencyGraphEdge) edges.next();
112 if ( this.edgePredicate.evaluate( e ) )
114 visitEdge( graph, e, visitor );
118 visitor.finishNode( node );
120 setNodeVisitState( node, SEEN );
123 public void visit( DependencyGraph graph, DependencyGraphVisitor visitor )
125 visit( graph, graph.getRootNode(), visitor );
128 public void visit( DependencyGraph graph, DependencyGraphNode startNode, DependencyGraphVisitor visitor )
130 nodeVisitStates.clear();
132 Iterator nodes = graph.getNodes().iterator();
133 while ( nodes.hasNext() )
135 setNodeVisitState( (DependencyGraphNode) nodes.next(), UNSEEN );
138 visitor.discoverGraph( graph );
140 visitNode( graph, startNode, visitor );
142 visitor.finishGraph( graph );