From 656d9ac2088c38921054de0a2c0baaa54258c63f Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sun, 10 Sep 2023 22:17:10 +0800 Subject: [PATCH] Refactor map Attempt methods --- .../MultiverseCore/utils/result/Attempt.java | 30 +++++++++++++++++-- .../MultiverseCore/worldnew/WorldManager.java | 24 +++++++-------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/result/Attempt.java b/src/main/java/com/onarandombox/MultiverseCore/utils/result/Attempt.java index fe375bee..d7c2ff98 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/result/Attempt.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/result/Attempt.java @@ -43,15 +43,31 @@ public interface Attempt { return this; } - default Attempt map(Function> mapper) { + default Attempt map(Function mapper) { if (this instanceof Success) { - return mapper.apply(this.get()); + return new Success<>(mapper.apply(get())); } else { return new Failure<>(getFailureReason(), getFailureMessage()); } } - default Attempt map(Supplier> mapper) { + default Attempt map(Supplier mapper) { + if (this instanceof Success) { + return new Success<>(mapper.get()); + } else { + return new Failure<>(getFailureReason(), getFailureMessage()); + } + } + + default Attempt mapAttempt(Function> mapper) { + if (this instanceof Success) { + return mapper.apply(get()); + } else { + return new Failure<>(getFailureReason(), getFailureMessage()); + } + } + + default Attempt mapAttempt(Supplier> mapper) { if (this instanceof Success) { return mapper.get(); } else { @@ -59,6 +75,14 @@ public interface Attempt { } } + default Attempt transform(UF failureReason) { + if (this instanceof Success) { + return new Success<>(get()); + } else { + return new Failure<>(failureReason, getFailureMessage()); + } + } + default Attempt convertReason(NF failure) { if (this instanceof Success) { return new Success<>(get()); diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java index a5302c35..cb1c4cc5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/WorldManager.java @@ -170,7 +170,7 @@ public class WorldManager { * @return The result of the creation. */ public Attempt createWorld(CreateWorldOptions options) { - return validateCreateWorldOptions(options).map(this::createValidatedWorld); + return validateCreateWorldOptions(options).mapAttempt(this::createValidatedWorld); } private Attempt validateCreateWorldOptions( @@ -221,7 +221,7 @@ public class WorldManager { */ public Attempt importWorld( ImportWorldOptions options) { - return validateImportWorldOptions(options).map(this::doImportWorld); + return validateImportWorldOptions(options).mapAttempt(this::doImportWorld); } private Attempt validateImportWorldOptions( @@ -317,7 +317,7 @@ public class WorldManager { * @return The result of the load. */ public Attempt loadWorld(@NotNull MultiverseWorld world) { - return validateWorldToLoad(world).map(this::doLoadWorld); + return validateWorldToLoad(world).mapAttempt(this::doLoadWorld); } private Attempt validateWorldToLoad( @@ -493,7 +493,7 @@ public class WorldManager { return getLoadedWorld(world).fold( () -> loadWorld(world) .convertReason(DeleteWorldResult.Failure.LOAD_FAILED) - .map(this::deleteWorld), + .mapAttempt(this::deleteWorld), this::deleteWorld); } @@ -508,8 +508,8 @@ public class WorldManager { AtomicReference worldFolder = new AtomicReference<>(); return validateWorldToDelete(world) .peek(worldFolder::set) - .map(() -> removeWorld(world).convertReason(DeleteWorldResult.Failure.REMOVE_FAILED)) - .map(() -> filesManipulator.deleteFolder(worldFolder.get()).fold( + .mapAttempt(() -> removeWorld(world).transform(DeleteWorldResult.Failure.REMOVE_FAILED)) + .mapAttempt(() -> filesManipulator.deleteFolder(worldFolder.get()).fold( exception -> worldActionResult(DeleteWorldResult.Failure.FAILED_TO_DELETE_FOLDER, world.getName(), exception), success -> worldActionResult(world.getName()))); @@ -533,17 +533,16 @@ public class WorldManager { */ public Attempt cloneWorld(@NotNull CloneWorldOptions options) { return cloneWorldValidateWorld(options) - .map(this::cloneWorldCopyFolder) - .map(validatedOptions -> { + .mapAttempt(this::cloneWorldCopyFolder) + .mapAttempt(validatedOptions -> { ImportWorldOptions importWorldOptions = ImportWorldOptions.worldName(validatedOptions.newWorldName()) .environment(validatedOptions.world().getEnvironment()) .generator(validatedOptions.world().getGenerator()); return importWorld(importWorldOptions).convertReason(CloneWorldResult.Failure.IMPORT_FAILED); }) - .map(newWorld -> { + .onSuccess(newWorld -> { cloneWorldTransferData(options, newWorld); saveWorldsConfig(); - return worldActionResult(newWorld); }); } @@ -621,11 +620,10 @@ public class WorldManager { return deleteWorld(world) .convertReason(RegenWorldResult.Failure.DELETE_FAILED) - .map(() -> createWorld(createWorldOptions).convertReason(RegenWorldResult.Failure.CREATE_FAILED)) - .map(newWorld -> { + .mapAttempt(() -> createWorld(createWorldOptions).convertReason(RegenWorldResult.Failure.CREATE_FAILED)) + .onSuccess(newWorld -> { dataTransfer.pasteAllTo(newWorld); saveWorldsConfig(); - return worldActionResult(newWorld); }); }