diff options
author | LexManos <LexManos@gmail.com> | 2021-01-17 17:35:01 -0800 |
---|---|---|
committer | LexManos <LexManos@gmail.com> | 2021-01-17 17:40:39 -0800 |
commit | 4089917696fffbd4b818fb90958d20f0714f93fb (patch) | |
tree | 1f5de4972d9afe0328d7d0ce929aac06c3bca15e /src/shared/java/net/minecraftforge/artifactural/base/artifact | |
parent | 631cd05e726092c51e95b52bb8a8bb6a2ae2cc42 (diff) | |
download | Artifactural-4089917696fffbd4b818fb90958d20f0714f93fb.tar.gz Artifactural-4089917696fffbd4b818fb90958d20f0714f93fb.tar.bz2 Artifactural-4089917696fffbd4b818fb90958d20f0714f93fb.zip |
Move to net.minecraftforge package, and update license headers.
Diffstat (limited to 'src/shared/java/net/minecraftforge/artifactural/base/artifact')
4 files changed, 339 insertions, 0 deletions
diff --git a/src/shared/java/net/minecraftforge/artifactural/base/artifact/ArtifactBase.java b/src/shared/java/net/minecraftforge/artifactural/base/artifact/ArtifactBase.java new file mode 100644 index 0000000..ddcc8a5 --- /dev/null +++ b/src/shared/java/net/minecraftforge/artifactural/base/artifact/ArtifactBase.java @@ -0,0 +1,72 @@ +/* + * Artifactural + * Copyright (c) 2018-2021. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.artifactural.base.artifact; + +import net.minecraftforge.artifactural.api.artifact.Artifact; +import net.minecraftforge.artifactural.api.artifact.ArtifactIdentifier; +import net.minecraftforge.artifactural.api.artifact.ArtifactMetadata; +import net.minecraftforge.artifactural.api.artifact.ArtifactType; +import net.minecraftforge.artifactural.api.cache.ArtifactCache; +import net.minecraftforge.artifactural.api.transform.ArtifactTransformer; + +public abstract class ArtifactBase implements Artifact { + + private final ArtifactIdentifier identifier; + private final ArtifactType type; + private final ArtifactMetadata metadata; + + public ArtifactBase(ArtifactIdentifier identifier, ArtifactType type, ArtifactMetadata metadata) { + this.identifier = identifier; + this.type = type; + this.metadata = metadata; + } + + @Override + public ArtifactIdentifier getIdentifier() { + return identifier; + } + + @Override + public ArtifactType getType() { + return type; + } + + @Override + public ArtifactMetadata getMetadata() { + return metadata; + } + + @Override + public Artifact apply(ArtifactTransformer transformer) { + if (!transformer.appliesTo(this)) return this; + return transformer.transform(this); + } + + @Override + public Artifact.Cached cache(ArtifactCache cache) { + return cache.store(this); + } + + @Override + public String toString() { + return getClass().getSimpleName() + "(" + identifier + ", " + type +", " + metadata; + } + +} diff --git a/src/shared/java/net/minecraftforge/artifactural/base/artifact/SimpleArtifactIdentifier.java b/src/shared/java/net/minecraftforge/artifactural/base/artifact/SimpleArtifactIdentifier.java new file mode 100644 index 0000000..5ab60f6 --- /dev/null +++ b/src/shared/java/net/minecraftforge/artifactural/base/artifact/SimpleArtifactIdentifier.java @@ -0,0 +1,70 @@ +/* + * Artifactural + * Copyright (c) 2018-2021. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.artifactural.base.artifact; + +import net.minecraftforge.artifactural.api.artifact.ArtifactIdentifier; + +public class SimpleArtifactIdentifier implements ArtifactIdentifier { + + private final String group, name, version, classifier, extension; + + public SimpleArtifactIdentifier(String group, String name, String version, String classifier, String extension) { + this.group = group; + this.name = name; + this.version = version; + this.classifier = classifier; + this.extension = extension; + } + + @Override + public String getGroup() { + return group; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getVersion() { + return version; + } + + @Override + public String getClassifier() { + return classifier; + } + + @Override + public String getExtension() { + return extension; + } + + @Override + public String toString() { + String ret = getGroup() + ':' + getName() + ':' + getVersion(); + if (classifier != null) + ret += ':' + getClassifier(); + if ("jar".equals(extension)) + ret += '@' + getExtension(); + return ret; + } +} diff --git a/src/shared/java/net/minecraftforge/artifactural/base/artifact/SimpleArtifactMetadata.java b/src/shared/java/net/minecraftforge/artifactural/base/artifact/SimpleArtifactMetadata.java new file mode 100644 index 0000000..21a1f20 --- /dev/null +++ b/src/shared/java/net/minecraftforge/artifactural/base/artifact/SimpleArtifactMetadata.java @@ -0,0 +1,86 @@ +/* + * Artifactural + * Copyright (c) 2018-2021. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.artifactural.base.artifact; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import net.minecraftforge.artifactural.api.artifact.ArtifactMetadata; + +public class SimpleArtifactMetadata implements ArtifactMetadata { + + private final List<Entry> entries = new LinkedList<>(); + private String hash = null; + + public SimpleArtifactMetadata() { + } + + private SimpleArtifactMetadata(SimpleArtifactMetadata parent, Entry entry) { + this.entries.addAll(parent.entries); + this.entries.add(entry); + } + + @Override + public ArtifactMetadata with(String key, String value) { + return new SimpleArtifactMetadata(this, new Entry(key, value)); + } + + @Override + public String getHash() { + if (hash != null) return hash; + try { + String str = entries.stream().map(Entry::toString).collect(Collectors.joining("\n")); + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + byte[] hashBytes = digest.digest(str.getBytes()); + StringBuilder hashBuilder = new StringBuilder(); + for (byte b : hashBytes) { + hashBuilder.append(String.format("%02x", b)); + } + return hash = hashBuilder.toString(); + } catch (NoSuchAlgorithmException ex) { + throw new RuntimeException(ex); + } + } + + @Override + public String toString() { + return "SimpleArtifactMetadata(" + entries.toString() + ", " + getHash() + ")"; + } + + private static class Entry { + + private final String key, value; + + private Entry(String key, String value) { + this.key = key; + this.value = value; + } + + @Override + public String toString() { + return '[' + key + ',' + value + ']'; + } + + } + +} diff --git a/src/shared/java/net/minecraftforge/artifactural/base/artifact/StreamableArtifact.java b/src/shared/java/net/minecraftforge/artifactural/base/artifact/StreamableArtifact.java new file mode 100644 index 0000000..0428572 --- /dev/null +++ b/src/shared/java/net/minecraftforge/artifactural/base/artifact/StreamableArtifact.java @@ -0,0 +1,111 @@ +/* + * Artifactural + * Copyright (c) 2018-2021. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.artifactural.base.artifact; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import net.minecraftforge.artifactural.api.artifact.Artifact; +import net.minecraftforge.artifactural.api.artifact.ArtifactIdentifier; +import net.minecraftforge.artifactural.api.artifact.ArtifactMetadata; +import net.minecraftforge.artifactural.api.artifact.ArtifactType; +import net.minecraftforge.artifactural.api.artifact.MissingArtifactException; +import net.minecraftforge.artifactural.api.artifact.Streamable; + +public class StreamableArtifact extends ArtifactBase { + + public static Artifact ofFile(ArtifactIdentifier identifier, ArtifactType type, File file) { + return new StreamableFileArtifact(identifier, type, file); + } + + public static Artifact ofURL(ArtifactIdentifier identifier, ArtifactType type, URL url) { + return new StreamableArtifact(identifier, type, url::openStream); + } + + public static Artifact ofBytes(ArtifactIdentifier identifier, ArtifactType type, byte[] bytes) { + return new StreamableArtifact(identifier, type, () -> new ByteArrayInputStream(bytes)); + } + + public static Artifact ofStreamable(ArtifactIdentifier identifier, ArtifactType type, Streamable streamable) { + return new StreamableArtifact(identifier, type, streamable); + } + + private final Streamable streamable; + + private StreamableArtifact(ArtifactIdentifier identifier, ArtifactType type, Streamable streamable) { + this(identifier, type, new SimpleArtifactMetadata(), streamable); + } + + private StreamableArtifact(ArtifactIdentifier identifier, ArtifactType type, ArtifactMetadata metadata, Streamable streamable) { + super(identifier, type, metadata); + this.streamable = streamable; + } + + @Override + public Artifact withMetadata(ArtifactMetadata metadata) { + return new StreamableArtifact(getIdentifier(), getType(), metadata, streamable); + } + + @Override + public boolean isPresent() { + try (InputStream is = openStream()) { + is.close(); + return true; + } catch (IOException ex) { + return false; + } + } + + @Override + public InputStream openStream() throws IOException { + return streamable.openStream(); + } + + private static class StreamableFileArtifact extends StreamableArtifact implements Artifact.Cached { + + private final File file; + + private StreamableFileArtifact(ArtifactIdentifier identifier, ArtifactType type, File file) { + super(identifier, type, () -> new FileInputStream(file)); + this.file = file; + } + + @Override + public File asFile() throws MissingArtifactException { + return file; + } + + @Override + public File getFileLocation() throws MissingArtifactException { + return file; + } + + @Override + public String toString() { + return "StreamableFileArtifact(" + file + ")"; + } + + } + +} |