Coverage Report - org.apache.maven.archiva.dependency.graph.DependencyGraph
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyGraph
0%
0/60
0%
0/20
0
 
 1  
 package org.apache.maven.archiva.dependency.graph;
 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.CollectionUtils;
 23  
 import org.apache.commons.collections.map.ListOrderedMap;
 24  
 import org.apache.maven.archiva.dependency.graph.functors.EdgeFromPredicate;
 25  
 import org.apache.maven.archiva.dependency.graph.functors.EdgeToPredicate;
 26  
 import org.apache.maven.archiva.model.ArtifactReference;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collection;
 30  
 import java.util.HashSet;
 31  
 import java.util.List;
 32  
 import java.util.Set;
 33  
 
 34  
 /**
 35  
  * DependencyGraph 
 36  
  *
 37  
  * @version $Id: DependencyGraph.java 755277 2009-03-17 15:18:35Z brett $
 38  
  */
 39  
 public class DependencyGraph
 40  
 {
 41  
     public static final int DISABLED_CYCLIC = 0;
 42  
 
 43  
     public static final int DISABLED_EXCLUDED = 1;
 44  
 
 45  
     public static final int DISABLED_OPTIONAL = 2;
 46  
 
 47  
     public static final int DISABLED_NEARER_DEP = 3;
 48  
 
 49  
     public static final int DISABLED_NEARER_EDGE = 4;
 50  
 
 51  
     private DependencyGraphNode rootNode;
 52  
 
 53  0
     private Set<DependencyGraphEdge> edges = new HashSet<DependencyGraphEdge>();
 54  
 
 55  0
     private ListOrderedMap nodes = new ListOrderedMap();
 56  
 
 57  
     public DependencyGraph( String groupId, String artifactId, String version )
 58  0
     {
 59  0
         ArtifactReference rootRef = new ArtifactReference();
 60  0
         rootRef.setGroupId( groupId );
 61  0
         rootRef.setArtifactId( artifactId );
 62  0
         rootRef.setVersion( version );
 63  0
         rootRef.setClassifier( "" );
 64  0
         rootRef.setType( "pom" );
 65  
 
 66  0
         this.rootNode = new DependencyGraphNode( rootRef );
 67  0
     }
 68  
 
 69  
     public DependencyGraph( DependencyGraphNode root )
 70  0
     {
 71  0
         this.rootNode = root;
 72  0
     }
 73  
 
 74  
     public Collection<DependencyGraphEdge> getEdges()
 75  
     {
 76  0
         return edges;
 77  
     }
 78  
 
 79  
     @SuppressWarnings("unchecked")
 80  
     public Collection<DependencyGraphNode> getNodes()
 81  
     {
 82  0
         return nodes.values();
 83  
     }
 84  
 
 85  
     public DependencyGraphNode getRootNode()
 86  
     {
 87  0
         return rootNode;
 88  
     }
 89  
 
 90  
     public void setRootNode( DependencyGraphNode rootNode )
 91  
     {
 92  0
         this.rootNode = rootNode;
 93  0
     }
 94  
 
 95  
     /**
 96  
      * Add the edge to the {@link DependencyGraph}.
 97  
      * 
 98  
      * @param edge the edge to add.
 99  
      */
 100  
     public void addEdge( final DependencyGraphEdge edge )
 101  
     {
 102  0
         if ( edge.getNodeFrom() == null )
 103  
         {
 104  0
             throw new IllegalArgumentException( "edge.nodeFrom cannot be null." );
 105  
         }
 106  
 
 107  0
         if ( edge.getNodeTo() == null )
 108  
         {
 109  0
             throw new IllegalArgumentException( "edge.nodeTo cannot be null." );
 110  
         }
 111  
 
 112  0
         this.edges.add( edge );
 113  0
     }
 114  
 
 115  
     public DependencyGraphNode addNode( DependencyGraphNode node )
 116  
     {
 117  0
         if ( node == null )
 118  
         {
 119  0
             throw new IllegalArgumentException( "Unable to add a null node." );
 120  
         }
 121  
 
 122  0
         if ( node.getArtifact() == null )
 123  
         {
 124  0
             throw new IllegalArgumentException( "Unable to add a node with a null artifact reference." );
 125  
         }
 126  
 
 127  0
         int prevNodeIdx = this.nodes.indexOf( node );
 128  
 
 129  
         // Found it in the node tree?
 130  0
         if ( prevNodeIdx >= 0 )
 131  
         {
 132  
             // Merge new node into existing node.
 133  0
             DependencyGraphNode previousNode = (DependencyGraphNode) this.nodes.get( prevNodeIdx );
 134  
 
 135  0
             if ( CollectionUtils.isNotEmpty( node.getExcludes() ) )
 136  
             {
 137  0
                 previousNode.getExcludes().addAll( node.getExcludes() );
 138  
             }
 139  
 
 140  0
             if ( CollectionUtils.isNotEmpty( node.getDependencyManagement() ) )
 141  
             {
 142  0
                 previousNode.getDependencyManagement().addAll( node.getDependencyManagement() );
 143  
             }
 144  
 
 145  0
             if ( node.isFromParent() )
 146  
             {
 147  0
                 previousNode.setFromParent( true );
 148  
             }
 149  
 
 150  
             // Return newly merged node (from existing node)
 151  0
             return previousNode;
 152  
         }
 153  
 
 154  
         // This is a new node, didn't exist before, just save it.
 155  0
         this.nodes.put( node.getArtifact(), node );
 156  
 
 157  0
         return node;
 158  
     }
 159  
 
 160  
     public boolean hasNode( DependencyGraphNode node )
 161  
     {
 162  0
         return this.nodes.containsKey( node.getArtifact() );
 163  
     }
 164  
 
 165  
     public boolean hasEdge( DependencyGraphEdge edge )
 166  
     {
 167  0
         return this.edges.contains( edge );
 168  
     }
 169  
 
 170  
     /**
 171  
      * Get the list of edges from the provided node.
 172  
      * 
 173  
      * @param node the node to use as the 'from' side of an edge.
 174  
      * @return the edges from the provided node.
 175  
      */
 176  
     public List<DependencyGraphEdge> getEdgesFrom( DependencyGraphNode node )
 177  
     {
 178  0
         List<DependencyGraphEdge> ret = new ArrayList<DependencyGraphEdge>();
 179  0
         CollectionUtils.select( this.edges, new EdgeFromPredicate( node.getArtifact() ), ret );
 180  0
         return ret;
 181  
     }
 182  
 
 183  
     /**
 184  
      * Get the list of edges to the provided node.
 185  
      * 
 186  
      * @param node the node to use as the 'to' side of an edge.
 187  
      * @return the edges to the provided node.
 188  
      */
 189  
     public List<DependencyGraphEdge> getEdgesTo( DependencyGraphNode node )
 190  
     {
 191  0
         List<DependencyGraphEdge> ret = new ArrayList<DependencyGraphEdge>();
 192  0
         CollectionUtils.select( this.edges, new EdgeToPredicate( node.getArtifact() ), ret );
 193  0
         return ret;
 194  
     }
 195  
 
 196  
     /**
 197  
      * Get the node for the specified artifact reference.
 198  
      * 
 199  
      * @param ref the artifact reference to use to find the node.
 200  
      * @return the node that was found. (null if not found)
 201  
      */
 202  
     public DependencyGraphNode getNode( ArtifactReference ref )
 203  
     {
 204  0
         return (DependencyGraphNode) this.nodes.get( ref );
 205  
     }
 206  
 
 207  
     public void removeEdge( DependencyGraphEdge edge )
 208  
     {
 209  0
         this.edges.remove( edge );
 210  0
     }
 211  
 
 212  
     public void removeNode( DependencyGraphNode node )
 213  
     {
 214  0
         List<DependencyGraphEdge> edges = getEdgesFrom( node );
 215  0
         if ( !edges.isEmpty() )
 216  
         {
 217  0
             System.out.println( "Removing node left <" + edges + "> hanging <from> edges." );
 218  
         }
 219  
 
 220  0
         edges = getEdgesTo( node );
 221  0
         if ( !edges.isEmpty() )
 222  
         {
 223  0
             System.out.println( "Removing node left <" + edges + "> hanging <to> edges." );
 224  
         }
 225  
 
 226  0
         this.nodes.remove( node.getArtifact() );
 227  0
     }
 228  
 }