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 * WalkBreadthFirstSearch
39 public class WalkBreadthFirstSearch
40 implements DependencyGraphWalker
42 private Map nodeVisitStates = new HashMap();
44 private Predicate edgePredicate;
46 public WalkBreadthFirstSearch()
48 this.edgePredicate = NotPredicate.getInstance( new EdgeDisabledPredicate() );
51 public Predicate getEdgePredicate()
53 return this.edgePredicate;
56 public void setEdgePredicate( Predicate edgePredicate )
58 this.edgePredicate = edgePredicate;
61 public Integer getNodeVisitState( DependencyGraphNode node )
63 return (Integer) nodeVisitStates.get( node.getArtifact() );
66 public Integer getNodeVisitState( ArtifactReference artifact )
68 return (Integer) nodeVisitStates.get( artifact );
71 public void setNodeVisitState( DependencyGraphNode node, Integer state )
73 this.nodeVisitStates.put( node.getArtifact(), state );
76 public void setNodeVisitState( ArtifactReference artifact, Integer state )
78 this.nodeVisitStates.put( artifact, state );
81 private void visitEdge( DependencyGraph graph, DependencyGraphEdge e, DependencyGraphVisitor visitor )
83 visitor.discoverEdge( e );
85 DependencyGraphNode node = graph.getNode( e.getNodeTo() );
87 if ( getNodeVisitState( node ) == UNSEEN )
89 setNodeVisitState( node, PROCESSING );
92 visitor.finishEdge( e );
95 private void visitNode( DependencyGraph graph, DependencyGraphNode node, DependencyGraphVisitor visitor )
97 setNodeVisitState( node, PROCESSING );
99 visitor.discoverNode( node );
102 // First dive down edges.
103 edges = graph.getEdgesFrom( node ).iterator();
104 while ( edges.hasNext() )
106 DependencyGraphEdge e = (DependencyGraphEdge) edges.next();
107 if ( this.edgePredicate.evaluate( e ) )
109 visitEdge( graph, e, visitor );
113 // Next move down edges.
114 edges = graph.getEdgesFrom( node ).iterator();
115 while ( edges.hasNext() )
117 DependencyGraphEdge e = (DependencyGraphEdge) edges.next();
119 if ( this.edgePredicate.evaluate( e ) )
121 DependencyGraphNode nodeTo = graph.getNode( e.getNodeTo() );
122 Integer state = getNodeVisitState( nodeTo );
123 if ( ( state == UNSEEN ) || ( state == PROCESSING ) )
125 visitNode( graph, nodeTo, visitor );
130 visitor.finishNode( node );
132 setNodeVisitState( node, SEEN );
135 public void visit( DependencyGraph graph, DependencyGraphVisitor visitor )
137 visit( graph, graph.getRootNode(), visitor );
140 public void visit( DependencyGraph graph, DependencyGraphNode startNode, DependencyGraphVisitor visitor )
142 nodeVisitStates.clear();
144 Iterator nodes = graph.getNodes().iterator();
145 while ( nodes.hasNext() )
147 setNodeVisitState( (DependencyGraphNode) nodes.next(), UNSEEN );
150 visitor.discoverGraph( graph );
152 visitNode( graph, startNode, visitor );
154 visitor.finishGraph( graph );