From 38270f8ee221ebdcfe92c813a32ab89d0a4b60c5 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Mon, 22 Jan 2024 00:42:16 -0700 Subject: [PATCH] Add a couple helper methods to Either (#654) --- .../cloud/commandframework/types/Either.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/cloud-core/src/main/java/cloud/commandframework/types/Either.java b/cloud-core/src/main/java/cloud/commandframework/types/Either.java index 4d2cc25ae..498246e9c 100644 --- a/cloud-core/src/main/java/cloud/commandframework/types/Either.java +++ b/cloud-core/src/main/java/cloud/commandframework/types/Either.java @@ -25,6 +25,7 @@ import cloud.commandframework.internal.ImmutableImpl; import java.util.Optional; +import java.util.function.Function; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; import org.immutables.value.Value; @@ -80,4 +81,42 @@ public interface Either { * @return the second value */ @NonNull Optional fallback(); + + /** + * Extract {@link #primary()}, or if it's not present, map {@link #fallback()} to {@code U}. + * + * @param mapFallback function mapping the fallback type to {@code U} + * @return extracted or mapped value + */ + default @NonNull U primaryOrMapFallback(final @NonNull Function mapFallback) { + return this.primary().orElseGet(() -> mapFallback.apply(this.fallback().get())); + } + + /** + * Extract {@link #fallback()}, or if it's not present, map {@link #primary()} to {@code V}. + * + * @param mapPrimary function mapping the primary type to {@code V} + * @return extracted or mapped value + */ + default @NonNull V fallbackOrMapPrimary(final @NonNull Function mapPrimary) { + return this.fallback().orElseGet(() -> mapPrimary.apply(this.primary().get())); + } + + /** + * Extract {@link #primary()} or {@link #fallback()}, in that order, applying the appropriate function + * to map the result to {@code R}. + * + * @param mapPrimary function mapping the primary type to {@code R} + * @param mapFallback function mapping the fallback type to {@code R} + * @param mapped type + * @return mapped value + */ + default @NonNull R mapEither( + final @NonNull Function mapPrimary, + final @NonNull Function mapFallback + ) { + return this.primary() + .map(mapPrimary) + .orElseGet(() -> this.fallback().map(mapFallback).get()); + } }