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