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 Tue, 04 Feb 2003 22:06:24 GMT
scolebourne    2003/02/04 14:06:24

  Modified:    lang/src/java/org/apache/commons/lang ArrayUtils.java
               lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
  Log:
  Add support for indexOf, lastIndexOf and contains for ArrayUtils
  from Nikolay Metchev, bug ref 15438
  
  Revision  Changes    Path
  1.8       +139 -16   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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ArrayUtils.java	23 Dec 2002 00:32:24 -0000	1.7
  +++ ArrayUtils.java	4 Feb 2003 22:06:24 -0000	1.8
  @@ -1,7 +1,7 @@
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -67,6 +67,7 @@
    * @author Stephen Colebourne
    * @author Moritz Petersen
    * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
  + * @author Nikolay Metchev
    * @since 2.0
    * @version $Id$
    */
  @@ -104,7 +105,7 @@
       }
   
       // Basic methods handling multi-dimensional arrays
  -    //--------------------------------------------------------------------------
  +    //-----------------------------------------------------------------------
       
       /**
        * <p>Outputs an array as a String, treating <code>null</code> as
an empty array.</p>
  @@ -166,7 +167,7 @@
           return new EqualsBuilder().append(array1, array2).isEquals();
       }
       
  -    //--------------------------------------------------------------------------
  +    //-----------------------------------------------------------------------
       
       /**
        * <p>Converts the given array into a {@link Map}. Each element of the array
  @@ -324,7 +325,7 @@
   //        return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
   //    }
       
  -    //--------------------------------------------------------------------------
  +    //-----------------------------------------------------------------------
   
       /**
        * <p>Shallow clones an array returning a typecast result and handling
  @@ -480,7 +481,7 @@
           return (boolean[]) array.clone();
       }
   
  -    //--------------------------------------------------------------------------
  +    //-----------------------------------------------------------------------
   
       /**
        * <p>Checks whether two arrays are the same length, treating
  @@ -680,6 +681,8 @@
           return array1.getClass().getName().equals(array2.getClass().getName());
       }
       
  +    //-----------------------------------------------------------------------
  +    
       /** 
        * Reverses the order of the given array.
        * <p>
  @@ -687,7 +690,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(Object[] array) {
           if (array == null) {
  @@ -710,7 +713,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(long[] array) {
           if (array == null) {
  @@ -733,7 +736,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(int[] array) {
           if (array == null) {
  @@ -756,7 +759,7 @@
        * <p>
        * There is no special handling for multi-dimensional arrays.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(short[] array) {
           if (array == null) {
  @@ -779,7 +782,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(char[] array) {
           if (array == null) {
  @@ -802,7 +805,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(byte[] array) {
           if (array == null) {
  @@ -825,7 +828,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(double[] array) {
           if (array == null) {
  @@ -848,7 +851,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(float[] array) {
           if (array == null) {
  @@ -871,7 +874,7 @@
        * <p>
        * The method does nothing if <code>null</code> is passed in.
        * 
  -     * @param array  the array to reverse
  +     * @param array  the array to reverse, may be <code>null</code>
        */
       public static void reverse(boolean[] array) {
           if (array == null) {
  @@ -888,5 +891,125 @@
               i++;
           }
       }
  -
  +    
  +    //-----------------------------------------------------------------------
  +    
  +    /**
  +     * Find the index of the given object in the array.
  +     * <p>
  +     * The method returns -1 if a <code>null</code> array is passed in.
  +     * 
  +     * @param array  the array to search through for the object, may be <code>null</code>
  +     * @param objectToFind  the object to find, may be <code>null</code>
  +     * @return the index of the object within the array, or -1 if not found
  +     */
  +    public static int indexOf(Object[] array, Object objectToFind) {
  +        return indexOf(array, objectToFind, 0);
  +    }
  +    
  +    /**
  +     * Find the index of the given object in the array starting at the given index.
  +     * <p>
  +     * The method returns -1 if a <code>null</code> array is passed in.
  +     * <p>
  +     * A negative startIndex is treated as zero. A startIndex larger than the array
  +     * length will return -1.
  +     * 
  +     * @param array  the array to search through for the object, may be <code>null</code>
  +     * @param objectToFind  the object to find, may be <code>null</code>
  +     * @param startIndex  the index to start searching at
  +     * @return the index of the object within the array starting at the
  +     *  given index, or -1 if not found
  +     */
  +    public static int indexOf(Object[] array, Object objectToFind, int startIndex) {
  +        if (array == null) {
  +            return -1;
  +        }
  +        if (startIndex < 0) {
  +            startIndex = 0;
  +        }
  +        if (objectToFind == null) {
  +            for (int i = startIndex; i < array.length; i++) {
  +                if (array[i] == null) {
  +                    return i;
  +                }
  +            }
  +        } else {
  +            for (int i = startIndex; i < array.length; i++) {
  +                if (objectToFind.equals(array[i])) {
  +                    return i;
  +                }
  +            }
  +        }
  +        return -1;
  +    }
  +    
  +    /**
  +     * Find the last index of the given object within the array.
  +     * <p>
  +     * The method returns -1 if a <code>null</code> array is passed in.
  +     * 
  +     * @param array  the array to travers backwords looking for the object, may be <code>null</code>
  +     * @param objectToFind  the object to find, may be <code>null</code>
  +     * @return the last index of the object to find, or -1 if not found
  +     */
  +    public static int lastIndexOf(Object[] array, Object objectToFind) {
  +        if (array == null) {
  +            return -1;
  +        }
  +        return lastIndexOf(array, objectToFind, array.length - 1);
  +    }
  +    
  +    /**
  +     * Find the last index of the given object in the array starting at the given index.
  +     * <p>
  +     * The method returns -1 if a <code>null</code> array is passed in.
  +     * <p>
  +     * A negative startIndex will return -1. A startIndex larger than the array
  +     * length will search from the end of the array.
  +     * 
  +     * @param array  the array to traverse for looking for the object, may be <code>null</code>
  +     * @param objectToFind  the object to find, may be <code>null</code>
  +     * @param startIndex  the start index to travers backwards from
  +     * @return the last index of the object within the array starting at the given index,
  +     *  or -1 if not found
  +     */
  +    public static int lastIndexOf(Object[] array, Object objectToFind, int startIndex)
{
  +        if (array == null) {
  +            return -1;
  +        }
  +        if (startIndex < 0) {
  +            return -1;
  +        } else if (startIndex >= array.length) {
  +            startIndex = array.length - 1;
  +        }
  +        if (objectToFind == null) {
  +            for (int i = startIndex; i >= 0; i--) {
  +                if (array[i] == null) {
  +                    return i;
  +                }
  +            }
  +        } else {
  +            for (int i = startIndex; i >= 0; i--) {
  +                if (objectToFind.equals(array[i])) {
  +                    return i;
  +                }
  +            }
  +        }
  +        return -1;
  +    }
  +    
  +    /**
  +     * Checks if the object is in the given array.
  +     * <p>
  +     * The method returns <code>false</code> if a <code>null</code>
array is passed in.
  +     * 
  +     * @param array  the array to search through
  +     * @param objectToFind  the object to find
  +     * @return <code>true</code> if the array contains the object
  +     */
  +    public static boolean contains(Object[] array, Object objectToFind) {
  +        return (indexOf(array, objectToFind) != -1);
  +    }
  +    
   }
  
  
  
  1.5       +70 -2     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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ArrayUtilsTest.java	15 Dec 2002 15:00:46 -0000	1.4
  +++ ArrayUtilsTest.java	4 Feb 2003 22:06:24 -0000	1.5
  @@ -1,7 +1,7 @@
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -65,6 +65,7 @@
    *
    * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
    * @author Moritz Petersen
  + * @author Nikolay Metchev
    * @version $Id$
    */
   public class ArrayUtilsTest extends TestCase {
  @@ -637,4 +638,71 @@
           assertEquals(null, array);
       }
       
  +    //-----------------------------------------------------------------------
  +    public void testIndexOf() {
  +        Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
  +        assertEquals(-1, ArrayUtils.indexOf(null, null));
  +        assertEquals(-1, ArrayUtils.indexOf(null, "0"));
  +        assertEquals(0, ArrayUtils.indexOf(array, "0"));
  +        assertEquals(1, ArrayUtils.indexOf(array, "1"));
  +        assertEquals(2, ArrayUtils.indexOf(array, "2"));
  +        assertEquals(3, ArrayUtils.indexOf(array, "3"));
  +        assertEquals(4, ArrayUtils.indexOf(array, null));
  +        assertEquals(-1, ArrayUtils.indexOf(array, "notInArray"));
  +    }
  +
  +    public void testIndexOfWithStartIndex() {
  +        Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
  +        assertEquals(-1, ArrayUtils.indexOf(null, null, 2));
  +        assertEquals(-1, ArrayUtils.indexOf(null, "0", 2));
  +        assertEquals(5, ArrayUtils.indexOf(array, "0", 2));
  +        assertEquals(-1, ArrayUtils.indexOf(array, "1", 2));
  +        assertEquals(2, ArrayUtils.indexOf(array, "2", 2));
  +        assertEquals(3, ArrayUtils.indexOf(array, "3", 2));
  +        assertEquals(4, ArrayUtils.indexOf(array, null, 2));
  +        assertEquals(-1, ArrayUtils.indexOf(array, "notInArray"));
  +        
  +        assertEquals(4, ArrayUtils.indexOf(array, null, -1));
  +        assertEquals(-1, ArrayUtils.indexOf(array, "0", 6));
  +    }
  +
  +    public void testLastIndexOf() {
  +        Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
  +        assertEquals(-1, ArrayUtils.lastIndexOf(null, null));
  +        assertEquals(-1, ArrayUtils.lastIndexOf(null, "0"));
  +        assertEquals(5, ArrayUtils.lastIndexOf(array, "0"));
  +        assertEquals(1, ArrayUtils.lastIndexOf(array, "1"));
  +        assertEquals(2, ArrayUtils.lastIndexOf(array, "2"));
  +        assertEquals(3, ArrayUtils.lastIndexOf(array, "3"));
  +        assertEquals(4, ArrayUtils.lastIndexOf(array, null));
  +        assertEquals(-1, ArrayUtils.lastIndexOf(array, "notInArray"));
  +    }
  +
  +    public void testLastIndexOfWithStartIndex() {
  +        Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
  +        assertEquals(-1, ArrayUtils.lastIndexOf(null, null, 2));
  +        assertEquals(-1, ArrayUtils.lastIndexOf(null, "0", 2));
  +        assertEquals(0, ArrayUtils.lastIndexOf(array, "0", 2));
  +        assertEquals(1, ArrayUtils.lastIndexOf(array, "1", 2));
  +        assertEquals(2, ArrayUtils.lastIndexOf(array, "2", 2));
  +        assertEquals(-1, ArrayUtils.lastIndexOf(array, "3", 2));
  +        assertEquals(4, ArrayUtils.lastIndexOf(array, null, 5));
  +        assertEquals(-1, ArrayUtils.lastIndexOf(array, null, 2));
  +        assertEquals(-1, ArrayUtils.lastIndexOf(array, "notInArray"));
  +        
  +        assertEquals(-1, ArrayUtils.lastIndexOf(array, null, -1));
  +        assertEquals(5, ArrayUtils.lastIndexOf(array, "0", 88));
  +    }
  +
  +    public void testContains() {
  +        Object[] array = new Object[] { "0", "1", "2", "3", null, "0" };
  +        assertEquals(false, ArrayUtils.contains(null, null));
  +        assertEquals(false, ArrayUtils.contains(null, "1"));
  +        assertEquals(true, ArrayUtils.contains(array, "0"));
  +        assertEquals(true, ArrayUtils.contains(array, "1"));
  +        assertEquals(true, ArrayUtils.contains(array, "2"));
  +        assertEquals(true, ArrayUtils.contains(array, "3"));
  +        assertEquals(true, ArrayUtils.contains(array, null));
  +        assertEquals(false, ArrayUtils.contains(array, "notInArray"));
  +    }
   }
  
  
  

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