View Javadoc
1   package org.apache.maven.report.projectinfo.dependencies;
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.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.shared.dependency.tree.DependencyNode;
29  import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
30  
31  /**
32   * @author Simon Wang
33   * @version $Id: DependencyVersionMap.java 1637575 2014-11-08 16:25:21Z khmarbaise $
34   * @since 2.8
35   */
36  public class DependencyVersionMap
37      implements DependencyNodeVisitor
38  {
39      private boolean uniqueVersions;
40  
41      private Map<String, List<DependencyNode>> idsToNode;
42  
43      // ----------------------------------------------------------------------
44      // Public methods
45      // ----------------------------------------------------------------------
46  
47      /**
48       * Create an instance.
49       */
50      public DependencyVersionMap()
51      {
52          idsToNode = new HashMap<String, List<DependencyNode>>();
53      }
54  
55      /**
56       * @param uniqueVersions {@link #uniqueVersions}
57       */
58      public void setUniqueVersions( boolean uniqueVersions )
59      {
60          this.uniqueVersions = uniqueVersions;
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      public boolean visit( DependencyNode node )
67      {
68          addDependency( node );
69          return !containsConflicts( node );
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public boolean endVisit( DependencyNode node )
76      {
77          return true;
78      }
79  
80      /**
81       * Get conflicting nodes groups
82       *
83       * @return conflicting nodes groups
84       */
85      public List<List<DependencyNode>> getConflictedVersionNumbers()
86      {
87          List<List<DependencyNode>> output = new ArrayList<List<DependencyNode>>();
88          for ( List<DependencyNode> nodes : idsToNode.values() )
89          {
90              if ( containsConflicts( nodes ) )
91              {
92                  output.add( nodes );
93              }
94          }
95          return output;
96      }
97  
98      // ----------------------------------------------------------------------
99      // Private methods
100     // ----------------------------------------------------------------------
101 
102     private void addDependency( DependencyNode node )
103     {
104         String key = constructKey( node );
105         List<DependencyNode> nodes = idsToNode.get( key );
106         if ( nodes == null )
107         {
108             nodes = new ArrayList<DependencyNode>();
109             idsToNode.put( key, nodes );
110         }
111         nodes.add( node );
112     }
113 
114     private String constructKey( DependencyNode node )
115     {
116         return constructKey( node.getArtifact() );
117     }
118 
119     private String constructKey( Artifact artifact )
120     {
121         return artifact.getGroupId() + ":" + artifact.getArtifactId();
122     }
123 
124     private String getVersion( Artifact artifact )
125     {
126         return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
127     }
128 
129     private boolean containsConflicts( DependencyNode node )
130     {
131         return containsConflicts( node.getArtifact() );
132     }
133 
134     private boolean containsConflicts( Artifact artifact )
135     {
136         return containsConflicts( idsToNode.get( constructKey( artifact ) ) );
137     }
138 
139     /**
140      * Check whether given dependency nodes contains conflicts
141      *
142      * @param nodes
143      * @return contains:true; not contains:false;
144      */
145     private boolean containsConflicts( List<DependencyNode> nodes )
146     {
147         String version = null;
148         for ( DependencyNode node : nodes )
149         {
150             if ( version == null )
151             {
152                 version = getVersion( node.getArtifact() );
153             }
154             else
155             {
156                 if ( version.compareTo( getVersion( node.getArtifact() ) ) != 0 )
157                 {
158                     return true;
159                 }
160             }
161         }
162         return false;
163     }
164 
165 }