You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by dj...@apache.org on 2015/04/09 08:27:16 UTC

svn commit: r1672244 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/ArrayUtils.java test/java/org/apache/commons/lang3/ArrayUtilsTest.java

Author: djones
Date: Thu Apr  9 06:27:15 2015
New Revision: 1672244

URL: http://svn.apache.org/r1672244
Log:
Implements LANG-1050: Change nullToEmpty methods to generics. Thanks to James Sawle. This closes #33 in GitHub too.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    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/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1672244&r1=1672243&r2=1672244&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Thu Apr  9 06:27:15 2015
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.5" date="tba" description="tba">
+    <action issue="LANG-1050" type="add" dev="djones" due-to="James Sawle">Change nullToEmpty methods to generics</action>
     <action issue="LANG-1111" type="fix" dev="chas">Fix FindBugs warnings in DurationFormatUtils</action>
     <action issue="LANG-1074" type="add" dev="djones" due-to="Haiyang Li">Add a method to ArrayUtils for removing all occurrences of a given element</action>
     <action issue="LANG-1107" type="update" dev="chas">Fix parsing edge cases in FastDateParser</action>

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=1672244&r1=1672243&r2=1672244&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 Thu Apr  9 06:27:15 2015
@@ -464,6 +464,29 @@ public class ArrayUtils {
      *
      * <p>This method returns an empty array for a {@code null} input array.</p>
      *
+     * @param array  the array to check for {@code null} or empty
+     * @param type   the class representation of the desired array
+     * @return the same array, {@code public static} empty array if {@code null}
+     * @throws IllegalArgumentException if the type argument is null
+     */
+    public static <T> T[] nullToEmpty(final T[] array, final Class<T[]> type) {
+        if(type == null) {
+            throw new IllegalArgumentException("The type must not be null");
+        }
+
+        if(array == null) {
+            return type.cast(Array.newInstance(type.getComponentType(), 0));
+        }
+        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>
      *

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=1672244&r1=1672243&r2=1672244&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 Thu Apr  9 06:27:15 2015
@@ -356,6 +356,37 @@ public class ArrayUtilsTest  {
 
     //-----------------------------------------------------------------------
 
+    private class TestClass{}
+    
+    @Test
+    public void testNullToEmptyGenericNull() {
+        TestClass[] output = ArrayUtils.nullToEmpty(null, TestClass[].class);
+    
+        assertTrue(output != null);
+        assertTrue(output.length == 0);
+    }
+    
+    @Test
+    public void testNullToEmptyGenericEmpty() {
+        TestClass[] input = new TestClass[]{};
+        TestClass[] output = ArrayUtils.nullToEmpty(input, TestClass[].class);
+    
+        assertSame(input, output);
+    }
+    
+    @Test
+    public void testNullToEmptyGeneric() {
+        TestClass[] input = new TestClass[]{new TestClass(), new TestClass()};
+        TestClass[] output = ArrayUtils.nullToEmpty(input, TestClass[].class);
+    
+        assertSame(input, output);
+    }
+    
+    @Test(expected=IllegalArgumentException.class)
+    public void testNullToEmptyGenericNullType() {
+        TestClass[] input = new TestClass[]{};
+        ArrayUtils.nullToEmpty(input, null);
+    }    
 
     @Test
     public void testNullToEmptyBooleanNull() throws Exception {