You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/08/20 14:38:46 UTC

(commons-lang) branch master updated (250addd74 -> dcba60a56)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


    from 250addd74 Add test
     new ee2041464 Let existing catch clause handle the NPE edge case
     new dcba60a56 Javadoc

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/commons/lang3/math/NumberUtils.java | 94 +++++++---------------
 .../apache/commons/lang3/math/NumberUtilsTest.java | 18 ++++-
 2 files changed, 43 insertions(+), 69 deletions(-)


(commons-lang) 02/02: Javadoc

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit dcba60a56927cc7f392528d0d093f0b0b3d9d486
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Aug 20 10:38:39 2024 -0400

    Javadoc
---
 .../org/apache/commons/lang3/math/NumberUtils.java | 44 +++++++++++-----------
 1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
index c3cb68c2b..fd667c542 100644
--- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
+++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
@@ -1302,7 +1302,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code byte}, returning
+     * Converts a {@link String} to a {@code byte}, returning
      * {@code zero} if the conversion fails.
      *
      * <p>If the string is {@code null}, {@code zero} is returned.</p>
@@ -1323,7 +1323,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code byte}, returning a
+     * Converts a {@link String} to a {@code byte}, returning a
      * default value if the conversion fails.
      *
      * <p>If the string is {@code null}, the default value is returned.</p>
@@ -1348,7 +1348,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link BigDecimal} to a {@code double}.
+     * Converts a {@link BigDecimal} to a {@code double}.
      *
      * <p>If the {@link BigDecimal} {@code value} is
      * {@code null}, then the specified default value is returned.</p>
@@ -1368,7 +1368,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link BigDecimal} to a {@code double}.
+     * Converts a {@link BigDecimal} to a {@code double}.
      *
      * <p>If the {@link BigDecimal} {@code value} is
      * {@code null}, then the specified default value is returned.</p>
@@ -1389,7 +1389,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code double}, returning
+     * Converts a {@link String} to a {@code double}, returning
      * {@code 0.0d} if the conversion fails.
      *
      * <p>If the string {@code str} is {@code null},
@@ -1411,7 +1411,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code double}, returning a
+     * Converts a {@link String} to a {@code double}, returning a
      * default value if the conversion fails.
      *
      * <p>If the string {@code str} is {@code null}, the default
@@ -1438,7 +1438,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code float}, returning
+     * Converts a {@link String} to a {@code float}, returning
      * {@code 0.0f} if the conversion fails.
      *
      * <p>If the string {@code str} is {@code null},
@@ -1460,7 +1460,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code float}, returning a
+     * Converts a {@link String} to a {@code float}, returning a
      * default value if the conversion fails.
      *
      * <p>If the string {@code str} is {@code null}, the default
@@ -1487,7 +1487,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to an {@code int}, returning
+     * Converts a {@link String} to an {@code int}, returning
      * {@code zero} if the conversion fails.
      *
      * <p>If the string is {@code null}, {@code zero} is returned.</p>
@@ -1508,7 +1508,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to an {@code int}, returning a
+     * Converts a {@link String} to an {@code int}, returning a
      * default value if the conversion fails.
      *
      * <p>If the string is {@code null}, the default value is returned.</p>
@@ -1533,7 +1533,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code long}, returning
+     * Converts a {@link String} to a {@code long}, returning
      * {@code zero} if the conversion fails.
      *
      * <p>If the string is {@code null}, {@code zero} is returned.</p>
@@ -1554,7 +1554,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code long}, returning a
+     * Converts a {@link String} to a {@code long}, returning a
      * default value if the conversion fails.
      *
      * <p>If the string is {@code null}, the default value is returned.</p>
@@ -1579,7 +1579,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link BigDecimal} to a {@link BigDecimal} with a scale of
+     * Converts a {@link BigDecimal} to a {@link BigDecimal} with a scale of
      * two that has been rounded using {@code RoundingMode.HALF_EVEN}. If the supplied
      * {@code value} is null, then {@code BigDecimal.ZERO} is returned.
      *
@@ -1595,7 +1595,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link BigDecimal} to a {@link BigDecimal} whose scale is the
+     * Converts a {@link BigDecimal} to a {@link BigDecimal} whose scale is the
      * specified value with a {@link RoundingMode} applied. If the input {@code value}
      * is {@code null}, we simply return {@code BigDecimal.ZERO}.
      *
@@ -1617,7 +1617,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link Double} to a {@link BigDecimal} with a scale of
+     * Converts a {@link Double} to a {@link BigDecimal} with a scale of
      * two that has been rounded using {@code RoundingMode.HALF_EVEN}. If the supplied
      * {@code value} is null, then {@code BigDecimal.ZERO} is returned.
      *
