commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r419122 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/ArrayUtils.java test/org/apache/commons/lang/ArrayUtilsTest.java
Date Wed, 05 Jul 2006 00:04:28 GMT
Author: bayard
Date: Tue Jul  4 17:04:28 2006
New Revision: 419122

URL: http://svn.apache.org/viewvc?rev=419122&view=rev
Log:
Adding the missing char/Character methods in ArrayUtils, as pointed out (and patched) by Andres
Almiray in LANG-267

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

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java?rev=419122&r1=419121&r2=419122&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java Tue
Jul  4 17:04:28 2006
@@ -2266,6 +2266,80 @@
 
     // Primitive/Object array converters
     // ----------------------------------------------------------------------
+
+    // Character array converters
+    // ----------------------------------------------------------------------
+    /**
+     * <p>Converts an array of object Characters to primitives.</p>
+     *
+     * <p>This method returns <code>null</code> for a <code>null</code>
input array.</p>
+     * 
+     * @param array  a <code>Character</code> array, may be <code>null</code>
+     * @return a <code>char</code> array, <code>null</code> if null
array input
+     * @throws NullPointerException if array content is <code>null</code>
+     */
+    public static char[] toPrimitive(Character[] array) {
+        if (array == null) {
+            return null;
+        } else if (array.length == 0) {
+            return EMPTY_CHAR_ARRAY;
+        }
+        final char[] result = new char[array.length];
+        for (int i = 0; i < array.length; i++) {
+            result[i] = array[i].charValue();
+        }
+        return result;
+    }
+
+    /**
+     * <p>Converts an array of object Character to primitives handling <code>null</code>.</p>
+     * 
+     * <p>This method returns <code>null</code> for a <code>null</code>
input array.</p>
+     * 
+     * @param array  a <code>Character</code> array, may be <code>null</code>
+     * @param valueForNull  the value to insert if <code>null</code> found
+     * @return a <code>char</code> array, <code>null</code> if null
array input
+     */
+    public static char[] toPrimitive(Character[] array, char valueForNull) {
+        if (array == null) {
+            return null;
+        } else if (array.length == 0) {
+            return EMPTY_CHAR_ARRAY;
+        }
+        final char[] result = new char[array.length];
+        for (int i = 0; i < array.length; i++) {
+            Character b = array[i];
+            result[i] = (b == null ? valueForNull : b.charValue());
+        }
+        return result;
+    }
+    
+    /**
+     * Converts an array of primitive chars to objects.<br>
+     * <p>
+     * <strong>This is method is not in ArrayUtils.</strong>
+     * </p>
+     * <p>
+     * This method returns <code>null</code> for a <code>null</code>
input
+     * array.
+     * </p>
+     * 
+     * @param array a <code>char</code> array
+     * @return a <code>Character</code> array, <code>null</code>
if null
+     *         array input
+     */
+    public static Object[] toObject(char[] array) {
+        if ( array == null ) {
+            return null;
+        }else if( array.length == 0 ){
+            return EMPTY_CHARACTER_OBJECT_ARRAY;
+        }
+        final Character[] result = new Character[array.length];
+        for( int i = 0; i < array.length; i++ ){
+            result[i] = new Character( array[i] );
+        }
+        return result;
+     }    
     
     // Long array converters
     // ----------------------------------------------------------------------

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java?rev=419122&r1=419121&r2=419122&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java
Tue Jul  4 17:04:28 2006
@@ -2023,6 +2023,62 @@
 
     // testToPrimitive/Object for byte
     //  -----------------------------------------------------------------------
+    public void testToPrimitive_char() {
+        final Character[] b = null;
+        assertEquals(null, ArrayUtils.toPrimitive(b));
+        
+        assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.toPrimitive(new Character[0]));
+        
+        assertTrue(Arrays.equals(
+            new char[] {Character.MIN_VALUE, Character.MAX_VALUE, '0'},
+            ArrayUtils.toPrimitive(new Character[] {new Character(Character.MIN_VALUE), 
+                new Character(Character.MAX_VALUE), new Character('0')}))
+        );
+
+        try {
+            ArrayUtils.toPrimitive(new Character[] {new Character(Character.MIN_VALUE), null});
+            fail();
+        } catch (NullPointerException ex) {}
+    }
+
+    public void testToPrimitive_char_char() {
+        final Character[] b = null;
+        assertEquals(null, ArrayUtils.toPrimitive(b, Character.MIN_VALUE));
+        
+        assertSame(ArrayUtils.EMPTY_CHAR_ARRAY, 
+            ArrayUtils.toPrimitive(new Character[0], (char)0));
+        
+        assertTrue(Arrays.equals(
+            new char[] {Character.MIN_VALUE, Character.MAX_VALUE, '0'},
+            ArrayUtils.toPrimitive(new Character[] {new Character(Character.MIN_VALUE), 
+                new Character(Character.MAX_VALUE), new Character('0')}, 
+                Character.MIN_VALUE))
+        );
+        
+        assertTrue(Arrays.equals(
+            new char[] {Character.MIN_VALUE, Character.MAX_VALUE, '0'},
+            ArrayUtils.toPrimitive(new Character[] {new Character(Character.MIN_VALUE), null,

+                new Character('0')}, Character.MAX_VALUE))
+        );
+    }
+
+    public void testToObject_char() {
+        final char[] b = null;
+        assertEquals(null, ArrayUtils.toObject(b));
+        
+        assertSame(ArrayUtils.EMPTY_CHARACTER_OBJECT_ARRAY, 
+            ArrayUtils.toObject(new char[0]));
+        
+        assertTrue(Arrays.equals(
+            new Character[] {new Character(Character.MIN_VALUE), 
+                new Character(Character.MAX_VALUE), new Character('0')},
+                ArrayUtils.toObject(new char[] {Character.MIN_VALUE, Character.MAX_VALUE,

+                '0'} ))
+        );
+    }
+    
+    // testToPrimitive/Object for byte
+    //  -----------------------------------------------------------------------
     public void testToPrimitive_byte() {
         final Byte[] b = null;
         assertEquals(null, ArrayUtils.toPrimitive(b));



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message