]> source.dussan.org Git - archiva.git/blob
96799496319b90caba3fba008ba53ae06cc2e688
[archiva.git] /
1 package org.apache.maven.archiva.dependency.graph.walk;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33
34 /**
35  * WalkBreadthFirstSearch 
36  *
37  * @version $Id$
38  */
39 public class WalkBreadthFirstSearch
40     implements DependencyGraphWalker
41 {
42     private Map nodeVisitStates = new HashMap();
43
44     private Predicate edgePredicate;
45
46     public WalkBreadthFirstSearch()
47     {
48         this.edgePredicate = NotPredicate.getInstance( new EdgeDisabledPredicate() );
49     }
50
51     public Predicate getEdgePredicate()
52     {
53         return this.edgePredicate;
54     }
55
56     public void setEdgePredicate( Predicate edgePredicate )
57     {
58         this.edgePredicate = edgePredicate;
59     }
60
61     public Integer getNodeVisitState( DependencyGraphNode node )
62     {
63         return (Integer) nodeVisitStates.get( node.getArtifact() );
64     }
65
66     public Integer getNodeVisitState( ArtifactReference artifact )
67     {
68         return (Integer) nodeVisitStates.get( artifact );
69     }
70
71     public void setNodeVisitState( DependencyGraphNode node, Integer state )
72     {
73         this.nodeVisitStates.put( node.getArtifact(), state );
74     }
75
76     public void setNodeVisitState( ArtifactReference artifact, Integer state )
77     {
78         this.nodeVisitStates.put( artifact, state );
79     }
80
81     private void visitEdge( DependencyGraph graph, DependencyGraphEdge e, DependencyGraphVisitor visitor )
82     {
83         visitor.discoverEdge( e );
84
85         DependencyGraphNode node = graph.getNode( e.getNodeTo() );
86
87         if ( getNodeVisitState( node ) == UNSEEN )
88         {
89             setNodeVisitState( node, PROCESSING );
90         }
91
92         visitor.finishEdge( e );
93     }
94
95     private void visitNode( DependencyGraph graph, DependencyGraphNode node, DependencyGraphVisitor visitor )
96     {
97         setNodeVisitState( node, PROCESSING );
98
99         visitor.discoverNode( node );
100
101         Iterator edges;
102         // First dive down edges.
103         edges = graph.getEdgesFrom( node ).iterator();
104         while ( edges.hasNext() )
105         {
106             DependencyGraphEdge e = (DependencyGraphEdge) edges.next();
107             if ( this.edgePredicate.evaluate( e ) )
108             {
109                 visitEdge( graph, e, visitor );
110             }
111         }
112
113         // Next move down edges.
114         edges = graph.getEdgesFrom( node ).iterator();
115         while ( edges.hasNext() )
116         {
117             DependencyGraphEdge e = (DependencyGraphEdge) edges.next();
118
119             if ( this.edgePredicate.evaluate( e ) )
120             {
121                 DependencyGraphNode nodeTo = graph.getNode( e.getNodeTo() );
122                 Integer state = getNodeVisitState( nodeTo );
123                 if ( ( state == UNSEEN ) || ( state == PROCESSING ) )
124                 {
125                     visitNode( graph, nodeTo, visitor );
126                 }
127             }
128         }
129
130         visitor.finishNode( node );
131
132         setNodeVisitState( node, SEEN );
133     }
134
135     public void visit( DependencyGraph graph, DependencyGraphVisitor visitor )
136     {
137         visit( graph, graph.getRootNode(), visitor );
138     }
139
140     public void visit( DependencyGraph graph, DependencyGraphNode startNode, DependencyGraphVisitor visitor )
141     {
142         nodeVisitStates.clear();
143
144         Iterator nodes = graph.getNodes().iterator();
145         while ( nodes.hasNext() )
146         {
147             setNodeVisitState( (DependencyGraphNode) nodes.next(), UNSEEN );
148         }
149
150         visitor.discoverGraph( graph );
151
152         visitNode( graph, startNode, visitor );
153
154         visitor.finishGraph( graph );
155     }
156 }