From 20812daf887198e8a29c87e699a02c2fe03705f1 Mon Sep 17 00:00:00 2001 From: Rollczi Date: Mon, 20 Dec 2021 01:49:57 +0100 Subject: [PATCH] Change return type in LiteComponentFactory#createSection() to Result. --- .../dev/rollczi/litecommands/LiteCommandsBuilder.java | 4 ++-- .../litecommands/component/LiteComponentFactory.java | 10 +++++----- .../litecommands/component/LiteComponentFactoryTest.kt | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java index 6668696a3..3c97af897 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/LiteCommandsBuilder.java @@ -171,12 +171,12 @@ public LiteCommands register() { for (Object instance : commandInstances) { registerResolvers.register(factory.createSection(instance) - .orThrow(() -> new IllegalArgumentException(instance.getClass() + " instance isn't a section"))); + .orElseThrow(RuntimeException::new)); } for (Class commandClass : commandClasses) { registerResolvers.register(factory.createSection(commandClass) - .orThrow(() -> new IllegalArgumentException(commandClass + " class isn't a section"))); + .orElseThrow(RuntimeException::new)); } for (LiteComponent resolver : registerResolvers.getResolvers().values()) { diff --git a/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteComponentFactory.java b/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteComponentFactory.java index e0e80e6b6..b429d732a 100644 --- a/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteComponentFactory.java +++ b/litecommands-core/src/main/java/dev/rollczi/litecommands/component/LiteComponentFactory.java @@ -22,7 +22,7 @@ public LiteComponentFactory(Logger logger, Injector injector, AnnotationParser p this.parser = parser; } - public Option createSection(Object sectionInstance) { + public Result createSection(Object sectionInstance) { Class sectionClass = sectionInstance.getClass(); return parser.parse(sectionClass).map(scope -> { @@ -35,7 +35,7 @@ public Option createSection(Object sectionInstance) { Set innerSections = PandaStream.of(sectionClass.getClasses()) .concat(sectionClass.getDeclaredClasses()) .distinct() - .mapOpt(innerClass -> Result.attempt(Throwable.class, () -> createSection(innerClass)) + .map(innerClass -> createSection(innerClass) .orElseThrow(error -> new RuntimeException("Can't create inner class " + innerClass, error))) .toSet(); @@ -44,11 +44,11 @@ public Option createSection(Object sectionInstance) { .resolvers(innerSections) .resolvers(executions) .build(); - }); + }).toResult(new RuntimeException(sectionInstance.getClass() + " class isn't a section")); } - public Option createSection(Class sectionClass) { - return Option.attempt(Throwable.class, () -> injector.newInstance(sectionClass)) + public Result createSection(Class sectionClass) { + return Result.attempt(Throwable.class, () -> injector.newInstance(sectionClass)) .flatMap(this::createSection); } diff --git a/litecommands-core/src/test/java/dev/rollczi/litecommands/component/LiteComponentFactoryTest.kt b/litecommands-core/src/test/java/dev/rollczi/litecommands/component/LiteComponentFactoryTest.kt index 841d9b824..96b6b5597 100644 --- a/litecommands-core/src/test/java/dev/rollczi/litecommands/component/LiteComponentFactoryTest.kt +++ b/litecommands-core/src/test/java/dev/rollczi/litecommands/component/LiteComponentFactoryTest.kt @@ -23,7 +23,7 @@ class LiteComponentFactoryTest : LiteCommandsSpec() { val sectionOption = factory.createSection(TestCommand::class.java) - assertTrue(sectionOption.isPresent) + assertTrue(sectionOption.isOk) val section = sectionOption.get() val scope = section.scope