mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-26 02:01:34 +01:00
Refactor map Attempt methods
This commit is contained in:
parent
2fe6297b1d
commit
656d9ac208
@ -43,15 +43,31 @@ public interface Attempt<T, F extends FailureReason> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
default <N> Attempt<N, F> map(Function<T, Attempt<N, F>> mapper) {
|
default <U> Attempt<U, F> map(Function<? super T, ? extends U> mapper) {
|
||||||
if (this instanceof Success) {
|
if (this instanceof Success) {
|
||||||
return mapper.apply(this.get());
|
return new Success<>(mapper.apply(get()));
|
||||||
} else {
|
} else {
|
||||||
return new Failure<>(getFailureReason(), getFailureMessage());
|
return new Failure<>(getFailureReason(), getFailureMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default <N> Attempt<N, F> map(Supplier<Attempt<N, F>> mapper) {
|
default <U> Attempt<U, F> map(Supplier<? extends U> mapper) {
|
||||||
|
if (this instanceof Success) {
|
||||||
|
return new Success<>(mapper.get());
|
||||||
|
} else {
|
||||||
|
return new Failure<>(getFailureReason(), getFailureMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default <U> Attempt<U, F> mapAttempt(Function<? super T, Attempt<U, F>> mapper) {
|
||||||
|
if (this instanceof Success) {
|
||||||
|
return mapper.apply(get());
|
||||||
|
} else {
|
||||||
|
return new Failure<>(getFailureReason(), getFailureMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default <U> Attempt<U, F> mapAttempt(Supplier<Attempt<U, F>> mapper) {
|
||||||
if (this instanceof Success) {
|
if (this instanceof Success) {
|
||||||
return mapper.get();
|
return mapper.get();
|
||||||
} else {
|
} else {
|
||||||
@ -59,6 +75,14 @@ public interface Attempt<T, F extends FailureReason> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default <UF extends FailureReason> Attempt<T, UF> transform(UF failureReason) {
|
||||||
|
if (this instanceof Success) {
|
||||||
|
return new Success<>(get());
|
||||||
|
} else {
|
||||||
|
return new Failure<>(failureReason, getFailureMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default <NF extends FailureReason> Attempt<T, NF> convertReason(NF failure) {
|
default <NF extends FailureReason> Attempt<T, NF> convertReason(NF failure) {
|
||||||
if (this instanceof Success) {
|
if (this instanceof Success) {
|
||||||
return new Success<>(get());
|
return new Success<>(get());
|
||||||
|
@ -170,7 +170,7 @@ public class WorldManager {
|
|||||||
* @return The result of the creation.
|
* @return The result of the creation.
|
||||||
*/
|
*/
|
||||||
public Attempt<LoadedMultiverseWorld, CreateWorldResult.Failure> createWorld(CreateWorldOptions options) {
|
public Attempt<LoadedMultiverseWorld, CreateWorldResult.Failure> createWorld(CreateWorldOptions options) {
|
||||||
return validateCreateWorldOptions(options).map(this::createValidatedWorld);
|
return validateCreateWorldOptions(options).mapAttempt(this::createValidatedWorld);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Attempt<CreateWorldOptions, CreateWorldResult.Failure> validateCreateWorldOptions(
|
private Attempt<CreateWorldOptions, CreateWorldResult.Failure> validateCreateWorldOptions(
|
||||||
@ -221,7 +221,7 @@ public class WorldManager {
|
|||||||
*/
|
*/
|
||||||
public Attempt<LoadedMultiverseWorld, ImportWorldResult.Failure> importWorld(
|
public Attempt<LoadedMultiverseWorld, ImportWorldResult.Failure> importWorld(
|
||||||
ImportWorldOptions options) {
|
ImportWorldOptions options) {
|
||||||
return validateImportWorldOptions(options).map(this::doImportWorld);
|
return validateImportWorldOptions(options).mapAttempt(this::doImportWorld);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Attempt<ImportWorldOptions, ImportWorldResult.Failure> validateImportWorldOptions(
|
private Attempt<ImportWorldOptions, ImportWorldResult.Failure> validateImportWorldOptions(
|
||||||
@ -317,7 +317,7 @@ public class WorldManager {
|
|||||||
* @return The result of the load.
|
* @return The result of the load.
|
||||||
*/
|
*/
|
||||||
public Attempt<LoadedMultiverseWorld, LoadWorldResult.Failure> loadWorld(@NotNull MultiverseWorld world) {
|
public Attempt<LoadedMultiverseWorld, LoadWorldResult.Failure> loadWorld(@NotNull MultiverseWorld world) {
|
||||||
return validateWorldToLoad(world).map(this::doLoadWorld);
|
return validateWorldToLoad(world).mapAttempt(this::doLoadWorld);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Attempt<MultiverseWorld, LoadWorldResult.Failure> validateWorldToLoad(
|
private Attempt<MultiverseWorld, LoadWorldResult.Failure> validateWorldToLoad(
|
||||||
@ -493,7 +493,7 @@ public class WorldManager {
|
|||||||
return getLoadedWorld(world).fold(
|
return getLoadedWorld(world).fold(
|
||||||
() -> loadWorld(world)
|
() -> loadWorld(world)
|
||||||
.convertReason(DeleteWorldResult.Failure.LOAD_FAILED)
|
.convertReason(DeleteWorldResult.Failure.LOAD_FAILED)
|
||||||
.map(this::deleteWorld),
|
.mapAttempt(this::deleteWorld),
|
||||||
this::deleteWorld);
|
this::deleteWorld);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -508,8 +508,8 @@ public class WorldManager {
|
|||||||
AtomicReference<File> worldFolder = new AtomicReference<>();
|
AtomicReference<File> worldFolder = new AtomicReference<>();
|
||||||
return validateWorldToDelete(world)
|
return validateWorldToDelete(world)
|
||||||
.peek(worldFolder::set)
|
.peek(worldFolder::set)
|
||||||
.map(() -> removeWorld(world).convertReason(DeleteWorldResult.Failure.REMOVE_FAILED))
|
.mapAttempt(() -> removeWorld(world).transform(DeleteWorldResult.Failure.REMOVE_FAILED))
|
||||||
.map(() -> filesManipulator.deleteFolder(worldFolder.get()).fold(
|
.mapAttempt(() -> filesManipulator.deleteFolder(worldFolder.get()).fold(
|
||||||
exception -> worldActionResult(DeleteWorldResult.Failure.FAILED_TO_DELETE_FOLDER,
|
exception -> worldActionResult(DeleteWorldResult.Failure.FAILED_TO_DELETE_FOLDER,
|
||||||
world.getName(), exception),
|
world.getName(), exception),
|
||||||
success -> worldActionResult(world.getName())));
|
success -> worldActionResult(world.getName())));
|
||||||
@ -533,17 +533,16 @@ public class WorldManager {
|
|||||||
*/
|
*/
|
||||||
public Attempt<LoadedMultiverseWorld, CloneWorldResult.Failure> cloneWorld(@NotNull CloneWorldOptions options) {
|
public Attempt<LoadedMultiverseWorld, CloneWorldResult.Failure> cloneWorld(@NotNull CloneWorldOptions options) {
|
||||||
return cloneWorldValidateWorld(options)
|
return cloneWorldValidateWorld(options)
|
||||||
.map(this::cloneWorldCopyFolder)
|
.mapAttempt(this::cloneWorldCopyFolder)
|
||||||
.map(validatedOptions -> {
|
.mapAttempt(validatedOptions -> {
|
||||||
ImportWorldOptions importWorldOptions = ImportWorldOptions.worldName(validatedOptions.newWorldName())
|
ImportWorldOptions importWorldOptions = ImportWorldOptions.worldName(validatedOptions.newWorldName())
|
||||||
.environment(validatedOptions.world().getEnvironment())
|
.environment(validatedOptions.world().getEnvironment())
|
||||||
.generator(validatedOptions.world().getGenerator());
|
.generator(validatedOptions.world().getGenerator());
|
||||||
return importWorld(importWorldOptions).convertReason(CloneWorldResult.Failure.IMPORT_FAILED);
|
return importWorld(importWorldOptions).convertReason(CloneWorldResult.Failure.IMPORT_FAILED);
|
||||||
})
|
})
|
||||||
.map(newWorld -> {
|
.onSuccess(newWorld -> {
|
||||||
cloneWorldTransferData(options, newWorld);
|
cloneWorldTransferData(options, newWorld);
|
||||||
saveWorldsConfig();
|
saveWorldsConfig();
|
||||||
return worldActionResult(newWorld);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -621,11 +620,10 @@ public class WorldManager {
|
|||||||
|
|
||||||
return deleteWorld(world)
|
return deleteWorld(world)
|
||||||
.convertReason(RegenWorldResult.Failure.DELETE_FAILED)
|
.convertReason(RegenWorldResult.Failure.DELETE_FAILED)
|
||||||
.map(() -> createWorld(createWorldOptions).convertReason(RegenWorldResult.Failure.CREATE_FAILED))
|
.mapAttempt(() -> createWorld(createWorldOptions).convertReason(RegenWorldResult.Failure.CREATE_FAILED))
|
||||||
.map(newWorld -> {
|
.onSuccess(newWorld -> {
|
||||||
dataTransfer.pasteAllTo(newWorld);
|
dataTransfer.pasteAllTo(newWorld);
|
||||||
saveWorldsConfig();
|
saveWorldsConfig();
|
||||||
return worldActionResult(newWorld);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user