commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1408713 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/math/NumberUtils.java
Date Tue, 13 Nov 2012 13:21:38 GMT
Author: sebb
Date: Tue Nov 13 13:21:37 2012
New Revision: 1408713

URL: http://svn.apache.org/viewvc?rev=1408713&view=rev
Log:
LANG-856 Code refactoring in NumberUtils

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1408713&r1=1408712&r2=1408713&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Tue Nov 13 13:21:37 2012
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.2" date="TBA" description="Next release">
+    <action issue="LANG-856" type="add">Code refactoring in NumberUtils</action>
     <action issue="LANG-855" type="add">NumberUtils#createBigInteger does not allow
for hex and octal numbers</action>
     <action issue="LANG-854" type="add">NumberUtils#createNumber - does not allow for
hex numbers to be larger than Long</action>
     <action issue="LANG-853" type="add">StringUtils join APIs for primitives</action>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java?rev=1408713&r1=1408712&r2=1408713&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Tue Nov 13 13:21:37 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.lang3.math;
 
+import java.lang.reflect.Array;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 
@@ -766,11 +767,7 @@ public class NumberUtils {
      */
     public static long min(long[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns min
         long min = array[0];
@@ -793,11 +790,7 @@ public class NumberUtils {
      */
     public static int min(int[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns min
         int min = array[0];
@@ -820,11 +813,7 @@ public class NumberUtils {
      */
     public static short min(short[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns min
         short min = array[0];
@@ -847,11 +836,7 @@ public class NumberUtils {
      */
     public static byte min(byte[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns min
         byte min = array[0];
@@ -875,11 +860,7 @@ public class NumberUtils {
      */
     public static double min(double[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns min
         double min = array[0];
@@ -906,11 +887,7 @@ public class NumberUtils {
      */
     public static float min(float[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns min
         float min = array[0];
@@ -938,11 +915,7 @@ public class NumberUtils {
      */
     public static long max(long[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
 
         // Finds and returns max
         long max = array[0];
@@ -965,11 +938,7 @@ public class NumberUtils {
      */
     public static int max(int[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns max
         int max = array[0];
@@ -992,11 +961,7 @@ public class NumberUtils {
      */
     public static short max(short[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns max
         short max = array[0];
@@ -1019,11 +984,7 @@ public class NumberUtils {
      */
     public static byte max(byte[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
     
         // Finds and returns max
         byte max = array[0];
@@ -1047,12 +1008,8 @@ public class NumberUtils {
      */
     public static double max(double[] array) {
         // Validates input
-        if (array== null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
-    
+        validateArray(array);
+
         // Finds and returns max
         double max = array[0];
         for (int j = 1; j < array.length; j++) {
@@ -1078,11 +1035,7 @@ public class NumberUtils {
      */
     public static float max(float[] array) {
         // Validates input
-        if (array == null) {
-            throw new IllegalArgumentException("The Array must not be null");
-        } else if (array.length == 0) {
-            throw new IllegalArgumentException("Array cannot be empty.");
-        }
+        validateArray(array);
 
         // Finds and returns max
         float max = array[0];
@@ -1097,6 +1050,14 @@ public class NumberUtils {
 
         return max;
     }
+
+    private static void validateArray(Object array) {
+        if (array == null) {
+            throw new IllegalArgumentException("The Array must not be null");
+        } else if (Array.getLength(array) == 0) {
+            throw new IllegalArgumentException("Array cannot be empty.");
+        }
+    }
      
     // 3 param min
     //-----------------------------------------------------------------------



Mime
View raw message