2015-06-05 02:32:36 +00:00
|
|
|
package com.earth2me.essentials.utils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parseFloat and parseDouble proxies that are protected against non-finite values.
|
|
|
|
*/
|
2015-06-05 15:38:06 +00:00
|
|
|
public class FloatUtil {
|
|
|
|
private FloatUtil() {
|
|
|
|
}
|
2015-06-05 02:32:36 +00:00
|
|
|
|
2015-06-05 15:38:06 +00:00
|
|
|
public static float parseFloat(String s) throws NumberFormatException {
|
2015-06-05 02:32:36 +00:00
|
|
|
float f = Float.parseFloat(s);
|
2015-06-05 15:38:06 +00:00
|
|
|
if (Float.isNaN(f)) {
|
2015-06-05 02:32:36 +00:00
|
|
|
throw new NumberFormatException("NaN is not valid");
|
|
|
|
}
|
2015-06-05 15:38:06 +00:00
|
|
|
if (Float.isInfinite(f)) {
|
2015-06-05 02:32:36 +00:00
|
|
|
throw new NumberFormatException("Infinity is not valid");
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:38:06 +00:00
|
|
|
public static double parseDouble(String s) throws NumberFormatException {
|
2015-06-05 02:32:36 +00:00
|
|
|
double d = Double.parseDouble(s);
|
2015-06-05 15:38:06 +00:00
|
|
|
if (Double.isNaN(d)) {
|
2015-06-05 02:32:36 +00:00
|
|
|
throw new NumberFormatException("NaN is not valid");
|
|
|
|
}
|
2015-06-05 15:38:06 +00:00
|
|
|
if (Double.isInfinite(d)) {
|
2015-06-05 02:32:36 +00:00
|
|
|
throw new NumberFormatException("Infinity is not valid");
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|