Skip to content

Commit

Permalink
[MENFORCER-435] Added documentation to artifact caches + changing the…
Browse files Browse the repository at this point in the history
… caching operation so the cached objects are already built
  • Loading branch information
jarmoniuk committed Dec 17, 2022
1 parent bec1442 commit 34fb709
Showing 1 changed file with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,23 +59,35 @@
* @since 3.0.0
*/
public final class ArtifactUtils {
private static final Map<DependencyNode, Artifact> NODE_ARTIFACT_MAP = new HashMap<>();
private static final Map<Artifact, org.eclipse.aether.artifact.Artifact> AETHER_ARTIFACT_MAP = new HashMap<>();

/**
* Caches the results of the {@link #toArtifact(DependencyNode)} method
*/
private static final Map<DependencyNode, Artifact> NODE_ARTIFACT_MAP = new ConcurrentHashMap<>();

/**
* Caches the results of the {@link #toArtifact(Artifact)} method
*/
private static final Map<Artifact, org.eclipse.aether.artifact.Artifact> 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.
*
* @param node {@link DependencyNode} to convert to {@link Artifact}
* @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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -179,8 +195,8 @@ public static Set<Artifact> filterDependencyArtifacts(Set<Artifact> 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) {
Expand Down

0 comments on commit 34fb709

Please sign in to comment.