Coverage Report - org.apache.maven.archiva.dependency.graph.walk.WalkBreadthFirstSearch
 
Classes in this File Line Coverage Branch Coverage Complexity
WalkBreadthFirstSearch
0%
0/43
0%
0/16
0
 
 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 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  
  * WalkBreadthFirstSearch 
 35  
  *
 36  
  * @version $Id: WalkBreadthFirstSearch.java 755277 2009-03-17 15:18:35Z brett $
 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  
         // First dive down edges.
 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  
         // Next move down edges.
 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  
 }