|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2024 Maveniverse Org. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v20.html |
| 7 | + */ |
| 8 | +package eu.maveniverse.maven.toolbox.shared; |
| 9 | + |
| 10 | +import static java.util.Objects.requireNonNull; |
| 11 | + |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.function.Supplier; |
| 21 | +import org.eclipse.aether.artifact.Artifact; |
| 22 | +import org.eclipse.aether.artifact.DefaultArtifact; |
| 23 | + |
| 24 | +/** |
| 25 | + * Construction to group artifacts, make them "like a project is". |
| 26 | + * <p> |
| 27 | + * Warning: this abstraction uses extension and not packaging, as this one does not create POM, it is caller obligation. |
| 28 | + */ |
| 29 | +public final class Artifacts implements Supplier<Collection<Artifact>> { |
| 30 | + private final String groupId; |
| 31 | + private final String artifactId; |
| 32 | + private final String version; |
| 33 | + private final String extension; |
| 34 | + private final Map<CE, Path> artifacts; |
| 35 | + |
| 36 | + public Artifacts(String gav) { |
| 37 | + DefaultArtifact prototype = new DefaultArtifact(gav); |
| 38 | + this.groupId = prototype.getGroupId(); |
| 39 | + this.artifactId = prototype.getArtifactId(); |
| 40 | + this.version = prototype.getVersion(); |
| 41 | + this.extension = prototype.getExtension(); |
| 42 | + this.artifacts = new HashMap<>(); |
| 43 | + } |
| 44 | + |
| 45 | + public void addPom(Path artifact) { |
| 46 | + addArtifact(null, "pom", artifact); |
| 47 | + } |
| 48 | + |
| 49 | + public void addMain(Path artifact) { |
| 50 | + addArtifact(null, extension, artifact); |
| 51 | + } |
| 52 | + |
| 53 | + public void addSources(Path artifact) { |
| 54 | + addArtifact("sources", "jar", artifact); |
| 55 | + } |
| 56 | + |
| 57 | + public void addJavadoc(Path artifact) { |
| 58 | + addArtifact("javadoc", "jar", artifact); |
| 59 | + } |
| 60 | + |
| 61 | + public void addArtifact(String classifier, String extension, Path artifact) { |
| 62 | + requireNonNull(extension, "extension"); |
| 63 | + requireNonNull(artifact, "artifact"); |
| 64 | + if (!Files.exists(artifact) || Files.isDirectory(artifact)) { |
| 65 | + throw new IllegalArgumentException("artifact backing file must exist and cannot be a directory"); |
| 66 | + } |
| 67 | + CE ce = new CE(classifier, extension); |
| 68 | + if (artifacts.containsKey(ce)) { |
| 69 | + throw new IllegalArgumentException("artifact already present"); |
| 70 | + } |
| 71 | + artifacts.put(ce, artifact); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public List<Artifact> get() { |
| 76 | + ArrayList<Artifact> result = new ArrayList<>(artifacts.size()); |
| 77 | + artifacts.forEach((ce, path) -> result.add( |
| 78 | + new DefaultArtifact(groupId, artifactId, ce.classifier, ce.extension, version).setFile(path.toFile()))); |
| 79 | + return result; |
| 80 | + } |
| 81 | + |
| 82 | + private static final class CE { |
| 83 | + private final String classifier; |
| 84 | + private final String extension; |
| 85 | + |
| 86 | + private CE(String classifier, String extension) { |
| 87 | + this.classifier = classifier; |
| 88 | + this.extension = requireNonNull(extension, "extension"); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(Object o) { |
| 93 | + if (this == o) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + if (o == null || getClass() != o.getClass()) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + CE ce = (CE) o; |
| 100 | + return Objects.equals(classifier, ce.classifier) && Objects.equals(extension, ce.extension); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int hashCode() { |
| 105 | + return Objects.hash(classifier, extension); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String toString() { |
| 110 | + return (classifier == null ? "" : classifier) + "." + extension; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments