Allow passing a supplier to command context

This fixes #116
This commit is contained in:
Alexander Söderberg 2020-11-25 12:22:04 +01:00 committed by Alexander Söderberg
parent e9134efad6
commit d8f0b1a47e

View file

@ -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<C> {
return this.<T>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 <T> Argument type
* @return Argument, or supplied default value
* @since 1.2.0
*/
public <T> @Nullable T getOrDefault(
final @NonNull String key,
final @NonNull Supplier<@Nullable T> defaultSupplier
) {
return this.<T>getOptional(key).orElseGet(defaultSupplier);
}
/**
* Get the raw input. This should only be used when {@link #isSuggestions()} is {@code true}
*