diff --git a/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java b/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java index 4d428b12..89ccc3a6 100644 --- a/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java +++ b/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java @@ -165,7 +165,7 @@ public final class StandardParserRegistry implements ParserRegistry { return new BooleanArgument.BooleanParser<>(liberal); }); this.registerParserSupplier(TypeToken.get(UUID.class), options -> new UUIDArgument.UUIDParser<>()); - this.registerParserSupplier(TypeToken.get(Duration.class), options -> new DurationArgument.DurationParser<>()); + this.registerParserSupplier(TypeToken.get(Duration.class), options -> new DurationArgument.Parser<>()); } private static boolean isPrimitive(final @NonNull TypeToken type) { diff --git a/cloud-core/src/main/java/cloud/commandframework/arguments/standard/DurationArgument.java b/cloud-core/src/main/java/cloud/commandframework/arguments/standard/DurationArgument.java index 674fb1b6..b8cb43d2 100644 --- a/cloud-core/src/main/java/cloud/commandframework/arguments/standard/DurationArgument.java +++ b/cloud-core/src/main/java/cloud/commandframework/arguments/standard/DurationArgument.java @@ -46,13 +46,12 @@ import java.util.stream.Stream; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; - /** - * Parses java.time.Duration from a 1d2h3m4s format. - * @param Command sender type + * Parses {@link Duration} from a 1d2h3m4s format. + * + * @param sender type * @since 1.7.0 */ -@SuppressWarnings("unused") public final class DurationArgument extends CommandArgument { /** @@ -71,7 +70,7 @@ public final class DurationArgument extends CommandArgument { super( required, name, - new DurationArgument.DurationParser<>(), + new Parser<>(), defaultValue, Duration.class, suggestionsProvider, @@ -80,74 +79,112 @@ public final class DurationArgument extends CommandArgument { } /** - * Create a new builder + * Create a new {@link Builder}. * - * @param name Name of the component - * @param Command sender type - * @return Created builder + * @param name argument name + * @param sender type + * @return new builder * @since 1.7.0 */ - public static @NonNull Builder newBuilder(final @NonNull String name) { + public static @NonNull Builder builder(final @NonNull String name) { return new Builder<>(name); } /** - * Create a new required command component + * Create a new required {@link DurationArgument}. * - * @param name Component name - * @param Command sender type - * @return Created component + * @param name argument name + * @param sender type + * @return built argument * @since 1.7.0 */ - public static @NonNull CommandArgument of(final @NonNull String name) { - return DurationArgument.newBuilder(name).asRequired().build(); + public static @NonNull DurationArgument of(final @NonNull String name) { + return DurationArgument.builder(name).asRequired().build(); } /** - * Create a new optional command component + * Create a new optional {@link DurationArgument}. * - * @param name Component name - * @param Command sender type - * @return Created component + * @param name argument name + * @param sender type + * @return built argument * @since 1.7.0 */ - public static @NonNull CommandArgument optional(final @NonNull String name) { - return DurationArgument.newBuilder(name).asOptional().build(); + public static @NonNull DurationArgument optional(final @NonNull String name) { + return DurationArgument.builder(name).asOptional().build(); } /** - * Create a new required command component with a default value + * Create a new optional {@link DurationArgument} with the specified default value. * - * @param name Component name - * @param defaultDuration Default duration - * @param Command sender type - * @return Created component + * @param name argument name + * @param defaultDuration default duration + * @param sender type + * @return built argument * @since 1.7.0 */ - public static @NonNull CommandArgument optional( + public static @NonNull DurationArgument optional( final @NonNull String name, final @NonNull String defaultDuration ) { - return DurationArgument.newBuilder(name).asOptionalWithDefault(defaultDuration).build(); + return DurationArgument.builder(name).asOptionalWithDefault(defaultDuration).build(); + } + + /** + * Create a new optional {@link DurationArgument} with the specified default value. + * + * @param name argument name + * @param defaultDuration default duration + * @param sender type + * @return built argument + * @since 1.7.0 + */ + public static @NonNull DurationArgument optional( + final @NonNull String name, + final @NonNull Duration defaultDuration + ) { + return DurationArgument.builder(name).asOptionalWithDefault(defaultDuration).build(); } - public static final class Builder extends CommandArgument.Builder { + /** + * Builder for {@link DurationArgument}. + * + * @param sender type + * @since 1.7.0 + */ + public static final class Builder extends TypedBuilder> { private Builder(final @NonNull String name) { super(Duration.class, name); } /** - * Builder a new boolean component + * Sets the command argument to be optional, with the specified default value. * - * @return Constructed component + * @param defaultValue default value + * @return this builder + * @see CommandArgument.Builder#asOptionalWithDefault(String) + * @since 1.7.0 + */ + public @NonNull Builder asOptionalWithDefault(final @NonNull Duration defaultValue) { + return this.asOptionalWithDefault(defaultValue.getSeconds() + "s"); + } + + /** + * Create a new {@link DurationArgument} from this builder. + * + * @return built argument * @since 1.7.0 */ @Override public @NonNull DurationArgument build() { - return new DurationArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), - this.getSuggestionsProvider(), this.getDefaultDescription() + return new DurationArgument<>( + this.isRequired(), + this.getName(), + this.getDefaultValue(), + this.getSuggestionsProvider(), + this.getDefaultDescription() ); } @@ -155,11 +192,12 @@ public final class DurationArgument extends CommandArgument { /** - * Represents a duration parser. + * Parser for {@link Duration}. + * + * @param sender type * @since 1.7.0 - * @param Command sender type */ - public static final class DurationParser implements ArgumentParser { + public static final class Parser implements ArgumentParser { @Override public @NonNull ArgumentParseResult parse( @@ -208,12 +246,7 @@ public final class DurationArgument extends CommandArgument { return ArgumentParseResult.success(duration); } - /** - * Provides suggestions for Durations. - * @since 1.7.0 - */ @Override - @SuppressWarnings("MixedMutabilityReturnType") public @NonNull List<@NonNull String> suggestions( final @NonNull CommandContext commandContext, final @NonNull String input @@ -244,7 +277,8 @@ public final class DurationArgument extends CommandArgument { } /** - * Represents a duration parse exception. + * Failure exception for {@link Parser}. + * * @since 1.7.0 */ public static final class DurationParseException extends ParserException { @@ -253,10 +287,10 @@ public final class DurationArgument extends CommandArgument { private final String input; /** - * Construct a new Duration parse exception + * Construct a new {@link DurationParseException}. * - * @param input String input - * @param context Command context + * @param input input string + * @param context command context * @since 1.7.0 */ public DurationParseException( @@ -264,7 +298,7 @@ public final class DurationArgument extends CommandArgument { final @NonNull CommandContext context ) { super( - DurationArgument.DurationParser.class, + Parser.class, context, StandardCaptionKeys.ARGUMENT_PARSE_FAILURE_DURATION, CaptionVariable.of("input", input) @@ -273,9 +307,9 @@ public final class DurationArgument extends CommandArgument { } /** - * Get the supplied input + * Get the supplied input string. * - * @return String value + * @return input string * @since 1.7.0 */ public @NonNull String getInput() {