From d8f0b1a47e64320faebff4f2db7f91d787cc7a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Wed, 25 Nov 2020 12:22:04 +0100 Subject: [PATCH] :sparkles: Allow passing a supplier to command context This fixes #116 --- .../context/CommandContext.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java index e60da783..a74a6733 100644 --- a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java +++ b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java @@ -38,6 +38,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Optional; +import java.util.function.Supplier; /** * Command context used to assist in the parsing of commands @@ -238,6 +239,22 @@ public final class CommandContext { return this.getOptional(key).orElse(defaultValue); } + /** + * Get a value if it exists, else return the value supplied by the given supplier + * + * @param key Argument key + * @param defaultSupplier Supplier of default value + * @param Argument type + * @return Argument, or supplied default value + * @since 1.2.0 + */ + public @Nullable T getOrDefault( + final @NonNull String key, + final @NonNull Supplier<@Nullable T> defaultSupplier + ) { + return this.getOptional(key).orElseGet(defaultSupplier); + } + /** * Get the raw input. This should only be used when {@link #isSuggestions()} is {@code true} *