Skip to content

Commit

Permalink
feat(core): refactor numerical parsing (#649)
Browse files Browse the repository at this point in the history
- the numerical parsers now extend `NumberParser`
- ranges have been moved into a `Range` object

---------

Co-authored-by: Jason Penilla <[email protected]>
  • Loading branch information
Citymonstret and jpenilla authored Jan 20, 2024
1 parent 5d79804 commit e35de97
Show file tree
Hide file tree
Showing 18 changed files with 725 additions and 492 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@

import cloud.commandframework.CommandComponent;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.parser.ParserDescriptor;
import cloud.commandframework.arguments.suggestion.BlockingSuggestionProvider;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.context.CommandInput;
import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.Objects;
import cloud.commandframework.types.range.ByteRange;
import cloud.commandframework.types.range.Range;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;

@API(status = API.Status.STABLE)
public final class ByteParser<C> implements ArgumentParser<C, Byte>, BlockingSuggestionProvider.Strings<C> {
public final class ByteParser<C> extends NumberParser<C, Byte, ByteRange> implements BlockingSuggestionProvider.Strings<C> {

/**
* Constant for the default/unset minimum value.
Expand Down Expand Up @@ -111,26 +111,22 @@ public final class ByteParser<C> implements ArgumentParser<C, Byte>, BlockingSug
return CommandComponent.<C, Byte>builder().parser(byteParser());
}

private final byte min;
private final byte max;

/**
* Construct a new byte parser
*
* @param min Minimum acceptable value
* @param max Maximum acceptable value
*/
public ByteParser(final byte min, final byte max) {
this.min = min;
this.max = max;
super(Range.byteRange(min, max));
}

@Override
public @NonNull ArgumentParseResult<Byte> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull CommandInput commandInput
) {
if (!commandInput.isValidByte(this.min, this.max)) {
if (!commandInput.isValidByte(this.range())) {
return ArgumentParseResult.failure(new ByteParseException(
commandInput.peekString(),
this,
Expand All @@ -140,61 +136,28 @@ public ByteParser(final byte min, final byte max) {
return ArgumentParseResult.success(commandInput.readByte());
}

/**
* Returns the minimum value accepted by this parser.
*
* @return min value
*/
public byte min() {
return this.min;
}

/**
* Returns the maximum value accepted by this parser.
*
* @return max value
*/
public byte max() {
return this.max;
}

/**
* Get whether this parser has a maximum set.
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
*
* @return whether the parser has a maximum set
* @since 1.5.0
*/
@Override
public boolean hasMax() {
return this.max != DEFAULT_MAXIMUM;
return this.range().maxByte() != DEFAULT_MAXIMUM;
}

/**
* Get whether this parser has a minimum set.
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
*
* @return whether the parser has a maximum set
* @since 1.5.0
*/
@Override
public boolean hasMin() {
return this.min != DEFAULT_MINIMUM;
return this.range().minByte() != DEFAULT_MINIMUM;
}

@Override
public @NonNull Iterable<@NonNull String> stringSuggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull CommandInput input
) {
return IntegerParser.getSuggestions(this.min, this.max, input);
return IntegerParser.getSuggestions(this.range(), input);
}


@API(status = API.Status.STABLE)
public static final class ByteParseException extends NumberParseException {


private final ByteParser<?> parser;

/**
* Create a new {@link ByteParseException}.
*
Expand All @@ -209,40 +172,12 @@ public ByteParseException(
final @NonNull ByteParser<?> parser,
final @NonNull CommandContext<?> commandContext
) {
super(input, parser.min, parser.max, ByteParser.class, commandContext);
this.parser = parser;
}

@Override
public boolean hasMin() {
return this.parser.hasMin();
}

@Override
public boolean hasMax() {
return this.parser.hasMax();
super(input, parser, commandContext);
}

@Override
public @NonNull String numberType() {
return "byte";
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final ByteParseException that = (ByteParseException) o;
return this.parser.equals(that.parser);
}

@Override
public int hashCode() {
return Objects.hash(this.parser);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@

import cloud.commandframework.CommandComponent;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.parser.ParserDescriptor;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.context.CommandInput;
import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.Objects;
import cloud.commandframework.types.range.DoubleRange;
import cloud.commandframework.types.range.Range;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;

@API(status = API.Status.STABLE)
public final class DoubleParser<C> implements ArgumentParser<C, Double> {
public final class DoubleParser<C> extends NumberParser<C, Double, DoubleRange> {

/**
* Constant for the default/unset minimum value.
Expand Down Expand Up @@ -110,26 +110,22 @@ public final class DoubleParser<C> implements ArgumentParser<C, Double> {
return CommandComponent.<C, Double>builder().parser(doubleParser());
}

private final double min;
private final double max;

/**
* Construct a new double parser
*
* @param min Minimum acceptable value
* @param max Maximum acceptable value
*/
public DoubleParser(final double min, final double max) {
this.min = min;
this.max = max;
super(Range.doubleRange(min, max));
}

@Override
public @NonNull ArgumentParseResult<Double> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull CommandInput commandInput
) {
if (!commandInput.isValidDouble(this.min, this.max)) {
if (!commandInput.isValidDouble(this.range())) {
return ArgumentParseResult.failure(new DoubleParseException(
commandInput.peekString(),
this,
Expand All @@ -139,53 +135,20 @@ public DoubleParser(final double min, final double max) {
return ArgumentParseResult.success(commandInput.readDouble());
}

/**
* Returns the minimum value accepted by this parser.
*
* @return min value
*/
public double min() {
return this.min;
}

/**
* Returns the maximum value accepted by this parser.
*
* @return max value
*/
public double max() {
return this.max;
}

/**
* Get whether this parser has a maximum set.
* This will compare the parser's maximum to {@link #DEFAULT_MAXIMUM}.
*
* @return whether the parser has a maximum set
* @since 1.5.0
*/
@Override
public boolean hasMax() {
return this.max != DEFAULT_MAXIMUM;
return this.range().maxDouble() != DEFAULT_MAXIMUM;
}

/**
* Get whether this parser has a minimum set.
* This will compare the parser's minimum to {@link #DEFAULT_MINIMUM}.
*
* @return whether the parser has a maximum set
* @since 1.5.0
*/
@Override
public boolean hasMin() {
return this.min != DEFAULT_MINIMUM;
return this.range().minDouble() != DEFAULT_MINIMUM;
}


@API(status = API.Status.STABLE)
public static final class DoubleParseException extends NumberParseException {


private final DoubleParser<?> parser;

/**
* Create a new {@link DoubleParseException}.
*
Expand All @@ -200,40 +163,12 @@ public DoubleParseException(
final @NonNull DoubleParser<?> parser,
final @NonNull CommandContext<?> commandContext
) {
super(input, parser.min, parser.max, DoubleParser.class, commandContext);
this.parser = parser;
}

@Override
public boolean hasMin() {
return this.parser.hasMin();
}

@Override
public boolean hasMax() {
return this.parser.hasMax();
super(input, parser, commandContext);
}

@Override
public @NonNull String numberType() {
return "double";
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final DoubleParseException that = (DoubleParseException) o;
return this.parser.equals(that.parser);
}

@Override
public int hashCode() {
return Objects.hash(this.parser);
}
}
}
Loading

0 comments on commit e35de97

Please sign in to comment.