From 4f7a741088773202d9bdb9cbb19dce603e286878 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Wed, 4 Sep 2024 22:26:13 +0200 Subject: [PATCH] The unmet dependencies map is now a list --- .../staropensource/sosengine/base/Engine.java | 13 ++++----- .../UnmetDependenciesException.java | 18 +++++-------- .../base/utility/DependencyResolver.java | 27 ++++++++++--------- .../utility/DependencyResolverTest.java | 12 ++++----- 4 files changed, 34 insertions(+), 36 deletions(-) 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 0e0558f0..a2715731 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/Engine.java +++ b/base/src/main/java/de/staropensource/sosengine/base/Engine.java @@ -413,16 +413,17 @@ public final class Engine extends SubsystemClass { order.add((DependencySubsystemVector) vector); } catch (Throwable throwable) { if (throwable instanceof UnmetDependenciesException exception) { - Map<@NotNull DependencyVector, @NotNull String> unmetDependencies = exception.getUnmetDependencies(); + List<@NotNull String> unmetDependencies = exception.getUnmetDependencies(); StringBuilder list = new StringBuilder(); - for (DependencyVector vector : unmetDependencies.keySet()) - list.append("- ") - .append(vector.getIdentifier()) - .append(": ") - .append(unmetDependencies.get(vector)); + for (String error : unmetDependencies) + list + .append("\n") + .append("- ") + .append(error); logger.crash("Found unresolved dependencies:" + list, throwable); + return; } logger.crash("An error occurred trying to resolve subsystem dependencies: " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage())); throw throwable; diff --git a/base/src/main/java/de/staropensource/sosengine/base/exception/dependency/UnmetDependenciesException.java b/base/src/main/java/de/staropensource/sosengine/base/exception/dependency/UnmetDependenciesException.java index dc63e696..95c54516 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/exception/dependency/UnmetDependenciesException.java +++ b/base/src/main/java/de/staropensource/sosengine/base/exception/dependency/UnmetDependenciesException.java @@ -24,7 +24,7 @@ import de.staropensource.sosengine.base.utility.DependencyResolver; import lombok.Getter; import org.jetbrains.annotations.NotNull; -import java.util.Map; +import java.util.List; /** * Thrown when the {@link DependencyResolver} cannot resolve one @@ -37,30 +37,24 @@ import java.util.Map; public class UnmetDependenciesException extends Exception { /** * Contains the unmet dependencies list supplied to the constructor. - *

- * The key contains the {@link DependencyVector} that - * has unmet dependencies, while the value contains the error. * - * @since v1-alpha1 + * @since v1-alpha4 * -- GETTER -- * Returns the unmet dependencies list supplied to the constructor. - *

- * The key contains the {@link DependencyVector} that - * has unmet dependencies, while the value contains the error. * * @return unmet dependencies list - * @since v1-alpha1 + * @since v1-alpha4 */ - private final @NotNull Map<@NotNull DependencyVector, @NotNull String> unmetDependencies; + private final @NotNull List<@NotNull String> unmetDependencies; /** * Constructs this exception. * * @param unmetDependencies map of all unmet dependencies * @see #unmetDependencies - * @since v1-alpha1 + * @since v1-alpha4 */ - public UnmetDependenciesException(@NotNull Map<@NotNull DependencyVector, @NotNull String> unmetDependencies) { + public UnmetDependenciesException(@NotNull List<@NotNull String> unmetDependencies) { this.unmetDependencies = unmetDependencies; } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java b/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java index 0dbe670c..12379569 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/DependencyResolver.java @@ -79,9 +79,13 @@ public final class DependencyResolver { * * @param vector {@link DependencyVector} to add * @return itself + * @throws IllegalArgumentException if the specified vector has been added already * @since v1-alpha1 */ - public synchronized @NotNull DependencyResolver addVector(@NotNull DependencyVector vector) { + public synchronized @NotNull DependencyResolver addVector(@NotNull DependencyVector vector) throws IllegalArgumentException { + if (vectors.contains(vector)) + throw new IllegalArgumentException("The specified vector has been added already"); + try { vectors.add(vector); } catch (IllegalArgumentException ignored) {} @@ -118,19 +122,17 @@ public final class DependencyResolver { * Throws an exception when detecting an unmet dependency or a dependency cycle. * * @return itself - * @throws IllegalStateException when encountering an invalid vector + * @throws IllegalStateException when encountering an invalid vector * @throws UnmetDependenciesException when dependencies are unmet - * @since v1-alpha1 + * @throws DependencyCycleException when a circular dependency is found + * @since v1-alpha4 */ public synchronized @NotNull DependencyResolver resolve() throws IllegalStateException, UnmetDependenciesException { - Map<@NotNull DependencyVector, @NotNull String> unmetDependencies = new HashMap<>(); + List<@NotNull String> unmetDependencies = new ArrayList<>(); List<@NotNull String> output; - for (DependencyVector vector : vectors) { - output = resolveVector(vector, new LinkedHashSet<>()); - for (String item : output) - unmetDependencies.put(vector, item); - } + for (DependencyVector vector : vectors) + unmetDependencies.addAll(resolveVector(vector, new LinkedHashSet<>())); if (!unmetDependencies.isEmpty()) throw new UnmetDependenciesException(unmetDependencies); @@ -144,11 +146,12 @@ public final class DependencyResolver { * Throws an exception when detecting an unmet dependency or a dependency cycle. * * @return list of unmet dependencies - * @throws IllegalStateException when encountering an invalid dependency or provider - * @throws Exception when some unknown error occurs + * @throws IllegalStateException when encountering an invalid dependency or provider + * @throws DependencyCycleException when a circular dependency is found + * @throws Exception when some unknown error occurs * @since v1-alpha4 */ - private @NotNull List<@NotNull String> resolveVector(@NotNull DependencyVector vector, @NotNull LinkedHashSet<@NotNull String> vectorsDependencyStack) throws IllegalStateException { + private @NotNull List<@NotNull String> resolveVector(@NotNull DependencyVector vector, @NotNull LinkedHashSet<@NotNull String> vectorsDependencyStack) throws IllegalStateException, DependencyCycleException { List<@NotNull String> unmetDependencies = new ArrayList<>(); vectorsDependencyStack.add(vector.getIdentifier()); diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java index 03446448..a6a6c069 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java @@ -273,8 +273,8 @@ class DependencyResolverTest extends TestBase { resolver.resolve(); } catch (UnmetDependenciesException exception) { getLogger().error("Dependency resolution failed in testResolve(layers=" + layers + "):"); - for (DependencyVector vector : exception.getUnmetDependencies().keySet()) - getLogger().error("-> " + vector.getIdentifier() + "=" + vector.getVersion() + ": " + exception.getUnmetDependencies().get(vector)); + for (String error : exception.getUnmetDependencies()) + getLogger().error("-> " + error); assertEquals("Please ignore this, this just exists to trigger an error", "", "Dependency resolution failed in testResolve(layers=" + layers + "): See logs"); } catch (DependencyCycleException exception) { @@ -497,8 +497,8 @@ class DependencyResolverTest extends TestBase { resolver.resolve(); } catch (UnmetDependenciesException exception) { getLogger().error("Dependency resolution failed in testResolve(layers=" + layers + ") (great!):"); - for (DependencyVector vector : exception.getUnmetDependencies().keySet()) - getLogger().error("-> " + vector.getIdentifier() + "=" + vector.getVersion() + ": " + exception.getUnmetDependencies().get(vector)); + for (String error : exception.getUnmetDependencies()) + getLogger().error("-> " + error); return; } catch (DependencyCycleException exception) { @@ -613,8 +613,8 @@ class DependencyResolverTest extends TestBase { resolver.resolve(); } catch (UnmetDependenciesException exception) { getLogger().error("Dependency resolution failed in testResolveDependencyCycle():"); - for (DependencyVector vector : exception.getUnmetDependencies().keySet()) - getLogger().error("-> " + vector.getIdentifier() + "=" + vector.getVersion() + ": " + exception.getUnmetDependencies().get(vector)); + for (String error : exception.getUnmetDependencies()) + getLogger().error("-> " + error); assertEquals("Please ignore this, this just exists to trigger an error", "", "Dependency resolution failed in testResolveDependencyCycle(): See logs"); } catch (DependencyCycleException exception) {