getValueAsInt - Checking if parsed long is within Java Integer.INT range

getValueAsInt - Using the int copy of NumerUtilities.parseLong
AskDialog: Organize commits
This commit is contained in:
foralost 2023-02-26 22:28:51 +01:00 committed by Ryan Kurtz
parent b30a94edc6
commit 62b96db10b
2 changed files with 53 additions and 2 deletions

View File

@ -246,7 +246,12 @@ public class AskDialog<T> extends DialogComponentProvider {
protected Integer getValueAsInt() {
String text = getValueAsString();
return text != null ? Integer.decode(text) : null;
if (text == null) {
return null;
}
return NumericUtilities.parseInt(text);
}
protected Long getValueAsLong() {

View File

@ -98,7 +98,53 @@ public final class NumericUtilities {
}
/**
* Parses the given string as a numeric value, detecting whether or not it begins with a hex
* Parses the given string as a numeric value, detecting whether or not it begins with a Hex
* prefix, and if not, parses as a int value.
*/
public static int parseInt(String numStr) {
String origStr = numStr;
int value = 0;
int sign = 1;
numStr = (numStr == null ? "" : numStr.trim());
if (numStr.length() == 0) {
return value;
}
if (numStr.startsWith("-")) {
sign = -1;
numStr = numStr.substring(1);
}
int radix = 10;
if (numStr.startsWith(HEX_PREFIX_x) || numStr.startsWith(HEX_PREFIX_X)) {
if (numStr.length() > 10) {
throw new NumberFormatException(numStr + " has too many digits.");
}
numStr = numStr.substring(2);
radix = 16;
}
if (numStr.length() == 0) {
return 0;
}
try {
BigInteger bi = new BigInteger(numStr, radix);
return bi.intValue() * sign;
}
catch (NumberFormatException e) {
// This is a little hacky, but the message should be complete and report about the
// original string
NumberFormatException e2 =
new NumberFormatException("Cannot parse int from " + origStr);
e2.setStackTrace(e.getStackTrace());
throw e2;
}
catch (ArithmeticException e) {
throw new NumberFormatException(origStr + " is too big.");
}
}
/**
* Parses the given string as a numeric value, detecting whether or not it begins with a Hex
* prefix, and if not, parses as a long int value.
* @param s the string to parse
* @return the long value