1 package org.apache.maven.archiva.dependency.graph.tasks;
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.maven.archiva.dependency.graph.DependencyGraph;
23 import org.apache.maven.archiva.dependency.graph.DependencyGraphEdge;
24 import org.apache.maven.archiva.dependency.graph.DependencyGraphKeys;
25 import org.apache.maven.archiva.dependency.graph.DependencyGraphNode;
26 import org.apache.maven.archiva.dependency.graph.walk.DependencyGraphVisitor;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
37 * Perform a transitive reduction of the graph.
41 public class ReduceTransitiveEdgesVisitor
42 extends AbstractReduceEdgeVisitor
43 implements DependencyGraphVisitor
47 public DependencyGraphEdge edge;
49 public int depth = Integer.MAX_VALUE;
52 class EdgeInfoDepthComparator
55 public int compare( Object obj0, Object obj1 )
57 EdgeInfo edgeInfo0 = (EdgeInfo) obj0;
58 EdgeInfo edgeInfo1 = (EdgeInfo) obj1;
60 return edgeInfo0.depth - edgeInfo1.depth;
65 * A Map of <(Node To) ArtifactReference, Map of <(Node From) ArtifactReference, EdgeInfo>>
67 private Map /*<ArtifactReference,<ArtifactReference,EdgeInfo>>*/nodeDistanceMap = new HashMap();
69 private int currentDepth;
71 public void discoverGraph( DependencyGraph graph )
73 super.discoverGraph( graph );
74 nodeDistanceMap.clear();
78 public void discoverEdge( DependencyGraphEdge edge )
80 /* WARNING: it is unwise to remove the edge at this point.
81 * as modifying the graph as it's being walked is dangerous.
83 * Just record the edge's current depth.
86 String nodeTo = DependencyGraphKeys.toKey( edge.getNodeTo() );
87 String nodeFrom = DependencyGraphKeys.toKey( edge.getNodeFrom() );
90 Map edgeInfoMap = (Map) nodeDistanceMap.get( nodeTo );
92 // Create sub-map if not present (yet)
93 if ( edgeInfoMap == null )
95 edgeInfoMap = new HashMap();
96 nodeDistanceMap.put( nodeTo, edgeInfoMap );
100 EdgeInfo edgeInfo = (EdgeInfo) edgeInfoMap.get( nodeFrom );
102 if ( edgeInfo == null )
104 // Create a new edgeinfo.
105 edgeInfo = new EdgeInfo();
106 edgeInfo.edge = edge;
107 edgeInfo.depth = currentDepth;
108 edgeInfoMap.put( nodeFrom, edgeInfo );
110 // test the current depth, if it is less than previous depth, save it
111 else if ( currentDepth < edgeInfo.depth )
113 edgeInfo.depth = currentDepth;
114 edgeInfoMap.put( nodeFrom, edgeInfo );
117 nodeDistanceMap.put( nodeTo, edgeInfoMap );
120 public void discoverNode( DependencyGraphNode node )
122 super.discoverNode( node );
127 public void finishNode( DependencyGraphNode node )
129 super.finishNode( node );
133 public void finishGraph( DependencyGraph graph )
135 super.finishGraph( graph );
137 // Now we prune/remove the edges that are transitive in nature.
139 Comparator edgeInfoDepthComparator = new EdgeInfoDepthComparator();
141 Iterator it = nodeDistanceMap.values().iterator();
142 while ( it.hasNext() )
144 Map edgeInfoMap = (Map) it.next();
146 if ( edgeInfoMap.size() > 1 )
148 List edgeInfos = new ArrayList();
149 edgeInfos.addAll( edgeInfoMap.values() );
150 Collections.sort( edgeInfos, edgeInfoDepthComparator );
152 for ( int i = 1; i < edgeInfos.size(); i++ )
154 EdgeInfo edgeInfo = (EdgeInfo) edgeInfos.get( i );
155 graph.removeEdge( edgeInfo.edge );