@@ -1633,7 +1633,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link Double} to a {@link BigDecimal} whose scale is the
+     * Converts a {@link Double} to a {@link BigDecimal} whose scale is the
      * specified value with a {@link RoundingMode} applied. If the input {@code value}
      * is {@code null}, we simply return {@code BigDecimal.ZERO}.
      *
@@ -1656,7 +1656,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link Float} to a {@link BigDecimal} with a scale of
+     * Converts a {@link Float} to a {@link BigDecimal} with a scale of
      * two that has been rounded using {@code RoundingMode.HALF_EVEN}. If the supplied
      * {@code value} is null, then {@code BigDecimal.ZERO} is returned.
      *
@@ -1672,7 +1672,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link Float} to a {@link BigDecimal} whose scale is the
+     * Converts a {@link Float} to a {@link BigDecimal} whose scale is the
      * specified value with a {@link RoundingMode} applied. If the input {@code value}
      * is {@code null}, we simply return {@code BigDecimal.ZERO}.
      *
@@ -1695,7 +1695,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@link BigDecimal} with a scale of
+     * Converts a {@link String} to a {@link BigDecimal} with a scale of
      * two that has been rounded using {@code RoundingMode.HALF_EVEN}. If the supplied
      * {@code value} is null, then {@code BigDecimal.ZERO} is returned.
      *
@@ -1711,7 +1711,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@link BigDecimal} whose scale is the
+     * Converts a {@link String} to a {@link BigDecimal} whose scale is the
      * specified value with a {@link RoundingMode} applied. If the input {@code value}
      * is {@code null}, we simply return {@code BigDecimal.ZERO}.
      *
@@ -1734,7 +1734,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to a {@code short}, returning
+     * Converts a {@link String} to a {@code short}, returning
      * {@code zero} if the conversion fails.
      *
      * <p>If the string is {@code null}, {@code zero} is returned.</p>
@@ -1755,7 +1755,7 @@ public class NumberUtils {
     }
 
     /**
-     * Convert a {@link String} to an {@code short}, returning a
+     * Converts a {@link String} to an {@code short}, returning a
      * default value if the conversion fails.
      *
      * <p>If the string is {@code null}, the default value is returned.</p>


(commons-lang) 01/02: Let existing catch clause handle the NPE edge case

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit ee2041464e60082a601d72be329bac57bc67328f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Aug 20 10:38:33 2024 -0400

    Let existing catch clause handle the NPE edge case
    
    Add tests
---
 .../org/apache/commons/lang3/math/NumberUtils.java | 50 +++-------------------
 .../apache/commons/lang3/math/NumberUtilsTest.java | 18 ++++++--
 2 files changed, 21 insertions(+), 47 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
index cc2eebc80..c3cb68c2b 100644
--- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
+++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
@@ -784,7 +784,6 @@ public class NumberUtils {
     public static byte max(final byte... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns max
         byte max = array[0];
         for (int i = 1; i < array.length; i++) {
@@ -792,7 +791,6 @@ public class NumberUtils {
                 max = array[i];
             }
         }
-
         return max;
     }
 
@@ -827,7 +825,6 @@ public class NumberUtils {
     public static double max(final double... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns max
         double max = array[0];
         for (int j = 1; j < array.length; j++) {
@@ -838,7 +835,6 @@ public class NumberUtils {
                 max = array[j];
             }
         }
-
         return max;
     }
 
@@ -871,7 +867,6 @@ public class NumberUtils {
     public static float max(final float... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns max
         float max = array[0];
         for (int j = 1; j < array.length; j++) {
@@ -882,7 +877,6 @@ public class NumberUtils {
                 max = array[j];
             }
         }
-
         return max;
     }
 
@@ -949,7 +943,6 @@ public class NumberUtils {
     public static int max(final int... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns max
         int max = array[0];
         for (int j = 1; j < array.length; j++) {
@@ -957,7 +950,6 @@ public class NumberUtils {
                 max = array[j];
             }
         }
-
         return max;
     }
 
@@ -991,7 +983,6 @@ public class NumberUtils {
     public static long max(final long... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns max
         long max = array[0];
         for (int j = 1; j < array.length; j++) {
@@ -999,7 +990,6 @@ public class NumberUtils {
                 max = array[j];
             }
         }
-
         return max;
     }
 
@@ -1042,7 +1032,6 @@ public class NumberUtils {
                 max = array[i];
             }
         }
-
         return max;
     }
 
@@ -1076,7 +1065,6 @@ public class NumberUtils {
     public static byte min(final byte... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns min
         byte min = array[0];
         for (int i = 1; i < array.length; i++) {
@@ -1084,7 +1072,6 @@ public class NumberUtils {
                 min = array[i];
             }
         }
-
         return min;
     }
 
@@ -1130,7 +1117,6 @@ public class NumberUtils {
                 min = array[i];
             }
         }
-
         return min;
     }
 
@@ -1174,7 +1160,6 @@ public class NumberUtils {
                 min = array[i];
             }
         }
-
         return min;
     }
 
@@ -1206,7 +1191,6 @@ public class NumberUtils {
     public static int min(final int... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns min
         int min = array[0];
         for (int j = 1; j < array.length; j++) {
@@ -1214,7 +1198,6 @@ public class NumberUtils {
                 min = array[j];
             }
         }
-
         return min;
     }
 
@@ -1248,7 +1231,6 @@ public class NumberUtils {
     public static long min(final long... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns min
         long min = array[0];
         for (int i = 1; i < array.length; i++) {
@@ -1256,7 +1238,6 @@ public class NumberUtils {
                 min = array[i];
             }
         }
-
         return min;
     }
 
@@ -1291,7 +1272,6 @@ public class NumberUtils {
     public static short min(final short... array) {
         // Validates input
         validateArray(array);
-
         // Finds and returns min
         short min = array[0];
         for (int i = 1; i < array.length; i++) {
@@ -1360,12 +1340,9 @@ public class NumberUtils {
      * @since 2.5
      */
     public static byte toByte(final String str, final byte defaultValue) {
-        if (str == null) {
-            return defaultValue;
-        }
         try {
             return Byte.parseByte(str);
-        } catch (final NumberFormatException nfe) {
+        } catch (final RuntimeException e) {
             return defaultValue;
         }
     }
