From 34fb709f1cc1aee3df246291cc9e7ea9fd54baa6 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Sat, 17 Dec 2022 18:46:22 +0100 Subject: [PATCH] [MENFORCER-435] Added documentation to artifact caches + changing the caching operation so the cached objects are already built --- .../plugins/enforcer/utils/ArtifactUtils.java | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java index f93546de..3beac7fc 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java +++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java @@ -21,10 +21,10 @@ import static java.util.Optional.ofNullable; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.apache.maven.RepositoryUtils; @@ -59,12 +59,21 @@ * @since 3.0.0 */ public final class ArtifactUtils { - private static final Map NODE_ARTIFACT_MAP = new HashMap<>(); - private static final Map AETHER_ARTIFACT_MAP = new HashMap<>(); + + /** + * Caches the results of the {@link #toArtifact(DependencyNode)} method + */ + private static final Map NODE_ARTIFACT_MAP = new ConcurrentHashMap<>(); + + /** + * Caches the results of the {@link #toArtifact(Artifact)} method + */ + private static final Map AETHER_ARTIFACT_MAP = + new ConcurrentHashMap<>(); /** * Converts {@link DependencyNode} to {@link Artifact}; in comparison - * to {@linkpl RepositoryUtils#toArtifact(org.eclipse.aether.artifact.Artifact)}, this method + * to {@link RepositoryUtils#toArtifact(org.eclipse.aether.artifact.Artifact)}, this method * assigns {@link Artifact#getScope()} and {@link Artifact#isOptional()} based on * the dependency information from the node. * @@ -72,10 +81,13 @@ public final class ArtifactUtils { * @return target artifact */ public static Artifact toArtifact(DependencyNode node) { - Artifact result = NODE_ARTIFACT_MAP.computeIfAbsent(node, n -> RepositoryUtils.toArtifact(n.getArtifact())); - ofNullable(node.getDependency()).ifPresent(dependency -> { - ofNullable(dependency.getScope()).ifPresent(result::setScope); - result.setOptional(dependency.isOptional()); + Artifact result = NODE_ARTIFACT_MAP.computeIfAbsent(node, n -> { + Artifact artifact = RepositoryUtils.toArtifact(n.getArtifact()); + ofNullable(node.getDependency()).ifPresent(dependency -> { + ofNullable(dependency.getScope()).ifPresent(artifact::setScope); + artifact.setOptional(dependency.isOptional()); + }); + return artifact; }); AETHER_ARTIFACT_MAP.putIfAbsent(result, node.getArtifact()); return result; @@ -87,7 +99,11 @@ public static Artifact toArtifact(DependencyNode node) { * @return target artifact */ public static org.eclipse.aether.artifact.Artifact toArtifact(Artifact artifact) { - return AETHER_ARTIFACT_MAP.computeIfAbsent(artifact, RepositoryUtils::toArtifact); + org.eclipse.aether.artifact.Artifact result = RepositoryUtils.toArtifact(artifact); + if (artifact != null) { + AETHER_ARTIFACT_MAP.putIfAbsent(artifact, result); + } + return result; } /** @@ -179,8 +195,8 @@ public static Set filterDependencyArtifacts(Set dependencies .map(p -> p.split(":")) .map(StringUtils::stripAll) .map(arr -> String.join(":", arr)) - .flatMap(pattern -> dependencies.stream() - .filter(artifact -> compareDependency(pattern, artifact))) + .flatMap(pattern -> + dependencies.stream().filter(artifact -> compareDependency(pattern, artifact))) .collect(Collectors.toSet())) .orElse(null); } catch (IllegalArgumentException e) {