commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1531399 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ArrayUtils.java test/java/org/apache/commons/lang3/ArrayUtilsTest.java
Date Fri, 11 Oct 2013 19:36:39 GMT
Author: mbenson
Date: Fri Oct 11 19:36:39 2013
New Revision: 1531399

URL: http://svn.apache.org/r1531399
Log:
[LANG-920] Add ArrayUtils#nullToEmpty(Class<?>[])

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java?rev=1531399&r1=1531398&r2=1531399&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ArrayUtils.java Fri Oct
11 19:36:39 2013
@@ -484,6 +484,26 @@ public class ArrayUtils {
      *
      * @param array  the array to check for {@code null} or empty
      * @return the same array, {@code public static} empty array if {@code null} or empty
input
+     * @since 3.2
+     */
+    public static Class<?>[] nullToEmpty(final Class<?>[] array) {
+        if (array == null || array.length == 0) {
+            return EMPTY_CLASS_ARRAY;
+        }
+        return array;
+    }
+
+    /**
+     * <p>Defensive programming technique to change a {@code null}
+     * reference to an empty one.</p>
+     *
+     * <p>This method returns an empty array for a {@code null} input array.</p>
+     *
+     * <p>As a memory optimizing technique an empty array passed in will be overridden
with
+     * the empty {@code public static} references in this class.</p>
+     *
+     * @param array  the array to check for {@code null} or empty
+     * @return the same array, {@code public static} empty array if {@code null} or empty
input
      * @since 2.5
      */
     public static String[] nullToEmpty(final String[] array) {

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java?rev=1531399&r1=1531398&r2=1531399&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java Fri
Oct 11 19:36:39 2013
@@ -489,6 +489,20 @@ public class ArrayUtilsTest  {
     }
 
     @Test
+    public void testNullToEmptyClass() {
+        // Test null handling
+        assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.nullToEmpty((Class<?>[])
null));
+        // Test valid array handling
+        final Class<?>[] original = { Object.class, String.class };
+        assertArrayEquals(original, ArrayUtils.nullToEmpty(original));
+        // Test empty array handling
+        final Class<?>[] empty = {};
+        final Class<?>[] result = ArrayUtils.nullToEmpty(empty);
+        assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, result);
+        assertTrue(empty != result);
+    }
+
+    @Test
     public void testNullToEmptyString() {
         // Test null handling
         assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.nullToEmpty((String[])
null));



Mime
View raw message