Coverage Report - org.apache.maven.archiva.dependency.graph.tasks.ReduceTransitiveEdgesVisitor
 
Classes in this File Line Coverage Branch Coverage Complexity
ReduceTransitiveEdgesVisitor
0%
0/41
0%
0/12
0
ReduceTransitiveEdgesVisitor$EdgeInfo
0%
0/2
N/A
0
ReduceTransitiveEdgesVisitor$EdgeInfoDepthComparator
0%
0/2
N/A
0
 
 1  
 package org.apache.maven.archiva.dependency.graph.tasks;
 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.ArrayList;
 23  
 import java.util.Collections;
 24  
 import java.util.Comparator;
 25  
 import java.util.HashMap;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.maven.archiva.dependency.graph.DependencyGraph;
 30  
 import org.apache.maven.archiva.dependency.graph.DependencyGraphEdge;
 31  
 import org.apache.maven.archiva.dependency.graph.DependencyGraphKeys;
 32  
 import org.apache.maven.archiva.dependency.graph.DependencyGraphNode;
 33  
 import org.apache.maven.archiva.dependency.graph.walk.DependencyGraphVisitor;
 34  
 
 35  
 /**
 36  
  * Perform a transitive reduction of the graph. 
 37  
  *
 38  
  * @version $Id: ReduceTransitiveEdgesVisitor.java 755277 2009-03-17 15:18:35Z brett $
 39  
  */
 40  0
 public class ReduceTransitiveEdgesVisitor
 41  
     extends AbstractReduceEdgeVisitor
 42  
     implements DependencyGraphVisitor
 43  
 {
 44  0
     class EdgeInfo
 45  
     {
 46  
         public DependencyGraphEdge edge;
 47  
 
 48  0
         public int depth = Integer.MAX_VALUE;
 49  
     }
 50  
 
 51  0
     class EdgeInfoDepthComparator
 52  
         implements Comparator<EdgeInfo>
 53  
     {
 54  
         public int compare( EdgeInfo obj0, EdgeInfo obj1 )
 55  
         {
 56  0
             return obj0.depth - obj1.depth;
 57  
         }
 58  
     }
 59  
 
 60  
     /**
 61  
      * A Map of &lt;(Node To) ArtifactReference, Map of &lt;(Node From) ArtifactReference, EdgeInfo&gt;&gt;
 62  
      */
 63  0
     private Map<String, Map<String, EdgeInfo>> nodeDistanceMap = new HashMap<String, Map<String, EdgeInfo>>();
 64  
 
 65  
     private int currentDepth;
 66  
 
 67  
     public void discoverGraph( DependencyGraph graph )
 68  
     {
 69  0
         super.discoverGraph( graph );
 70  0
         nodeDistanceMap.clear();
 71  0
         currentDepth = 0;
 72  0
     }
 73  
 
 74  
     public void discoverEdge( DependencyGraphEdge edge )
 75  
     {
 76  
         /* WARNING: it is unwise to remove the edge at this point.
 77  
          *          as modifying the graph as it's being walked is dangerous.
 78  
          *          
 79  
          * Just record the edge's current depth.
 80  
          */
 81  
 
 82  0
         String nodeTo = DependencyGraphKeys.toKey( edge.getNodeTo() );
 83  0
         String nodeFrom = DependencyGraphKeys.toKey( edge.getNodeFrom() );
 84  
 
 85  
         // Get sub-map
 86  0
         Map<String,EdgeInfo> edgeInfoMap = nodeDistanceMap.get( nodeTo );
 87  
 
 88  
         // Create sub-map if not present (yet)
 89  0
         if ( edgeInfoMap == null )
 90  
         {
 91  0
             edgeInfoMap = new HashMap<String,EdgeInfo>();
 92  0
             nodeDistanceMap.put( nodeTo, edgeInfoMap );
 93  
         }
 94  
 
 95  
         // Get sub-map-value.
 96  0
         EdgeInfo edgeInfo = (EdgeInfo) edgeInfoMap.get( nodeFrom );
 97  
 
 98  0
         if ( edgeInfo == null )
 99  
         {
 100  
             // Create a new edgeinfo.
 101  0
             edgeInfo = new EdgeInfo();
 102  0
             edgeInfo.edge = edge;
 103  0
             edgeInfo.depth = currentDepth;
 104  0
             edgeInfoMap.put( nodeFrom, edgeInfo );
 105  
         }
 106  
         // test the current depth, if it is less than previous depth, save it
 107  0
         else if ( currentDepth < edgeInfo.depth )
 108  
         {
 109  0
             edgeInfo.depth = currentDepth;
 110  0
             edgeInfoMap.put( nodeFrom, edgeInfo );
 111  
         }
 112  
 
 113  0
         nodeDistanceMap.put( nodeTo, edgeInfoMap );
 114  0
     }
 115  
 
 116  
     public void discoverNode( DependencyGraphNode node )
 117  
     {
 118  0
         super.discoverNode( node );
 119  0
         currentDepth++;
 120  
 
 121  0
     }
 122  
 
 123  
     public void finishNode( DependencyGraphNode node )
 124  
     {
 125  0
         super.finishNode( node );
 126  0
         currentDepth--;
 127  0
     }
 128  
 
 129  
     public void finishGraph( DependencyGraph graph )
 130  
     {
 131  0
         super.finishGraph( graph );
 132  
 
 133  
         // Now we prune/remove the edges that are transitive in nature.
 134  
 
 135  0
         Comparator<EdgeInfo> edgeInfoDepthComparator = new EdgeInfoDepthComparator();
 136  
 
 137  0
         for ( Map<String, EdgeInfo> edgeInfoMap : nodeDistanceMap.values() )
 138  
         {
 139  0
             if ( edgeInfoMap.size() > 1 )
 140  
             {
 141  0
                 List<EdgeInfo> edgeInfos = new ArrayList<EdgeInfo>();
 142  0
                 edgeInfos.addAll( edgeInfoMap.values() );
 143  0
                 Collections.sort( edgeInfos, edgeInfoDepthComparator );
 144  
 
 145  0
                 for ( int i = 1; i < edgeInfos.size(); i++ )
 146  
                 {
 147  0
                     EdgeInfo edgeInfo = (EdgeInfo) edgeInfos.get( i );
 148  0
                     graph.removeEdge( edgeInfo.edge );
 149  
                 }
 150  0
             }
 151  
         }
 152  0
     }
 153  
 }