Skip to content

Commit

Permalink
Add utility ValueResolverContext#(popInt(), popDouble(), popFloat(), …
Browse files Browse the repository at this point in the history
…popLong())
  • Loading branch information
Revxrsal committed Oct 8, 2021
1 parent d922d38 commit 270ad39
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import revxrsal.commands.process.ValueResolver.ValueResolverContext;

import java.util.List;
import java.util.function.Function;

public final class BaseCommandDispatcher {

Expand Down Expand Up @@ -270,5 +271,32 @@ public ValueContextR(List<String> input,
@Override public String pop() {
return argumentStack.pop();
}

private <T> T num(Function<String, T> f) {
String input = pop();
try {
if (input.startsWith("0x"))
return (T) Integer.valueOf(input.substring(2), 16);
return f.apply(input);
} catch (NumberFormatException e) {
throw new InvalidNumberException(parameter(), input);
}
}

@Override public int popInt() {
return num(Integer::parseInt);
}

@Override public double popDouble() {
return num(Double::parseDouble);
}

@Override public float popFloat() {
return num(Float::parseFloat);
}

@Override public long popLong() {
return num(Long::parseLong);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import revxrsal.commands.command.ArgumentStack;
import revxrsal.commands.command.CommandParameter;
import revxrsal.commands.exception.CommandExceptionHandler;
import revxrsal.commands.exception.InvalidNumberException;
import revxrsal.commands.process.ParameterResolver.ParameterResolverContext;

import java.util.NoSuchElementException;

/**
* A resolver for resolving values that, by default, require data from the arguments
* to resolve their value.
Expand Down Expand Up @@ -61,10 +64,50 @@ interface ValueResolverContext extends ParameterResolverContext {
* Returns (and removes) the first value in the {@link #arguments() argument stack}.
*
* @return The first value.
* @throws java.util.NoSuchElementException if the stack is empty
* @throws NoSuchElementException if the stack is empty
*/
String pop();

/**
* Returns (and removes) the first value in the {@link #arguments() argument stack}
* and parses it into an integer.
*
* @return The first value, as an int.
* @throws NoSuchElementException if the stack is empty
* @throws InvalidNumberException if an invalid number is inputted
*/
int popInt();

/**
* Returns (and removes) the first value in the {@link #arguments() argument stack}
* and parses it into a double.
*
* @return The first value, as a double.
* @throws NoSuchElementException if the stack is empty
* @throws InvalidNumberException if an invalid number is inputted
*/
double popDouble();

/**
* Returns (and removes) the first value in the {@link #arguments() argument stack}
* and parses it into a float.
*
* @return The first value, as a float.
* @throws NoSuchElementException if the stack is empty
* @throws InvalidNumberException if an invalid number is inputted
*/
float popFloat();

/**
* Returns (and removes) the first value in the {@link #arguments() argument stack}
* and parses it into a double.
*
* @return The first value, as a long.
* @throws NoSuchElementException if the stack is empty
* @throws InvalidNumberException if an invalid number is inputted
*/
long popLong();

}

}

0 comments on commit 270ad39

Please sign in to comment.