You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/11/29 13:56:16 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang ArrayUtilsTest.java

scolebourne    2003/11/29 04:56:16

  Modified:    lang/src/java/org/apache/commons/lang ArrayUtils.java
               lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
  Log:
  Add subArray method to get a portion of an array
  from Ash
  
  Revision  Changes    Path
  1.26      +44 -5     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.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- ArrayUtils.java	22 Aug 2003 17:25:33 -0000	1.25
  +++ ArrayUtils.java	29 Nov 2003 12:56:15 -0000	1.26
  @@ -62,7 +62,7 @@
   import org.apache.commons.lang.builder.ToStringStyle;
   
   /**
  - * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and primitive wrapper arrays 
  + * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and primitive wrapper arrays
    * (like <code>Integer[]</code>).</p>
    * 
    * <p>This class tries to handle <code>null</code> input gracefully.
  @@ -78,6 +78,7 @@
    * @author Tim O'Brien
    * @author Pete Gieser
    * @author Gary Gregory
  + * @author Ash
    * @since 2.0
    * @version $Id$
    */
  @@ -251,7 +252,7 @@
        * 
        * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
        *
  -     * @param array  an array whose elements are either a {@link java.util.Map.Entry} or 
  +     * @param array  an array whose elements are either a {@link java.util.Map.Entry} or
        *  an Array containing at least two elements, may be <code>null</code>
        * @return a <code>Map</code> that was created from the array
        * @throws IllegalArgumentException  if one element of this Array is
  @@ -272,13 +273,13 @@
               } else if (object instanceof Object[]) {
                   Object[] entry = (Object[]) object;
                   if (entry.length < 2) {
  -                    throw new IllegalArgumentException("Array element " + i + ", '" 
  +                    throw new IllegalArgumentException("Array element " + i + ", '"
                           + object
                           + "', has a length less than 2");
                   }
                   map.put(entry[0], entry[1]);
               } else {
  -                throw new IllegalArgumentException("Array element " + i + ", '" 
  +                throw new IllegalArgumentException("Array element " + i + ", '"
                           + object
                           + "', is neither of type Map.Entry nor an Array");
               }
  @@ -433,6 +434,44 @@
               return null;
           }
           return (boolean[]) array.clone();
  +    }
  +
  +    // Subarrays
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Produces a new array containing the elements between
  +     * the start and end indices.</p>
  +     * 
  +     * <p>The start index is inclusive, the end index exclusive.
  +     * Null array input produces null output.
  +     * The result is always an <code>Object[]</code> instance</p>
  +     *
  +     * @param array  the array
  +     * @param startIndex  the starting index. Undervalue (&lt;0)
  +     *      is promoted to 0, overvalue (&gt;array.length) results
  +     *      in an empty array.
  +     * @param endIndex  elements upto endIndex-1 are present in the
  +     *      returned subarray. Undervalue (&lt; startIndex) produces
  +     *      empty array, overvalue (&gt;array.length) is demoted to
  +     *      array length.
  +     */
  +    public static Object[] subArray(Object[] array, int startIndexInclusive, int endIndexExclusive) {
  +        if (array == null) {
  +            return null;
  +        }
  +        if (startIndexInclusive < 0) {
  +            startIndexInclusive = 0;
  +        }
  +        if (endIndexExclusive > array.length) {
  +            endIndexExclusive = array.length;
  +        }
  +        int newSize = endIndexExclusive - startIndexInclusive;
  +        if (newSize <= 0) {
  +            return EMPTY_OBJECT_ARRAY;
  +        }
  +        Object[] subArray = new Object[newSize];
  +        System.arraycopy(array, startIndexInclusive, subArray, 0, newSize);
  +        return subArray;
       }
   
       // Is same length
  
  
  
  1.15      +44 -1     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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ArrayUtilsTest.java	11 Oct 2003 19:58:40 -0000	1.14
  +++ ArrayUtilsTest.java	29 Nov 2003 12:56:16 -0000	1.15
  @@ -272,6 +272,49 @@
       }
   
       //-----------------------------------------------------------------------
  +
  +    public void testSubArray() {
  +        Object[] inarray = { "a", "b", "c", "d", "e", "f"};
  +
  +        assertEquals("0 start, mid end", "abcd",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 0, 4)));
  +        assertEquals("0 start, length end", "abcdef",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 0, inarray.length)));
  +        assertEquals("mid start, mid end", "bcd",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 1, 4)));
  +        assertEquals("mid start, length end", "bcdef",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 1, inarray.length)));
  +
  +        assertNull("null input", ArrayUtils.subArray(null, 0, 3));
  +        assertEquals("empty array", "",
  +            StringUtils.join(ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2)));
  +        assertEquals("start > end", "",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 4, 2)));
  +        assertEquals("start == end", "",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 3, 3)));
  +        assertEquals("start undershoot, normal end", "abcd",
  +            StringUtils.join(ArrayUtils.subArray(inarray, -2, 4)));
  +        assertEquals("start overshoot, any end", "",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 33, 4)));
  +        assertEquals("normal start, end overshoot", "cdef",
  +            StringUtils.join(ArrayUtils.subArray(inarray, 2, 33)));
  +        assertEquals("start undershoot, end overshoot", "abcdef",
  +            StringUtils.join(ArrayUtils.subArray(inarray, -2, 12)));
  +            
  +        // object-level tests
  +        assertSame("empty array, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
  +            ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2));
  +        assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
  +            ArrayUtils.subArray(inarray, 4, 1));
  +        assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
  +            ArrayUtils.subArray(inarray, 33, 1));
  +        assertSame("start == end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
  +            ArrayUtils.subArray(inarray, 3, 3));
  +        assertSame("start overshoot, any end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
  +            ArrayUtils.subArray(inarray, 8733, 4));
  +    }
  +
  +    //-----------------------------------------------------------------------
       public void testSameLength() {
           Object[] nullArray = null;
           Object[] emptyArray = new Object[0];
  
  
  

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