@@ -1453,12 +1430,9 @@ public class NumberUtils {
      * @since 2.1
      */
     public static double toDouble(final String str, final double defaultValue) {
-      if (str == null) {
-          return defaultValue;
-      }
       try {
           return Double.parseDouble(str);
-      } catch (final NumberFormatException nfe) {
+      } catch (final RuntimeException e) {
           return defaultValue;
       }
     }
@@ -1505,12 +1479,9 @@ public class NumberUtils {
      * @since 2.1
      */
     public static float toFloat(final String str, final float defaultValue) {
-      if (str == null) {
-          return defaultValue;
-      }
       try {
           return Float.parseFloat(str);
-      } catch (final NumberFormatException nfe) {
+      } catch (final RuntimeException e) {
           return defaultValue;
       }
     }
@@ -1554,12 +1525,9 @@ public class NumberUtils {
      * @since 2.1
      */
     public static int toInt(final String str, final int defaultValue) {
-        if (str == null) {
-            return defaultValue;
-        }
         try {
             return Integer.parseInt(str);
-        } catch (final NumberFormatException nfe) {
+        } catch (final RuntimeException e) {
             return defaultValue;
         }
     }
@@ -1603,12 +1571,9 @@ public class NumberUtils {
      * @since 2.1
      */
     public static long toLong(final String str, final long defaultValue) {
-        if (str == null) {
-            return defaultValue;
-        }
         try {
             return Long.parseLong(str);
-        } catch (final NumberFormatException nfe) {
+        } catch (final RuntimeException e) {
             return defaultValue;
         }
     }
@@ -1807,12 +1772,9 @@ public class NumberUtils {
      * @since 2.5
      */
     public static short toShort(final String str, final short defaultValue) {
-        if (str == null) {
-            return defaultValue;
-        }
         try {
             return Short.parseShort(str);
-        } catch (final NumberFormatException nfe) {
+        } catch (final RuntimeException e) {
             return defaultValue;
         }
     }
diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
index 46f46630c..b07be6662 100644
--- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
@@ -1612,6 +1612,8 @@ public class NumberUtilsTest extends AbstractLangTest {
         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(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() {
         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("", (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("+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("", 5.0f));
+        assertEquals(5.0f, NumberUtils.toFloat(null, 5.0f));
     }
 
     /**
@@ -1685,6 +1691,8 @@ public class NumberUtilsTest extends AbstractLangTest {
     public void testToIntStringI() {
         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("", 5));
+        assertEquals(5, NumberUtils.toInt(null, 5));
     }
 
     /**
@@ -1709,6 +1717,8 @@ public class NumberUtilsTest extends AbstractLangTest {
     public void testToLongStringL() {
         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("", 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)
             .multiply(BigDecimal.valueOf(1000)).toString(), "toScaledBigDecimal(Float, int, RoundingMode) 4 failed");
         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(),
             "toScaledBigDecimal(String) 4 failed");
         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(),
             "toScaledBigDecimal(String, int, RoundingMode) 4 failed");
         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() {
         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("", (short) 5));
+        assertEquals(5, NumberUtils.toShort(null, (short) 5));
     }
 }