View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo.dependencies;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.shared.dependency.graph.DependencyNode;
28  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
29  
30  /**
31   * @author Simon Wang
32   * @since 2.8
33   */
34  public class DependencyVersionMap implements DependencyNodeVisitor {
35      private boolean uniqueVersions;
36  
37      private Map<String, List<DependencyNode>> idsToNode;
38  
39      // ----------------------------------------------------------------------
40      // Public methods
41      // ----------------------------------------------------------------------
42  
43      /**
44       * Create an instance.
45       */
46      public DependencyVersionMap() {
47          idsToNode = new HashMap<>();
48      }
49  
50      /**
51       * @param uniqueVersions {@link #uniqueVersions}
52       */
53      public void setUniqueVersions(boolean uniqueVersions) {
54          this.uniqueVersions = uniqueVersions;
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      public boolean visit(DependencyNode node) {
61          addDependency(node);
62          return !containsConflicts(node);
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      public boolean endVisit(DependencyNode node) {
69          return true;
70      }
71  
72      /**
73       * Get conflicting nodes groups
74       *
75       * @return conflicting nodes groups
76       */
77      public List<List<DependencyNode>> getConflictedVersionNumbers() {
78          List<List<DependencyNode>> output = new ArrayList<>();
79          for (List<DependencyNode> nodes : idsToNode.values()) {
80              if (containsConflicts(nodes)) {
81                  output.add(nodes);
82              }
83          }
84          return output;
85      }
86  
87      // ----------------------------------------------------------------------
88      // Private methods
89      // ----------------------------------------------------------------------
90  
91      private void addDependency(DependencyNode node) {
92          String key = constructKey(node);
93          List<DependencyNode> nodes = idsToNode.get(key);
94          if (nodes == null) {
95              nodes = new ArrayList<>();
96              idsToNode.put(key, nodes);
97          }
98          nodes.add(node);
99      }
100 
101     private String constructKey(DependencyNode node) {
102         return constructKey(node.getArtifact());
103     }
104 
105     private String constructKey(Artifact artifact) {
106         return artifact.getGroupId() + ":" + artifact.getArtifactId();
107     }
108 
109     private String getVersion(Artifact artifact) {
110         return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
111     }
112 
113     private boolean containsConflicts(DependencyNode node) {
114         return containsConflicts(node.getArtifact());
115     }
116 
117     private boolean containsConflicts(Artifact artifact) {
118         return containsConflicts(idsToNode.get(constructKey(artifact)));
119     }
120 
121     /**
122      * Check whether given dependency nodes contains conflicts
123      *
124      * @param nodes
125      * @return contains:true; not contains:false;
126      */
127     private boolean containsConflicts(List<DependencyNode> nodes) {
128         String version = null;
129         for (DependencyNode node : nodes) {
130             if (version == null) {
131                 version = getVersion(node.getArtifact());
132             } else {
133                 if (version.compareTo(getVersion(node.getArtifact())) != 0) {
134                     return true;
135                 }
136             }
137         }
138         return false;
139     }
140 }