|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.opensource.cloudbomdashboard.dependencies; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | +import static com.google.common.base.Verify.verify; |
| 21 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 22 | + |
| 23 | +import com.google.common.base.Joiner; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.common.collect.ImmutableListMultimap; |
| 26 | +import com.google.common.collect.Iterables; |
| 27 | +import com.google.common.collect.Multimaps; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | +import org.eclipse.aether.artifact.Artifact; |
| 31 | +import org.eclipse.aether.graph.Dependency; |
| 32 | +import org.eclipse.aether.graph.DependencyNode; |
| 33 | + |
| 34 | +/** Problem in a Maven artifact in a dependency tree. */ |
| 35 | +public abstract class ArtifactProblem { |
| 36 | + |
| 37 | + protected final Artifact artifact; |
| 38 | + |
| 39 | + protected final ImmutableList<DependencyNode> dependencyPath; |
| 40 | + |
| 41 | + // todo since the artifact is the last element in the list, there's really no |
| 42 | + // reason to pass the artifact here. |
| 43 | + protected ArtifactProblem(Artifact artifact, List<DependencyNode> dependencyPath) { |
| 44 | + this.artifact = checkNotNull(artifact); |
| 45 | + this.dependencyPath = ImmutableList.copyOf(dependencyPath); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns the dependency path to the artifact from the root of the dependency tree. An empty list |
| 50 | + * if the path is unknown. |
| 51 | + */ |
| 52 | + protected String getPath() { |
| 53 | + return Joiner.on(" > ").join(dependencyPath); |
| 54 | + } |
| 55 | + |
| 56 | + /** Returns the Maven artifact that has the problem. */ |
| 57 | + public Artifact getArtifact() { |
| 58 | + return artifact; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns formatted string describing {@code problems} by removing similar problems per artifact. |
| 63 | + */ |
| 64 | + public static String formatProblems(Iterable<ArtifactProblem> problems) { |
| 65 | + ImmutableListMultimap<Artifact, ArtifactProblem> artifactToProblems = |
| 66 | + Multimaps.index(problems, ArtifactProblem::getArtifact); |
| 67 | + |
| 68 | + StringBuilder output = new StringBuilder(); |
| 69 | + for (Artifact artifact : artifactToProblems.keySet()) { |
| 70 | + ImmutableList<ArtifactProblem> artifactProblems = artifactToProblems.get(artifact); |
| 71 | + int otherCount = artifactProblems.size() - 1; |
| 72 | + verify(otherCount >= 0, "artifactToProblems should have at least one value for one key"); |
| 73 | + ArtifactProblem firstProblem = Iterables.getFirst(artifactProblems, null); |
| 74 | + output.append(firstProblem); |
| 75 | + if (otherCount == 1) { |
| 76 | + output.append(" and a problem on the same artifact."); |
| 77 | + } else if (otherCount > 1) { |
| 78 | + output.append(" and " + otherCount + " problems on the same artifact."); |
| 79 | + } |
| 80 | + output.append("\n"); |
| 81 | + } |
| 82 | + return output.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean equals(Object other) { |
| 87 | + if (this == other) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + if (other == null || getClass() != other.getClass()) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + ArtifactProblem otherProblem = (ArtifactProblem) other; |
| 95 | + return Objects.equals(artifact, otherProblem.artifact) |
| 96 | + && equalsOnDependencies(dependencyPath, otherProblem.dependencyPath); |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean equalsOnDependencies( |
| 100 | + List<DependencyNode> listA, List<DependencyNode> listB) { |
| 101 | + int size = listA.size(); |
| 102 | + if (listB.size() != size) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + for (int i = 0; i < size; i++) { |
| 107 | + DependencyNode nodeA = listA.get(i); |
| 108 | + DependencyNode nodeB = listB.get(i); |
| 109 | + Dependency dependencyA = nodeA.getDependency(); |
| 110 | + Dependency dependencyB = nodeB.getDependency(); |
| 111 | + if (!Objects.equals(dependencyA, dependencyB)) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + } |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public int hashCode() { |
| 120 | + ImmutableList<Dependency> dependencyList = |
| 121 | + dependencyPath.stream() |
| 122 | + .map(DependencyNode::getDependency) |
| 123 | + .filter(Objects::nonNull) // root may not have a dependency |
| 124 | + .collect(toImmutableList()); |
| 125 | + return Objects.hash(artifact, dependencyList); |
| 126 | + } |
| 127 | +} |
0 commit comments