Converters - S3nS3IW00/JCommands GitHub Wiki
If an argument's input type is different than its result type, it needs to be converted to the result type. By default, JCommands can convert to Boolean
, Byte
, Short
, Integer
, Long
, Float
and Double
from String
by parsing the value, but for other conversions, a custom converter has to be registered.
To create a converter an instance of ArgumentResultConverter<F, T>
object or a class that implements this interface has to be created. The class has two generic types, the F
is the type of the input, and the T
is the type of the result.
- Lambda expression
ArgumentResultConverter<String, Integer> numberConverter = value -> Integer.parseInt(value);
- Class
public class NumberConverter implements ArgumentResultConverter<String, Integer> {
@Override
public Integer convertTo(String value) {
return Integer.parseInt(String.valueOf(value));
}
}
The converters can be registered on InputArgument<I, O>
with convertResult(ArgumentResultConverter)
method with the matching input and result type.