Start adding some standard component types
This commit is contained in:
parent
667e7cc49d
commit
62abfa5e95
9 changed files with 432 additions and 15 deletions
|
|
@ -29,7 +29,7 @@ import com.intellectualsites.commands.context.CommandContext;
|
||||||
import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
|
import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
|
||||||
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
|
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
|
||||||
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
||||||
import com.intellectualsites.commands.parser.ComponentParseResult;
|
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||||
import com.intellectualsites.commands.sender.CommandSender;
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
@ -85,7 +85,7 @@ public class CommandTree<C extends CommandSender> {
|
||||||
|
|
||||||
private Optional<Command<C>> parseCommand(@Nonnull final CommandContext<C> commandContext,
|
private Optional<Command<C>> parseCommand(@Nonnull final CommandContext<C> commandContext,
|
||||||
@Nonnull final Queue<String> commandQueue,
|
@Nonnull final Queue<String> commandQueue,
|
||||||
@Nonnull final Node<CommandComponent<C, ?>> root) throws NoSuchCommandException {
|
@Nonnull final Node<CommandComponent<C, ?>> root) {
|
||||||
if (!this.isPermitted(commandContext.getCommandSender(), root)) {
|
if (!this.isPermitted(commandContext.getCommandSender(), root)) {
|
||||||
/* TODO: Send not allowed */
|
/* TODO: Send not allowed */
|
||||||
throw new RuntimeException("Nope!");
|
throw new RuntimeException("Nope!");
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
package com.intellectualsites.commands.components;
|
package com.intellectualsites.commands.components;
|
||||||
|
|
||||||
import com.intellectualsites.commands.Command;
|
import com.intellectualsites.commands.Command;
|
||||||
import com.intellectualsites.commands.parser.ComponentParser;
|
import com.intellectualsites.commands.components.parser.ComponentParser;
|
||||||
import com.intellectualsites.commands.sender.CommandSender;
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
@ -38,6 +38,7 @@ import java.util.regex.Pattern;
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
* @param <T> The type that the component parses into
|
* @param <T> The type that the component parses into
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class CommandComponent<C extends CommandSender, T> implements Comparable<CommandComponent<?, ?>> {
|
public class CommandComponent<C extends CommandSender, T> implements Comparable<CommandComponent<?, ?>> {
|
||||||
|
|
||||||
private static final Pattern NAME_PATTERN = Pattern.compile("[A-Za-z0-9]+");
|
private static final Pattern NAME_PATTERN = Pattern.compile("[A-Za-z0-9]+");
|
||||||
|
|
@ -63,7 +64,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
|
|
||||||
private Command<C> owningCommand;
|
private Command<C> owningCommand;
|
||||||
|
|
||||||
CommandComponent(final boolean required, @Nonnull final String name,
|
public CommandComponent(final boolean required, @Nonnull final String name,
|
||||||
@Nonnull final ComponentParser<C, T> parser) {
|
@Nonnull final ComponentParser<C, T> parser) {
|
||||||
this.required = required;
|
this.required = required;
|
||||||
this.name = Objects.requireNonNull(name, "Name may not be null");
|
this.name = Objects.requireNonNull(name, "Name may not be null");
|
||||||
|
|
@ -78,7 +79,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
*
|
*
|
||||||
* @param clazz Argument class
|
* @param clazz Argument class
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
* @param <T> Argument Type
|
* @param <T> Argument Type. Used to make the compiler happy.
|
||||||
* @return Component builder
|
* @return Component builder
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
|
@ -185,13 +186,13 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
* @param <T> Component value type
|
* @param <T> Component value type
|
||||||
*/
|
*/
|
||||||
public static final class Builder<C extends CommandSender, T> {
|
public static class Builder<C extends CommandSender, T> {
|
||||||
|
|
||||||
private String name;
|
protected String name;
|
||||||
private boolean required = true;
|
protected boolean required = true;
|
||||||
private ComponentParser<C, T> parser;
|
protected ComponentParser<C, T> parser;
|
||||||
|
|
||||||
private Builder() {
|
protected Builder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@
|
||||||
package com.intellectualsites.commands.components;
|
package com.intellectualsites.commands.components;
|
||||||
|
|
||||||
import com.intellectualsites.commands.context.CommandContext;
|
import com.intellectualsites.commands.context.CommandContext;
|
||||||
import com.intellectualsites.commands.parser.ComponentParseResult;
|
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||||
import com.intellectualsites.commands.parser.ComponentParser;
|
import com.intellectualsites.commands.components.parser.ComponentParser;
|
||||||
import com.intellectualsites.commands.sender.CommandSender;
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
// SOFTWARE.
|
// SOFTWARE.
|
||||||
//
|
//
|
||||||
package com.intellectualsites.commands.parser;
|
package com.intellectualsites.commands.components.parser;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
// SOFTWARE.
|
// SOFTWARE.
|
||||||
//
|
//
|
||||||
package com.intellectualsites.commands.parser;
|
package com.intellectualsites.commands.components.parser;
|
||||||
|
|
||||||
import com.intellectualsites.commands.context.CommandContext;
|
import com.intellectualsites.commands.context.CommandContext;
|
||||||
import com.intellectualsites.commands.sender.CommandSender;
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
@ -0,0 +1,170 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands.components.standard;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.components.CommandComponent;
|
||||||
|
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||||
|
import com.intellectualsites.commands.components.parser.ComponentParser;
|
||||||
|
import com.intellectualsites.commands.context.CommandContext;
|
||||||
|
import com.intellectualsites.commands.exceptions.parsing.NumberParseException;
|
||||||
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class ByteComponent<C extends CommandSender> extends CommandComponent<C, Byte> {
|
||||||
|
|
||||||
|
private final byte min;
|
||||||
|
private final byte max;
|
||||||
|
|
||||||
|
private ByteComponent(final boolean required, @Nonnull final String name, final byte min, final byte max) {
|
||||||
|
super(required, name, new ByteParser<>(min, max));
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static <C extends CommandSender> Builder<C> newBuilder() {
|
||||||
|
return new Builder<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static <C extends CommandSender> CommandComponent<C, Byte> required(@Nonnull final String name) {
|
||||||
|
return ByteComponent.<C>newBuilder().named(name).asRequired().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static <C extends CommandSender> CommandComponent<C, Byte> optional(@Nonnull final String name) {
|
||||||
|
return ByteComponent.<C>newBuilder().named(name).asOptional().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, Byte> {
|
||||||
|
|
||||||
|
private byte min = Byte.MIN_VALUE;
|
||||||
|
private byte max = Byte.MAX_VALUE;
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public Builder<C> withMin(final byte min) {
|
||||||
|
this.min = min;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public Builder<C> withMax(final byte max) {
|
||||||
|
this.max = max;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public ByteComponent<C> build() {
|
||||||
|
return new ByteComponent<>(this.required, this.name, this.min, this.max);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the minimum accepted byteeger that could have been parsed
|
||||||
|
*
|
||||||
|
* @return Minimum byteeger
|
||||||
|
*/
|
||||||
|
public byte getMin() {
|
||||||
|
return this.min;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the maximum accepted byteeger that could have been parsed
|
||||||
|
*
|
||||||
|
* @return Maximum byteeger
|
||||||
|
*/
|
||||||
|
public byte getMax() {
|
||||||
|
return this.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static final class ByteParser<C extends CommandSender> implements ComponentParser<C, Byte> {
|
||||||
|
|
||||||
|
private final byte min;
|
||||||
|
private final byte max;
|
||||||
|
|
||||||
|
public ByteParser(final byte min, final byte max) {
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public ComponentParseResult<Byte> parse(
|
||||||
|
@Nonnull final CommandContext<C> commandContext,
|
||||||
|
@Nonnull final Queue<String> inputQueue) {
|
||||||
|
final String input = inputQueue.peek();
|
||||||
|
if (input == null) {
|
||||||
|
return ComponentParseResult.failure(new NullPointerException("No input was provided"));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
final byte value = Byte.parseByte(input);
|
||||||
|
if (value < this.min || value > this.max) {
|
||||||
|
return ComponentParseResult.failure(
|
||||||
|
new ByteParseException(input,
|
||||||
|
this.min,
|
||||||
|
this.max));
|
||||||
|
}
|
||||||
|
return ComponentParseResult.success(value);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
return ComponentParseResult.failure(
|
||||||
|
new ByteParseException(input, this.min,
|
||||||
|
this.max));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final class ByteParseException extends NumberParseException {
|
||||||
|
|
||||||
|
public ByteParseException(@Nonnull final String input, final byte min, final byte max) {
|
||||||
|
super(input, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.getMin().byteValue() == Byte.MIN_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.getMax().byteValue() == Byte.MAX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNumberType() {
|
||||||
|
return "byte";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,160 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands.components.standard;
|
||||||
|
|
||||||
|
import com.intellectualsites.commands.components.CommandComponent;
|
||||||
|
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||||
|
import com.intellectualsites.commands.components.parser.ComponentParser;
|
||||||
|
import com.intellectualsites.commands.context.CommandContext;
|
||||||
|
import com.intellectualsites.commands.exceptions.parsing.NumberParseException;
|
||||||
|
import com.intellectualsites.commands.sender.CommandSender;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class IntegerComponent<C extends CommandSender> extends CommandComponent<C, Integer> {
|
||||||
|
|
||||||
|
private final int min;
|
||||||
|
private final int max;
|
||||||
|
|
||||||
|
private IntegerComponent(final boolean required, @Nonnull final String name, final int min, final int max) {
|
||||||
|
super(required, name, new IntegerParser<>(min, max));
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull public static <C extends CommandSender> Builder<C> newBuilder() {
|
||||||
|
return new Builder<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull public static <C extends CommandSender> CommandComponent<C, Integer> required(@Nonnull final String name) {
|
||||||
|
return IntegerComponent.<C>newBuilder().named(name).asRequired().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull public static <C extends CommandSender> CommandComponent<C, Integer> optional(@Nonnull final String name) {
|
||||||
|
return IntegerComponent.<C>newBuilder().named(name).asOptional().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, Integer> {
|
||||||
|
|
||||||
|
private int min = Integer.MIN_VALUE;
|
||||||
|
private int max = Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
@Nonnull public Builder<C> withMin(final int min) {
|
||||||
|
this.min = min;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull public Builder<C> withMax(final int max) {
|
||||||
|
this.max = max;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public IntegerComponent<C> build() {
|
||||||
|
return new IntegerComponent<>(this.required, this.name, this.min, this.max);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the minimum accepted integer that could have been parsed
|
||||||
|
*
|
||||||
|
* @return Minimum integer
|
||||||
|
*/
|
||||||
|
public int getMin() {
|
||||||
|
return this.min;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the maximum accepted integer that could have been parsed
|
||||||
|
*
|
||||||
|
* @return Maximum integer
|
||||||
|
*/
|
||||||
|
public int getMax() {
|
||||||
|
return this.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static final class IntegerParser<C extends CommandSender> implements ComponentParser<C, Integer> {
|
||||||
|
|
||||||
|
private final int min;
|
||||||
|
private final int max;
|
||||||
|
|
||||||
|
public IntegerParser(final int min, final int max) {
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public ComponentParseResult<Integer> parse(
|
||||||
|
@Nonnull final CommandContext<C> commandContext,
|
||||||
|
@Nonnull final Queue<String> inputQueue) {
|
||||||
|
final String input = inputQueue.peek();
|
||||||
|
if (input == null) {
|
||||||
|
return ComponentParseResult.failure(new NullPointerException("No input was provided"));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
final int value = Integer.parseInt(input);
|
||||||
|
if (value < this.min || value > this.max) {
|
||||||
|
return ComponentParseResult.failure(new IntegerParseException(input, this.min, this.max));
|
||||||
|
}
|
||||||
|
return ComponentParseResult.success(value);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
return ComponentParseResult.failure(new IntegerParseException(input, this.min, this.max));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final class IntegerParseException extends NumberParseException {
|
||||||
|
|
||||||
|
public IntegerParseException(@Nonnull final String input, final int min, final int max) {
|
||||||
|
super(input, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasMin() {
|
||||||
|
return this.getMin().intValue() == Integer.MIN_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasMax() {
|
||||||
|
return this.getMax().intValue() == Integer.MAX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNumberType() {
|
||||||
|
return "integer";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,86 @@
|
||||||
|
//
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2020 IntellectualSites
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
package com.intellectualsites.commands.exceptions.parsing;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
public abstract class NumberParseException extends IllegalArgumentException {
|
||||||
|
|
||||||
|
private final String input;
|
||||||
|
private final Number min;
|
||||||
|
private final Number max;
|
||||||
|
|
||||||
|
public NumberParseException(@Nonnull final String input, final int min, final int max) {
|
||||||
|
this.input = input;
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
if (this.hasMin() && this.hasMax()) {
|
||||||
|
return "'" + this.input + "' is not a valid " + this.getNumberType() + " in the range [" + this.min + ", " + this.max + "]";
|
||||||
|
} else if (this.hasMin()) {
|
||||||
|
return "'" + this.input + "' is not a valid " + this.getNumberType() + " above " + this.min;
|
||||||
|
} else if (this.hasMax()) {
|
||||||
|
return "'" + this.input + "' is not a valid " + this.getNumberType() + " below " + this.max;
|
||||||
|
} else {
|
||||||
|
return String.format("'%s' is not a valid %s", this.input, this.getNumberType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract String getNumberType();
|
||||||
|
|
||||||
|
public abstract boolean hasMax();
|
||||||
|
|
||||||
|
public abstract boolean hasMin();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the input that failed to parse
|
||||||
|
*
|
||||||
|
* @return Input
|
||||||
|
*/
|
||||||
|
@Nonnull public String getInput() {
|
||||||
|
return this.input;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the minimum accepted integer that could have been parsed
|
||||||
|
*
|
||||||
|
* @return Minimum integer
|
||||||
|
*/
|
||||||
|
public Number getMin() {
|
||||||
|
return this.min;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the maximum accepted integer that could have been parsed
|
||||||
|
*
|
||||||
|
* @return Maximum integer
|
||||||
|
*/
|
||||||
|
public Number getMax() {
|
||||||
|
return this.max;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -32,7 +32,7 @@ import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
|
||||||
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
|
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
|
||||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||||
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
|
||||||
import com.intellectualsites.commands.parser.ComponentParseResult;
|
import com.intellectualsites.commands.components.parser.ComponentParseResult;
|
||||||
import org.jline.reader.Candidate;
|
import org.jline.reader.Candidate;
|
||||||
import org.jline.reader.Completer;
|
import org.jline.reader.Completer;
|
||||||
import org.jline.reader.LineReader;
|
import org.jline.reader.LineReader;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue