From 58b9b268a1e6bd1270344e0237d0dac752028d05 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 17 Aug 2024 13:26:12 +0200 Subject: [PATCH] Update DependencyVector This commit introduces a "Builder" nested class, adds a way to specify which other vector a vector provides and includes a few smaller changes to DependencyVectors. This commit does not work at the moment as I'm in the process of rewriting the DependencyResolver to work correctly, as it should. --- .../sosengine/ansi/AnsiSubsystem.java | 6 +- .../staropensource/sosengine/base/Engine.java | 6 +- .../types/DependencySubsystemVector.java | 75 ++++-- .../base/logging/LoggerInstance.java | 6 +- .../base/types/DependencyVector.java | 252 ++++++++++++++++-- .../graphics/glfw/GlfwSubsystem.java | 15 +- .../graphics/opengl/OpenGlSubsystem.java | 13 +- .../sosengine/graphics/GraphicsSubsystem.java | 6 +- .../graphics/vulkan/VulkanSubsystem.java | 14 +- .../slf4j_compat/Slf4jCompatSubsystem.java | 6 +- 10 files changed, 353 insertions(+), 46 deletions(-) diff --git a/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiSubsystem.java b/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiSubsystem.java index 1b31e326..7db5830a 100644 --- a/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiSubsystem.java +++ b/ansi/src/main/java/de/staropensource/sosengine/ansi/AnsiSubsystem.java @@ -77,6 +77,10 @@ public final class AnsiSubsystem extends SubsystemClass { /** {@inheritDoc} */ @Override public @NotNull DependencyVector getDependencyVector() { - return new DependencyVector("ansi", StarOpenSourceVersioningSystem.class, EngineInformation.getVersioningString()); + return new DependencyVector.Builder() + .setIdentifier("ansi") + .setVersioningSystem(StarOpenSourceVersioningSystem.class) + .setVersion(EngineInformation.getVersioningString()) + .build(); } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/Engine.java b/base/src/main/java/de/staropensource/sosengine/base/Engine.java index 96e4bb7d..79467548 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/Engine.java +++ b/base/src/main/java/de/staropensource/sosengine/base/Engine.java @@ -435,7 +435,11 @@ public final class Engine extends SubsystemClass { /** {@inheritDoc} */ @Override public @NotNull DependencyVector getDependencyVector() { - return new DependencyVector("engine", StarOpenSourceVersioningSystem.class, EngineInformation.getVersioningString()); + return new DependencyVector.Builder() + .setIdentifier("engine") + .setVersioningSystem(StarOpenSourceVersioningSystem.class) + .setVersion(EngineInformation.getVersioningString()) + .build(); } /** diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/types/DependencySubsystemVector.java b/base/src/main/java/de/staropensource/sosengine/base/internal/types/DependencySubsystemVector.java index 0f6032ad..85195aa5 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/types/DependencySubsystemVector.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/types/DependencySubsystemVector.java @@ -24,8 +24,9 @@ import de.staropensource.sosengine.base.classes.VersioningSystem; import de.staropensource.sosengine.base.types.DependencyVector; import lombok.Getter; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import java.util.List; +import java.util.Set; /** * Represents a dependency vector with an additional {@code mainClass} field. @@ -50,41 +51,83 @@ public final class DependencySubsystemVector extends DependencyVector { private final SubsystemClass mainClass; /** - * Reuses a {@link DependencyVector} instance. + * Reuses an existing {@link DependencyVector} to create a new {@link DependencySubsystemVector}. * - * @param vector existing dependency vector to use + * @param vector existing dependency vector to reuse * @param mainClass {@link SubsystemClass} to associate * @since v1-alpha1 */ public DependencySubsystemVector(@NotNull DependencyVector vector, @NotNull SubsystemClass mainClass) { - super(mainClass.getName(), vector.getVersioningSystem(), vector.getVersion(), vector.getDependencies()); + super(mainClass.getName(), vector.getVersioningSystem(), vector.getVersion(), vector.getDependencies(), vector.getProvides()); this.mainClass = mainClass; } /** - * Creates a new dependency vector. + * Creates a new {@link DependencySubsystemVector}. * * @param versioningSystem versioning system to use * @param version version - * @param dependencies dependencies in the usual format ({@code dependencyA}, {@code dependencyB=5.1}, {@code dependencyC>3.1}, {@code dependencyD<6.1}) + * @param dependencies dependencies in the usual format ({@code dependencyA}, {@code dependencyB=5.1}, {@code dependencyC>3.1}, {@code dependencyD<6.1}, {@code dependencyE>5.3<5.4}) + * @param provides vectors this vector provides, in the usual format ({@code identifier=version}) * @param mainClass {@link SubsystemClass} to associate * @since v1-alpha1 */ - public DependencySubsystemVector(@NotNull Class versioningSystem, @NotNull String version, @NotNull List<@NotNull String> dependencies, @NotNull SubsystemClass mainClass) { - super(mainClass.getName(), versioningSystem, version, dependencies); + public DependencySubsystemVector(@NotNull Class versioningSystem, @NotNull String version, @NotNull Set<@NotNull String> dependencies, @NotNull Set<@NotNull String> provides, @NotNull SubsystemClass mainClass) { + super(mainClass.getName(), versioningSystem, version, dependencies, provides); this.mainClass = mainClass; } /** - * Creates a new dependency vector. + * Provides an API for building {@link DependencySubsystemVector}s more easily. * - * @param versioningSystem versioning system to use - * @param version version - * @param mainClass {@link SubsystemClass} to associate - * @since v1-alpha1 + * @since v1-alpha4 */ - public DependencySubsystemVector(@NotNull Class versioningSystem, @NotNull String version, @NotNull SubsystemClass mainClass) { - super(mainClass.getName(), versioningSystem, version); - this.mainClass = mainClass; + public static final class Builder extends DependencyVector.Builder { + /** + * Contains the {@link SubsystemClass} to associate. + * + * @see DependencySubsystemVector#mainClass + * @since v1-alpha4 + */ + private @Nullable SubsystemClass mainClass; + + /** + * {@inheritDoc} + * @deprecated Use {@link #buildCustom()} instead + */ + @Deprecated + @Override + public @NotNull DependencyVector build() { + return super.build(); + } + + /** + * Builds a new {@link DependencySubsystemVector} instance. + * + * @return new {@link DependencySubsystemVector} + * @throws IllegalStateException if the identifier, versioning system, version or main class is unset + * @since v1-alpha4 + */ + public DependencySubsystemVector buildCustom() { + setIdentifier("this does not need to be set"); + + // Check for required fields + checkRequiredFields(); + + // Set defaults + setDefaults(); + + //noinspection DataFlowIssue // IDE is dumb + return new DependencySubsystemVector(getVersioningSystem(), getVersion(), getDependencies(), getProvides(), mainClass); + } + + /** {@inheritDoc} */ + @Override + protected void checkRequiredFields() throws IllegalStateException { + super.checkRequiredFields(); + + if (mainClass == null) + throw new IllegalStateException("The main class is unset"); + } } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/logging/LoggerInstance.java b/base/src/main/java/de/staropensource/sosengine/base/logging/LoggerInstance.java index 2628bc69..36b4c3c2 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/logging/LoggerInstance.java +++ b/base/src/main/java/de/staropensource/sosengine/base/logging/LoggerInstance.java @@ -245,12 +245,12 @@ public final class LoggerInstance { public LoggerInstance build() throws IllegalStateException { // Check for required fields if (clazz == null) - throw new IllegalStateException("The class is unset."); + throw new IllegalStateException("The class is unset"); // Set defaults - if (origin == null || origin.isEmpty()) + if (origin == null || origin.isBlank()) origin = "APPLICATION"; - if (metadata == null || metadata.isEmpty()) + if (metadata == null || metadata.isBlank()) metadata = null; return new LoggerInstance(clazz, origin, metadata); diff --git a/base/src/main/java/de/staropensource/sosengine/base/types/DependencyVector.java b/base/src/main/java/de/staropensource/sosengine/base/types/DependencyVector.java index 1ee2e5aa..2d7859bf 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/types/DependencyVector.java +++ b/base/src/main/java/de/staropensource/sosengine/base/types/DependencyVector.java @@ -23,9 +23,10 @@ import de.staropensource.sosengine.base.classes.VersioningSystem; import de.staropensource.sosengine.base.utility.DependencyResolver; import lombok.Getter; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; /** * Represents a dependency vector, used by {@link DependencyResolver}. @@ -72,16 +73,28 @@ public class DependencyVector { protected @NotNull String version; /** - * Contains all vectors this vector hard-depends on. + * Contains a set of all identifiers and their versions this vector depends on. * - * @since v1-alpha1 + * @since v1-alpha4 * -- GETTER -- - * Returns all vectors this vector hard-depends on. + * Contains a set of all identifiers and their versions this vector depends on. * - * @return hard dependencies + * @return dependencies * @since v1-alpha1 */ - protected @NotNull List<@NotNull String> dependencies; + protected @NotNull Set<@NotNull String> dependencies; + + /** + * Contains a set of all identifiers and their versions this vector provides. + * + * @since v1-alpha4 + * -- GETTER -- + * Contains a set of all identifiers and their versions this vector provides. + * + * @return provided vectors + * @since v1-alpha4 + */ + protected @NotNull Set<@NotNull String> provides; /** * Creates a new dependency vector. @@ -89,26 +102,229 @@ public class DependencyVector { * @param identifier identifier (name for example) * @param versioningSystem versioning system to use * @param version version - * @param dependencies dependencies in the usual format ({@code dependencyA}, {@code dependencyB=5.1}, {@code dependencyC>3.1}, {@code dependencyD<6.1}) + * @param dependencies dependencies in the usual format ({@code dependencyA}, {@code dependencyB=5.1}, {@code dependencyC>3.1}, {@code dependencyD<6.1}, {@code dependencyE>5.3<5.4}) + * @param provides vectors this vector provides, in the usual format ({@code identifier=version}) + * @since v1-alpha4 */ - public DependencyVector(@NotNull String identifier, @NotNull Class versioningSystem, @NotNull String version, @NotNull List<@NotNull String> dependencies) { + protected DependencyVector(@NotNull String identifier, @NotNull Class versioningSystem, @NotNull String version, @NotNull Set<@NotNull String> dependencies, @NotNull Set<@NotNull String> provides) { this.identifier = identifier; this.versioningSystem = versioningSystem; this.version = version; this.dependencies = dependencies; + this.provides = provides; } /** - * Creates a new dependency vector. + * Provides an API for building {@link DependencyVector}s more easily. * - * @param identifier identifier (name for example) - * @param versioningSystem versioning system to use - * @param version version + * @since v1-alpha4 */ - public DependencyVector(@NotNull String identifier, @NotNull Class versioningSystem, @NotNull String version) { - this.identifier = identifier; - this.versioningSystem = versioningSystem; - this.version = version; - this.dependencies = new ArrayList<>(); + @SuppressWarnings({ "unused" }) + public static class Builder { + /** + * Contains the identifier of the new vector. + * + * @see DependencyVector#identifier + * @since v1-alpha4 + */ + private @Nullable String identifier; + + /** + * Contains which versioning system the new vector should use. + * + * @see DependencyVector#versioningSystem + * @since v1-alpha4 + */ + private @Nullable Class versioningSystem; + + /** + * Contains the version of the new vector. + * + * @see DependencyVector#version + * @since v1-alpha4 + */ + private @Nullable String version; + + /** + * Contains a set of all identifiers and their versions the new vector should depend on. + * + * @see DependencyVector#dependencies + * @since v1-alpha4 + */ + private @Nullable Set<@NotNull String> dependencies; + + /** + * Contains a set of all identifiers and their versions the new vector should provide. + * + * @see DependencyVector#provides + * @since v1-alpha4 + */ + private @Nullable Set<@NotNull String> provides; + + /** + * Constructs this class. + * + * @since v1-alpha4 + */ + public Builder() {} + + /** + * Builds a new {@link DependencyVector} instance. + * + * @return new {@link DependencyVector} + * @throws IllegalStateException if the identifier, versioning system or version is unset + * @since v1-alpha4 + */ + public @NotNull DependencyVector build() throws IllegalStateException { + // Check for required fields + checkRequiredFields(); + + // Set defaults + setDefaults(); + + //noinspection DataFlowIssue // IDE is dumb + return new DependencyVector(identifier, versioningSystem, version, dependencies, provides); + } + + /** + * Checks for required fields. + * + * @since v1-alpha4 + */ + protected void checkRequiredFields() throws IllegalStateException { + if (identifier == null || identifier.isBlank()) + throw new IllegalStateException("The identifier is unset"); + if (versioningSystem == null) + throw new IllegalStateException("The versioning system is unset"); + if (version == null || version.isBlank()) + throw new IllegalStateException("The version is unset"); + } + + /** + * Allows extenders to set defaults. + * + * @since v1-alpha4 + */ + protected void setDefaults() { + if (dependencies == null) + dependencies = new HashSet<>(); + if (provides == null) + provides = new HashSet<>(); + } + + /** + * Returns the identifier of the new vector. + * + * @return identifier + * @see DependencyVector#identifier + * @since v1-alpha4 + */ + public final @Nullable String getIdentifier() { + return identifier; + } + + /** + * Returns the versioning system the new vector should use. + * + * @return versioning system to use + * @see DependencyVector#versioningSystem + * @since v1-alpha4 + */ + public final @Nullable Class getVersioningSystem() { + return versioningSystem; + } + + /** + * Returns the version of the new vector. + * + * @return vector version + * @see DependencyVector#version + * @since v1-alpha4 + */ + public final @Nullable String getVersion() { + return version; + } + + /** + * Contains a set of all identifiers and their versions the new vector should depend on. + * + * @return dependencies + * @see DependencyVector#dependencies + * @since v1-alpha4 + */ + public final @Nullable Set<@NotNull String> getDependencies() { + return dependencies; + } + + /** + * Contains a set of all identifiers and their versions the new vector should provide. + * + * @return provided vectors + * @see DependencyVector#provides + * @since v1-alpha4 + */ + public final @Nullable Set<@NotNull String> getProvides() { + return provides; + } + + /** + * Sets the identifier of the new vector. + * + * @param identifier new identifier + * @see DependencyVector#identifier + * @since v1-alpha4 + */ + public final @NotNull DependencyVector.Builder setIdentifier(@Nullable String identifier) { + this.identifier = identifier; + return this; + } + + /** + * Sets the versioning system the new vector should use. + * + * @param versioningSystem versioning system to use + * @see DependencyVector#versioningSystem + * @since v1-alpha4 + */ + public final @NotNull DependencyVector.Builder setVersioningSystem(@Nullable Class versioningSystem) { + this.versioningSystem = versioningSystem; + return this; + } + + /** + * Sets the version of the new vector. + * + * @param version vector version + * @see DependencyVector#version + * @since v1-alpha4 + */ + public final @NotNull DependencyVector.Builder setVersion(@Nullable String version) { + this.version = version; + return this; + } + + /** + * Sets a set of all identifiers and their versions the new vector should depend on. + * + * @param dependencies dependencies + * @see DependencyVector#dependencies + * @since v1-alpha4 + */ + public final @NotNull DependencyVector.Builder setDependencies(@Nullable Set<@NotNull String> dependencies) { + this.dependencies = dependencies; + return this; + } + + /** + * Sets a set of all identifiers and their versions the new vector should provide. + * + * @param provides provided vectors + * @see DependencyVector#provides + * @since v1-alpha4 + */ + public final @NotNull DependencyVector.Builder setProvides(@Nullable Set<@NotNull String> provides) { + this.provides = provides; + return this; + } } } diff --git a/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/GlfwSubsystem.java b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/GlfwSubsystem.java index 7eef63b3..5d4db974 100644 --- a/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/GlfwSubsystem.java +++ b/graphics/glfw/src/main/java/de/staropensource/sosengine/graphics/glfw/GlfwSubsystem.java @@ -27,14 +27,15 @@ import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.DependencyVector; import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.graphics.events.GraphicsErrorEvent; -import de.staropensource.sosengine.graphics.glfw.exceptions.GlfwInitializationException; import de.staropensource.sosengine.graphics.exceptions.NotOnMainThreadException; +import de.staropensource.sosengine.graphics.glfw.exceptions.GlfwInitializationException; import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallbackI; -import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import static org.lwjgl.glfw.GLFW.*; @@ -111,7 +112,15 @@ public final class GlfwSubsystem extends SubsystemClass { /** {@inheritDoc} */ @Override public @NotNull DependencyVector getDependencyVector() { - return new DependencyVector(getName(), StarOpenSourceVersioningSystem.class, EngineInformation.getVersioningString(), Arrays.stream(new String[]{ "graphics" }).toList()); + Set<@NotNull String> dependencies = new HashSet<>(); + dependencies.add("graphics"); + + return new DependencyVector.Builder() + .setIdentifier(getName()) + .setVersioningSystem(StarOpenSourceVersioningSystem.class) + .setVersion(EngineInformation.getVersioningString()) + .setDependencies(dependencies) + .build(); } /** diff --git a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java index a810d253..f16d56ad 100644 --- a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java +++ b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java @@ -37,7 +37,9 @@ import lombok.Getter; import org.jetbrains.annotations.NotNull; import org.lwjgl.system.MemoryUtil; +import java.util.HashSet; import java.util.Locale; +import java.util.Set; import static org.lwjgl.glfw.GLFW.*; @@ -138,7 +140,16 @@ public final class OpenGlSubsystem extends ApiClass { /** {@inheritDoc} */ @Override public @NotNull DependencyVector getDependencyVector() { - return new DependencyVector("opengl", StarOpenSourceVersioningSystem.class, EngineInformation.getVersioningString()); + Set<@NotNull String> dependencies = new HashSet<>(); + dependencies.add("graphics"); + dependencies.add("glfw"); + + return new DependencyVector.Builder() + .setIdentifier(getName()) + .setVersioningSystem(StarOpenSourceVersioningSystem.class) + .setVersion(EngineInformation.getVersioningString()) + .setDependencies(dependencies) + .build(); } /** {@inheritDoc} */ diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java index 3ff57ee0..d828dd09 100644 --- a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java @@ -141,7 +141,11 @@ public final class GraphicsSubsystem extends SubsystemClass { /** {@inheritDoc} */ @Override public @NotNull DependencyVector getDependencyVector() { - return new DependencyVector("graphics", StarOpenSourceVersioningSystem.class, EngineInformation.getVersioningString()); + return new DependencyVector.Builder() + .setIdentifier("graphics") + .setVersioningSystem(StarOpenSourceVersioningSystem.class) + .setVersion(EngineInformation.getVersioningString()) + .build(); } /** diff --git a/graphics/vulkan/src/main/java/de/staropensource/sosengine/graphics/vulkan/VulkanSubsystem.java b/graphics/vulkan/src/main/java/de/staropensource/sosengine/graphics/vulkan/VulkanSubsystem.java index 9edf789c..695a3bbd 100644 --- a/graphics/vulkan/src/main/java/de/staropensource/sosengine/graphics/vulkan/VulkanSubsystem.java +++ b/graphics/vulkan/src/main/java/de/staropensource/sosengine/graphics/vulkan/VulkanSubsystem.java @@ -29,6 +29,9 @@ import de.staropensource.sosengine.base.utility.Miscellaneous; import lombok.Getter; import org.jetbrains.annotations.NotNull; +import java.util.HashSet; +import java.util.Set; + import static org.lwjgl.glfw.GLFW.glfwSetErrorCallback; import static org.lwjgl.glfw.GLFW.glfwTerminate; @@ -110,6 +113,15 @@ public final class VulkanSubsystem extends SubsystemClass { /** {@inheritDoc} */ @Override public @NotNull DependencyVector getDependencyVector() { - return new DependencyVector("vulkan", StarOpenSourceVersioningSystem.class, EngineInformation.getVersioningString()); + Set<@NotNull String> dependencies = new HashSet<>(); + dependencies.add("graphics"); + dependencies.add("glfw"); + + return new DependencyVector.Builder() + .setIdentifier(getName()) + .setVersioningSystem(StarOpenSourceVersioningSystem.class) + .setVersion(EngineInformation.getVersioningString()) + .setDependencies(dependencies) + .build(); } } diff --git a/slf4j-compat/src/main/java/de/staropensource/sosengine/slf4j_compat/Slf4jCompatSubsystem.java b/slf4j-compat/src/main/java/de/staropensource/sosengine/slf4j_compat/Slf4jCompatSubsystem.java index cd0a308d..bc9aa736 100644 --- a/slf4j-compat/src/main/java/de/staropensource/sosengine/slf4j_compat/Slf4jCompatSubsystem.java +++ b/slf4j-compat/src/main/java/de/staropensource/sosengine/slf4j_compat/Slf4jCompatSubsystem.java @@ -98,6 +98,10 @@ public class Slf4jCompatSubsystem extends SubsystemClass { /** {@inheritDoc} */ @Override public @NotNull DependencyVector getDependencyVector() { - return new DependencyVector("slf4j-compat", StarOpenSourceVersioningSystem.class, EngineInformation.getVersioningString()); + return new DependencyVector.Builder() + .setIdentifier("slf4j-compat") + .setVersioningSystem(StarOpenSourceVersioningSystem.class) + .setVersion(EngineInformation.getVersioningString()) + .build(); } }