commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
Date Wed, 25 Jun 2003 23:33:47 GMT
scolebourne    2003/06/25 16:33:47

  Modified:    lang/src/java/org/apache/commons/lang ArrayUtils.java
               lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
  Log:
  Add primitive boolean/object conversions
  Bug 21068, from Matthew Hawthorne
  
  Revision  Changes    Path
  1.15      +70 -1     jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java
  
  Index: ArrayUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ArrayUtils.java	25 Jun 2003 23:32:08 -0000	1.14
  +++ ArrayUtils.java	25 Jun 2003 23:33:47 -0000	1.15
  @@ -68,6 +68,7 @@
    * @author Moritz Petersen
    * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
    * @author Nikolay Metchev
  + * @author Matthew Hawthorne
    * @since 2.0
    * @version $Id$
    */
  @@ -903,6 +904,74 @@
        */
       public static boolean contains(final Object[] array, final Object objectToFind) {
           return (indexOf(array, objectToFind) != -1);
  +    }
  +
  +    // Primitive/Object converters
  +    // ----------------------------------------------------------------------
  +    /**
  +     * <p>Converts an array of object Booleans to primitives.</p>
  +     *
  +     * <p>This method returns <code>null</code> if <code>null</code>
input.</p>
  +     * 
  +     * @param array  a <code>Boolean</code> array, may be <code>null</code>
  +     * @return a <code>boolean</code> array
  +     * @throws NullPointerException if array content is <code>null</code>
  +     */
  +    public static boolean[] toPrimitive(final Boolean[] array) {
  +        if (array == null) {
  +            return null;
  +        } else if (array.length == 0) {
  +            return EMPTY_BOOLEAN_ARRAY;
  +        }
  +        final boolean[] result = new boolean[array.length];
  +        for (int i = 0; i < array.length; i++) {
  +            result[i] = array[i].booleanValue();
  +        }
  +        return result;
  +    }
  +
  +    /**
  +     * <p>Converts an array of object Booleans to primitives handling null.</p>
  +     * 
  +     * <p>This method returns <code>null</code> if <code>null</code>
input.</p>
  +     * 
  +     * @param array  a <code>Boolean</code> array, may be <code>null</code>
  +     * @param valueForNull  the value to insert if <code>null</code> found
  +     * @return a <code>boolean</code> array
  +     */
  +    public static boolean[] toPrimitive(final Boolean[] array, final boolean valueForNull)
{
  +        if (array == null) {
  +            return null;
  +        } else if (array.length == 0) {
  +            return EMPTY_BOOLEAN_ARRAY;
  +        }
  +        final boolean[] result = new boolean[array.length];
  +        for (int i = 0; i < array.length; i++) {
  +            Boolean b = array[i];
  +            result[i] = (b == null ? valueForNull : b.booleanValue());
  +        }
  +        return result;
  +    }
  +
  +    /**
  +     * <p>Converts an array of primitive booleans to objects.</p>
  +     *
  +     * <p>This method returns <code>null</code> if <code>null</code>
input.</p>
  +     * 
  +     * @param array  a <code>boolean</code> array
  +     * @return a <code>Boolean</code> array
  +     */
  +    public static Boolean[] toObject(final boolean[] array) {
  +        if (array == null) {
  +            return null;
  +        } else if (array.length == 0) {
  +            return EMPTY_BOOLEAN_OBJECT_ARRAY;
  +        }
  +        final Boolean[] result = new Boolean[array.length];
  +        for (int i = 0; i < array.length; i++) {
  +            result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
  +        }
  +        return result;
       }
   
   }
  
  
  
  1.7       +48 -4     jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java
  
  Index: ArrayUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ArrayUtilsTest.java	23 Mar 2003 21:47:30 -0000	1.6
  +++ ArrayUtilsTest.java	25 Jun 2003 23:33:47 -0000	1.7
  @@ -64,9 +64,10 @@
   /**
    * Unit tests {@link org.apache.commons.lang.ArrayUtils}.
    *
  - * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
  + * @author Stephen Colebourne
    * @author Moritz Petersen
    * @author Nikolay Metchev
  + * @author Matthew Hawthorne
    * @version $Id$
    */
   public class ArrayUtilsTest extends TestCase {
  @@ -80,8 +81,8 @@
       }
   
       public static Test suite() {
  -    	TestSuite suite = new TestSuite(ArrayUtilsTest.class);
  -    	suite.setName("ArrayUtils Tests");
  +        TestSuite suite = new TestSuite(ArrayUtilsTest.class);
  +        suite.setName("ArrayUtils Tests");
           return suite;
       }
   
  @@ -706,4 +707,47 @@
           assertEquals(true, ArrayUtils.contains(array, null));
           assertEquals(false, ArrayUtils.contains(array, "notInArray"));
       }
  +    
  +    // testToPrimitive/Object for boolean
  +    //  -----------------------------------------------------------------------
  +    public void testToPrimitive_boolean() {
  +        assertEquals(null, ArrayUtils.toPrimitive(null));
  +        assertSame(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.toPrimitive(new Boolean[0]));
  +        assertTrue(Arrays.equals(
  +            new boolean[] {true, false, true},
  +            ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE}))
  +        );
  +
  +        try {
  +            ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, null});
  +            fail();
  +        } catch (NullPointerException ex) {}
  +    }
  +
  +    public void testToPrimitive_boolean_boolean() {
  +        assertEquals(null, ArrayUtils.toPrimitive(null, false));
  +        assertSame(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.toPrimitive(new Boolean[0],
false));
  +        assertTrue(Arrays.equals(
  +            new boolean[] {true, false, true},
  +            ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE},
false))
  +        );
  +        assertTrue(Arrays.equals(
  +            new boolean[] {true, false, false},
  +            ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, null, Boolean.FALSE}, false))
  +        );
  +        assertTrue(Arrays.equals(
  +            new boolean[] {true, true, false},
  +            ArrayUtils.toPrimitive(new Boolean[] {Boolean.TRUE, null, Boolean.FALSE}, true))
  +        );
  +    }
  +
  +    public void testToObject_boolean() {
  +        assertEquals(null, ArrayUtils.toObject(null));
  +        assertSame(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, ArrayUtils.toObject(new boolean[0]));
  +        assertTrue(Arrays.equals(
  +            new Boolean[] {Boolean.TRUE, Boolean.FALSE, Boolean.TRUE},
  +            ArrayUtils.toObject(new boolean[] {true, false, true}))
  +        );
  +    }
  +
   }
  
  
  

---------------------------------------------------------------------
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