Let existing catch clause handle the NPE edge case
Add tests
This commit is contained in:
parent
250addd743
commit
ee2041464e
|
@ -784,7 +784,6 @@ public class NumberUtils {
|
||||||
public static byte max(final byte... array) {
|
public static byte max(final byte... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns max
|
// Finds and returns max
|
||||||
byte max = array[0];
|
byte max = array[0];
|
||||||
for (int i = 1; i < array.length; i++) {
|
for (int i = 1; i < array.length; i++) {
|
||||||
|
@ -792,7 +791,6 @@ public class NumberUtils {
|
||||||
max = array[i];
|
max = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -827,7 +825,6 @@ public class NumberUtils {
|
||||||
public static double max(final double... array) {
|
public static double max(final double... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns max
|
// Finds and returns max
|
||||||
double max = array[0];
|
double max = array[0];
|
||||||
for (int j = 1; j < array.length; j++) {
|
for (int j = 1; j < array.length; j++) {
|
||||||
|
@ -838,7 +835,6 @@ public class NumberUtils {
|
||||||
max = array[j];
|
max = array[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -871,7 +867,6 @@ public class NumberUtils {
|
||||||
public static float max(final float... array) {
|
public static float max(final float... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns max
|
// Finds and returns max
|
||||||
float max = array[0];
|
float max = array[0];
|
||||||
for (int j = 1; j < array.length; j++) {
|
for (int j = 1; j < array.length; j++) {
|
||||||
|
@ -882,7 +877,6 @@ public class NumberUtils {
|
||||||
max = array[j];
|
max = array[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -949,7 +943,6 @@ public class NumberUtils {
|
||||||
public static int max(final int... array) {
|
public static int max(final int... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns max
|
// Finds and returns max
|
||||||
int max = array[0];
|
int max = array[0];
|
||||||
for (int j = 1; j < array.length; j++) {
|
for (int j = 1; j < array.length; j++) {
|
||||||
|
@ -957,7 +950,6 @@ public class NumberUtils {
|
||||||
max = array[j];
|
max = array[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -991,7 +983,6 @@ public class NumberUtils {
|
||||||
public static long max(final long... array) {
|
public static long max(final long... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns max
|
// Finds and returns max
|
||||||
long max = array[0];
|
long max = array[0];
|
||||||
for (int j = 1; j < array.length; j++) {
|
for (int j = 1; j < array.length; j++) {
|
||||||
|
@ -999,7 +990,6 @@ public class NumberUtils {
|
||||||
max = array[j];
|
max = array[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1042,7 +1032,6 @@ public class NumberUtils {
|
||||||
max = array[i];
|
max = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1076,7 +1065,6 @@ public class NumberUtils {
|
||||||
public static byte min(final byte... array) {
|
public static byte min(final byte... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns min
|
// Finds and returns min
|
||||||
byte min = array[0];
|
byte min = array[0];
|
||||||
for (int i = 1; i < array.length; i++) {
|
for (int i = 1; i < array.length; i++) {
|
||||||
|
@ -1084,7 +1072,6 @@ public class NumberUtils {
|
||||||
min = array[i];
|
min = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1130,7 +1117,6 @@ public class NumberUtils {
|
||||||
min = array[i];
|
min = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1174,7 +1160,6 @@ public class NumberUtils {
|
||||||
min = array[i];
|
min = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1206,7 +1191,6 @@ public class NumberUtils {
|
||||||
public static int min(final int... array) {
|
public static int min(final int... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns min
|
// Finds and returns min
|
||||||
int min = array[0];
|
int min = array[0];
|
||||||
for (int j = 1; j < array.length; j++) {
|
for (int j = 1; j < array.length; j++) {
|
||||||
|
@ -1214,7 +1198,6 @@ public class NumberUtils {
|
||||||
min = array[j];
|
min = array[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1248,7 +1231,6 @@ public class NumberUtils {
|
||||||
public static long min(final long... array) {
|
public static long min(final long... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns min
|
// Finds and returns min
|
||||||
long min = array[0];
|
long min = array[0];
|
||||||
for (int i = 1; i < array.length; i++) {
|
for (int i = 1; i < array.length; i++) {
|
||||||
|
@ -1256,7 +1238,6 @@ public class NumberUtils {
|
||||||
min = array[i];
|
min = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1291,7 +1272,6 @@ public class NumberUtils {
|
||||||
public static short min(final short... array) {
|
public static short min(final short... array) {
|
||||||
// Validates input
|
// Validates input
|
||||||
validateArray(array);
|
validateArray(array);
|
||||||
|
|
||||||
// Finds and returns min
|
// Finds and returns min
|
||||||
short min = array[0];
|
short min = array[0];
|
||||||
for (int i = 1; i < array.length; i++) {
|
for (int i = 1; i < array.length; i++) {
|
||||||
|
@ -1360,12 +1340,9 @@ public class NumberUtils {
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
*/
|
*/
|
||||||
public static byte toByte(final String str, final byte defaultValue) {
|
public static byte toByte(final String str, final byte defaultValue) {
|
||||||
if (str == null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return Byte.parseByte(str);
|
return Byte.parseByte(str);
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final RuntimeException e) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1453,12 +1430,9 @@ public class NumberUtils {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static double toDouble(final String str, final double defaultValue) {
|
public static double toDouble(final String str, final double defaultValue) {
|
||||||
if (str == null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return Double.parseDouble(str);
|
return Double.parseDouble(str);
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final RuntimeException e) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1505,12 +1479,9 @@ public class NumberUtils {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static float toFloat(final String str, final float defaultValue) {
|
public static float toFloat(final String str, final float defaultValue) {
|
||||||
if (str == null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return Float.parseFloat(str);
|
return Float.parseFloat(str);
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final RuntimeException e) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1554,12 +1525,9 @@ public class NumberUtils {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static int toInt(final String str, final int defaultValue) {
|
public static int toInt(final String str, final int defaultValue) {
|
||||||
if (str == null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(str);
|
return Integer.parseInt(str);
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final RuntimeException e) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1603,12 +1571,9 @@ public class NumberUtils {
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static long toLong(final String str, final long defaultValue) {
|
public static long toLong(final String str, final long defaultValue) {
|
||||||
if (str == null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return Long.parseLong(str);
|
return Long.parseLong(str);
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final RuntimeException e) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1807,12 +1772,9 @@ public class NumberUtils {
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
*/
|
*/
|
||||||
public static short toShort(final String str, final short defaultValue) {
|
public static short toShort(final String str, final short defaultValue) {
|
||||||
if (str == null) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return Short.parseShort(str);
|
return Short.parseShort(str);
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final RuntimeException e) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1612,6 +1612,8 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
assertEquals(NumberUtils.toDouble("-001.2345", 5.1d), -1.2345d, "toDouble(String, int) 4 failed");
|
assertEquals(NumberUtils.toDouble("-001.2345", 5.1d), -1.2345d, "toDouble(String, int) 4 failed");
|
||||||
assertEquals(1.2345d, NumberUtils.toDouble("+001.2345", 5.1d), "toDouble(String, int) 5 failed");
|
assertEquals(1.2345d, NumberUtils.toDouble("+001.2345", 5.1d), "toDouble(String, int) 5 failed");
|
||||||
assertEquals(0d, NumberUtils.toDouble("000.00", 5.1d), "toDouble(String, int) 7 failed");
|
assertEquals(0d, NumberUtils.toDouble("000.00", 5.1d), "toDouble(String, int) 7 failed");
|
||||||
|
assertEquals(5.1d, NumberUtils.toDouble("", 5.1d));
|
||||||
|
assertEquals(5.1d, NumberUtils.toDouble((String) null, 5.1d));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1632,6 +1634,8 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
public void testToByteStringI() {
|
public void testToByteStringI() {
|
||||||
assertEquals(123, NumberUtils.toByte("123", (byte) 5), "toByte(String, byte) 1 failed");
|
assertEquals(123, NumberUtils.toByte("123", (byte) 5), "toByte(String, byte) 1 failed");
|
||||||
assertEquals(5, NumberUtils.toByte("12.3", (byte) 5), "toByte(String, byte) 2 failed");
|
assertEquals(5, NumberUtils.toByte("12.3", (byte) 5), "toByte(String, byte) 2 failed");
|
||||||
|
assertEquals(5, NumberUtils.toByte("", (byte) 5));
|
||||||
|
assertEquals(5, NumberUtils.toByte(null, (byte) 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1665,6 +1669,8 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
assertEquals(5.0f, NumberUtils.toFloat("-001Z.2345", 5.0f), "toFloat(String, int) 3 failed");
|
assertEquals(5.0f, NumberUtils.toFloat("-001Z.2345", 5.0f), "toFloat(String, int) 3 failed");
|
||||||
assertEquals(5.0f, NumberUtils.toFloat("+001AB.2345", 5.0f), "toFloat(String, int) 4 failed");
|
assertEquals(5.0f, NumberUtils.toFloat("+001AB.2345", 5.0f), "toFloat(String, int) 4 failed");
|
||||||
assertEquals(5.0f, NumberUtils.toFloat("001Z.2345", 5.0f), "toFloat(String, int) 5 failed");
|
assertEquals(5.0f, NumberUtils.toFloat("001Z.2345", 5.0f), "toFloat(String, int) 5 failed");
|
||||||
|
assertEquals(5.0f, NumberUtils.toFloat("", 5.0f));
|
||||||
|
assertEquals(5.0f, NumberUtils.toFloat(null, 5.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1685,6 +1691,8 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
public void testToIntStringI() {
|
public void testToIntStringI() {
|
||||||
assertEquals(12345, NumberUtils.toInt("12345", 5), "toInt(String, int) 1 failed");
|
assertEquals(12345, NumberUtils.toInt("12345", 5), "toInt(String, int) 1 failed");
|
||||||
assertEquals(5, NumberUtils.toInt("1234.5", 5), "toInt(String, int) 2 failed");
|
assertEquals(5, NumberUtils.toInt("1234.5", 5), "toInt(String, int) 2 failed");
|
||||||
|
assertEquals(5, NumberUtils.toInt("", 5));
|
||||||
|
assertEquals(5, NumberUtils.toInt(null, 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1709,6 +1717,8 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
public void testToLongStringL() {
|
public void testToLongStringL() {
|
||||||
assertEquals(12345L, NumberUtils.toLong("12345", 5L), "toLong(String, long) 1 failed");
|
assertEquals(12345L, NumberUtils.toLong("12345", 5L), "toLong(String, long) 1 failed");
|
||||||
assertEquals(5L, NumberUtils.toLong("1234.5", 5L), "toLong(String, long) 2 failed");
|
assertEquals(5L, NumberUtils.toLong("1234.5", 5L), "toLong(String, long) 2 failed");
|
||||||
|
assertEquals(5L, NumberUtils.toLong("", 5L));
|
||||||
|
assertEquals(5L, NumberUtils.toLong(null, 5L));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1823,7 +1833,7 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
assertEquals("23521.0000", NumberUtils.toScaledBigDecimal(Float.valueOf(23.521f), 4, RoundingMode.HALF_EVEN)
|
assertEquals("23521.0000", NumberUtils.toScaledBigDecimal(Float.valueOf(23.521f), 4, RoundingMode.HALF_EVEN)
|
||||||
.multiply(BigDecimal.valueOf(1000)).toString(), "toScaledBigDecimal(Float, int, RoundingMode) 4 failed");
|
.multiply(BigDecimal.valueOf(1000)).toString(), "toScaledBigDecimal(Float, int, RoundingMode) 4 failed");
|
||||||
assertEquals(NumberUtils.toScaledBigDecimal((Float) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
|
assertEquals(NumberUtils.toScaledBigDecimal((Float) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
|
||||||
"toScaledBigDecimal(Float, int, RoundingMode) 5 failed");
|
"toScaledBigDecimal(Float, int, RoundingMode) 5 failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1841,7 +1851,7 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
assertEquals("2352.00", NumberUtils.toScaledBigDecimal("23.525").multiply(BigDecimal.valueOf(100)).toString(),
|
assertEquals("2352.00", NumberUtils.toScaledBigDecimal("23.525").multiply(BigDecimal.valueOf(100)).toString(),
|
||||||
"toScaledBigDecimal(String) 4 failed");
|
"toScaledBigDecimal(String) 4 failed");
|
||||||
assertEquals(NumberUtils.toScaledBigDecimal((String) null), BigDecimal.ZERO,
|
assertEquals(NumberUtils.toScaledBigDecimal((String) null), BigDecimal.ZERO,
|
||||||
"toScaledBigDecimal(String) 5 failed");
|
"toScaledBigDecimal(String) 5 failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1860,7 +1870,7 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
.multiply(BigDecimal.valueOf(1000)).toString(),
|
.multiply(BigDecimal.valueOf(1000)).toString(),
|
||||||
"toScaledBigDecimal(String, int, RoundingMode) 4 failed");
|
"toScaledBigDecimal(String, int, RoundingMode) 4 failed");
|
||||||
assertEquals(NumberUtils.toScaledBigDecimal((String) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
|
assertEquals(NumberUtils.toScaledBigDecimal((String) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
|
||||||
"toScaledBigDecimal(String, int, RoundingMode) 5 failed");
|
"toScaledBigDecimal(String, int, RoundingMode) 5 failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1881,5 +1891,7 @@ public class NumberUtilsTest extends AbstractLangTest {
|
||||||
public void testToShortStringI() {
|
public void testToShortStringI() {
|
||||||
assertEquals(12345, NumberUtils.toShort("12345", (short) 5), "toShort(String, short) 1 failed");
|
assertEquals(12345, NumberUtils.toShort("12345", (short) 5), "toShort(String, short) 1 failed");
|
||||||
assertEquals(5, NumberUtils.toShort("1234.5", (short) 5), "toShort(String, short) 2 failed");
|
assertEquals(5, NumberUtils.toShort("1234.5", (short) 5), "toShort(String, short) 2 failed");
|
||||||
|
assertEquals(5, NumberUtils.toShort("", (short) 5));
|
||||||
|
assertEquals(5, NumberUtils.toShort(null, (short) 5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue