Some ArgParser cleanups
This commit is contained in:
parent
0a2e3f98f9
commit
a0f412f255
@ -19,29 +19,36 @@ package baritone.api.utils.command.argparser;
|
||||
|
||||
public abstract class ArgParser<T> implements IArgParser<T> {
|
||||
|
||||
private final Class<T> klass;
|
||||
private final Class<T> target;
|
||||
|
||||
protected ArgParser(Class<T> klass) {
|
||||
this.klass = klass;
|
||||
private ArgParser(Class<T> target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<T> getKlass() {
|
||||
return klass;
|
||||
public Class<T> getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public static abstract class Stateless<T> extends ArgParser<T> implements IArgParser.Stateless<T> {
|
||||
|
||||
public Stateless(Class<T> target) {
|
||||
super(target);
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Stated<T, S> extends ArgParser<T> implements IArgParser.Stated<T, S> {
|
||||
|
||||
private final Class<S> stateKlass;
|
||||
private final Class<S> stateType;
|
||||
|
||||
protected Stated(Class<T> klass, Class<S> stateKlass) {
|
||||
super(klass);
|
||||
this.stateKlass = stateKlass;
|
||||
protected Stated(Class<T> target, Class<S> stateType) {
|
||||
super(target);
|
||||
this.stateType = stateType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<S> getStateKlass() {
|
||||
return stateKlass;
|
||||
public Class<S> getStateType() {
|
||||
return stateType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,30 +31,30 @@ public class ArgParserManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param klass The class to search for.
|
||||
* @param type The type trying to be parsed
|
||||
* @return A parser that can parse arguments into this class, if found.
|
||||
*/
|
||||
public static <T> ArgParser.Stateless<T> getParserStateless(Class<T> klass) {
|
||||
public static <T> ArgParser.Stateless<T> getParserStateless(Class<T> type) {
|
||||
//noinspection unchecked
|
||||
return REGISTRY.descendingStream()
|
||||
.filter(ArgParser.Stateless.class::isInstance)
|
||||
.map(ArgParser.Stateless.class::cast)
|
||||
.filter(parser -> parser.getKlass().isAssignableFrom(klass))
|
||||
.filter(parser -> parser.getTarget().isAssignableFrom(type))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param klass The class to search for.
|
||||
* @param type The type trying to be parsed
|
||||
* @return A parser that can parse arguments into this class, if found.
|
||||
*/
|
||||
public static <T, S> ArgParser.Stated<T, S> getParserStated(Class<T> klass, Class<S> stateKlass) {
|
||||
public static <T, S> ArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> stateKlass) {
|
||||
//noinspection unchecked
|
||||
return REGISTRY.descendingStream()
|
||||
.filter(ArgParser.Stated.class::isInstance)
|
||||
.map(ArgParser.Stated.class::cast)
|
||||
.filter(parser -> parser.getKlass().isAssignableFrom(klass))
|
||||
.filter(parser -> parser.getStateKlass().isAssignableFrom(stateKlass))
|
||||
.filter(parser -> parser.getTarget().isAssignableFrom(type))
|
||||
.filter(parser -> parser.getStateType().isAssignableFrom(stateKlass))
|
||||
.map(ArgParser.Stated.class::cast)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
@ -63,28 +63,28 @@ public class ArgParserManager {
|
||||
/**
|
||||
* Attempt to parse the specified argument with a stateless {@link ArgParser} that outputs the specified class.
|
||||
*
|
||||
* @param klass The class to parse the argument into.
|
||||
* @param type The type to try and parse the argument into.
|
||||
* @param arg The argument to parse.
|
||||
* @return An instance of the specified class.
|
||||
* @throws CommandNoParserForTypeException If no parser exists for that type
|
||||
* @throws CommandInvalidTypeException If the parsing failed
|
||||
*/
|
||||
public static <T> T parseStateless(Class<T> klass, CommandArgument arg) {
|
||||
ArgParser.Stateless<T> parser = getParserStateless(klass);
|
||||
public static <T> T parseStateless(Class<T> type, CommandArgument arg) {
|
||||
ArgParser.Stateless<T> parser = getParserStateless(type);
|
||||
if (parser == null) {
|
||||
throw new CommandNoParserForTypeException(klass);
|
||||
throw new CommandNoParserForTypeException(type);
|
||||
}
|
||||
try {
|
||||
return parser.parseArg(arg);
|
||||
} catch (RuntimeException exc) {
|
||||
throw new CommandInvalidTypeException(arg, klass.getSimpleName());
|
||||
throw new CommandInvalidTypeException(arg, type.getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to parse the specified argument with a stated {@link ArgParser} that outputs the specified class.
|
||||
*
|
||||
* @param klass The class to parse the argument into.
|
||||
* @param type The type to try and parse the argument into.
|
||||
* @param arg The argument to parse.
|
||||
* @param state The state to pass to the {@link ArgParser.Stated}.
|
||||
* @return An instance of the specified class.
|
||||
@ -92,15 +92,15 @@ public class ArgParserManager {
|
||||
* @throws CommandInvalidTypeException If the parsing failed
|
||||
* @see ArgParser.Stated
|
||||
*/
|
||||
public static <T, S> T parseStated(Class<T> klass, Class<S> stateKlass, CommandArgument arg, S state) {
|
||||
ArgParser.Stated<T, S> parser = getParserStated(klass, stateKlass);
|
||||
public static <T, S> T parseStated(Class<T> type, Class<S> stateKlass, CommandArgument arg, S state) {
|
||||
ArgParser.Stated<T, S> parser = getParserStated(type, stateKlass);
|
||||
if (parser == null) {
|
||||
throw new CommandNoParserForTypeException(klass);
|
||||
throw new CommandNoParserForTypeException(type);
|
||||
}
|
||||
try {
|
||||
return parser.parseArg(arg, state);
|
||||
} catch (RuntimeException exc) {
|
||||
throw new CommandInvalidTypeException(arg, klass.getSimpleName());
|
||||
throw new CommandInvalidTypeException(arg, type.getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ import java.util.Locale;
|
||||
|
||||
public class DefaultArgParsers {
|
||||
|
||||
public static class IntArgumentParser extends ArgParser<Integer> implements IArgParser.Stateless<Integer> {
|
||||
public static class IntArgumentParser extends ArgParser.Stateless<Integer> {
|
||||
|
||||
public static final IntArgumentParser INSTANCE = new IntArgumentParser();
|
||||
|
||||
@ -39,7 +39,7 @@ public class DefaultArgParsers {
|
||||
}
|
||||
}
|
||||
|
||||
public static class LongArgumentParser extends ArgParser<Long> implements IArgParser.Stateless<Long> {
|
||||
public static class LongArgumentParser extends ArgParser.Stateless<Long> {
|
||||
|
||||
public static final LongArgumentParser INSTANCE = new LongArgumentParser();
|
||||
|
||||
@ -53,7 +53,7 @@ public class DefaultArgParsers {
|
||||
}
|
||||
}
|
||||
|
||||
public static class FloatArgumentParser extends ArgParser<Float> implements IArgParser.Stateless<Float> {
|
||||
public static class FloatArgumentParser extends ArgParser.Stateless<Float> {
|
||||
|
||||
public static final FloatArgumentParser INSTANCE = new FloatArgumentParser();
|
||||
|
||||
@ -71,7 +71,7 @@ public class DefaultArgParsers {
|
||||
}
|
||||
}
|
||||
|
||||
public static class DoubleArgumentParser extends ArgParser<Double> implements IArgParser.Stateless<Double> {
|
||||
public static class DoubleArgumentParser extends ArgParser.Stateless<Double> {
|
||||
|
||||
public static final DoubleArgumentParser INSTANCE = new DoubleArgumentParser();
|
||||
|
||||
@ -89,7 +89,7 @@ public class DefaultArgParsers {
|
||||
}
|
||||
}
|
||||
|
||||
public static class BooleanArgumentParser extends ArgParser<Boolean> implements IArgParser.Stateless<Boolean> {
|
||||
public static class BooleanArgumentParser extends ArgParser.Stateless<Boolean> {
|
||||
|
||||
public static final BooleanArgumentParser INSTANCE = new BooleanArgumentParser();
|
||||
public static final List<String> TRUTHY_VALUES = Arrays.asList("1", "true", "yes", "t", "y", "on", "enable");
|
||||
|
@ -24,7 +24,7 @@ public interface IArgParser<T> {
|
||||
/**
|
||||
* @return the class of this parser.
|
||||
*/
|
||||
Class<T> getKlass();
|
||||
Class<T> getTarget();
|
||||
|
||||
/**
|
||||
* A stateless argument parser is just that. It takes a {@link CommandArgument} and outputs its type.
|
||||
@ -50,7 +50,7 @@ public interface IArgParser<T> {
|
||||
*/
|
||||
interface Stated<T, S> extends IArgParser<T> {
|
||||
|
||||
Class<S> getStateKlass();
|
||||
Class<S> getStateType();
|
||||
|
||||
/**
|
||||
* @param arg The argument to parse.
|
||||
|
@ -31,7 +31,6 @@ import java.util.stream.StreamSupport;
|
||||
* @param <V> The entry type that will be stored in this registry. This can be anything, really - preferably anything
|
||||
* that works as a HashMap key, as that's what's used to keep track of which entries are registered or not.
|
||||
*/
|
||||
@SuppressWarnings({"unused", "UnusedReturnValue"})
|
||||
public class Registry<V> {
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user