commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djo...@apache.org
Subject svn commit: r1672025 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/ArrayUtils.java test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
Date Wed, 08 Apr 2015 07:53:50 GMT
Author: djones
Date: Wed Apr  8 07:53:49 2015
New Revision: 1672025

URL: http://svn.apache.org/r1672025
Log:
Implements LANG-1074: Add a method to ArrayUtils for removing all occurrences of a given element.
Thanks to Haiyang Li.

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/ArrayUtilsRemoveTest.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=1672025&r1=1672024&r2=1672025&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Wed Apr  8 07:53:49 2015
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.5" date="tba" description="tba">
+    <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>
   </release>
 

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=1672025&r1=1672024&r2=1672025&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 Wed Apr
 8 07:53:49 2015
@@ -6295,4 +6295,275 @@ public class ArrayUtils {
         }
         return true;
     }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static boolean[] removeAllOccurences(final boolean[] array, final boolean element)
{
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));        
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static char[] removeAllOccurences(final char[] array, final char element) {
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));        
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static byte[] removeAllOccurences(final byte[] array, final byte element) {
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static short[] removeAllOccurences(final short[] array, final short element) {
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static int[] removeAllOccurences(final int[] array, final int element) {
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static long[] removeAllOccurences(final long[] array, final long element) {
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static float[] removeAllOccurences(final float[] array, final float element) {
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static double[] removeAllOccurences(final double[] array, final double element)
{
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));
+    }
+
+    /**
+     * <p>
+     * Removes the occurrences of the specified element from the specified array. All subsequent
elements are shifted to
+     * the left (subtracts one from their indices). If the array doesn't contains such an
element, no elements are
+     * removed from the array. <code>null</code> will be returned if the input
array is <code>null</code>
+     * </p>
+     * 
+     * @param <T> the type of object in the array
+     * @param element the element to remove
+     * @param array the input array
+     * 
+     * @return A new array containing the existing elements except the occurrences of the
specified element.
+     * @since 3.5
+     */
+    public static <T> T[] removeAllOccurences(final T[] array, final T element) {
+        int index = indexOf(array, element);
+        if (index == INDEX_NOT_FOUND) {
+            return clone(array);
+        }
+        
+        int[] indices = new int[array.length - index];
+        indices[0] = index;
+        int count = 1;
+        
+        while ((index = indexOf(array, element, indices[count - 1] + 1)) != INDEX_NOT_FOUND)
{
+            indices[count++] = index;
+        }
+        
+        return removeAll(array, Arrays.copyOf(indices, count));
+    }
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java?rev=1672025&r1=1672024&r2=1672025&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
Wed Apr  8 07:53:49 2015
@@ -484,4 +484,193 @@ public class ArrayUtilsRemoveTest {
         assertEquals(Short.TYPE, array.getClass().getComponentType());
     }
     
+
+    @Test
+    public void testRemoveAllBooleanOccurences() {
+        boolean[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, true));
+
+        a = new boolean[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurences(a,
true)));
+
+        a = new boolean[] { true };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurences(a,
true)));
+
+        a = new boolean[] { true, true };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurences(a,
true)));
+
+        a = new boolean[] { false, true, true, false, true };
+        assertTrue(Arrays.equals(new boolean[] { false, false }, ArrayUtils.removeAllOccurences(a,
true)));
+
+        a = new boolean[] { false, true, true, false, true };
+        assertTrue(Arrays.equals(new boolean[] { true, true, true }, ArrayUtils.removeAllOccurences(a,
false)));
+    }
+
+    @Test
+    public void testRemoveAllCharOccurences() {
+        char[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, '2'));
+
+        a = new char[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurences(a,
'2')));
+
+        a = new char[] { '2' };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurences(a,
'2')));
+
+        a = new char[] { '2', '2' };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurences(a,
'2')));
+
+        a = new char[] { '1', '2', '2', '3', '2' };
+        assertTrue(Arrays.equals(new char[] { '1', '3' }, ArrayUtils.removeAllOccurences(a,
'2')));
+
+        a = new char[] { '1', '2', '2', '3', '2' };
+        assertTrue(Arrays.equals(new char[] { '1', '2', '2', '3', '2' }, ArrayUtils.removeAllOccurences(a,
'4')));
+    }
+    
+    @Test
+    public void testRemoveAllByteOccurences() {
+        byte[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, (byte) 2));
+
+        a = new byte[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurences(a,
(byte) 2)));
+
+        a = new byte[] { 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurences(a,
(byte) 2)));
+
+        a = new byte[] { 2, 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurences(a,
(byte) 2)));
+
+        a = new byte[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new byte[] { 1, 3 }, ArrayUtils.removeAllOccurences(a, (byte)
2)));
+
+        a = new byte[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new byte[] { 1, 2, 2, 3, 2 }, ArrayUtils.removeAllOccurences(a,
(byte) 4)));
+    }
+
+    @Test
+    public void testRemoveAllShortOccurences() {
+        short[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, (short) 2));
+
+        a = new short[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurences(a,
(short) 2)));
+
+        a = new short[] { 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurences(a,
(short) 2)));
+
+        a = new short[] { 2, 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurences(a,
(short) 2)));
+
+        a = new short[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new short[] { 1, 3 }, ArrayUtils.removeAllOccurences(a,
(short) 2)));
+
+        a = new short[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new short[] { 1, 2, 2, 3, 2 }, ArrayUtils.removeAllOccurences(a,
(short) 4)));
+    }
+
+    @Test
+    public void testRemoveAllIntOccurences() {        
+        int[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, 2));
+
+        a = new int[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new int[] { 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new int[] { 2, 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new int[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new int[] { 1, 3 }, ArrayUtils.removeAllOccurences(a, 2)));
+
+        a = new int[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new int[] { 1, 2, 2, 3, 2 }, ArrayUtils.removeAllOccurences(a,
4)));
+    }    
+    
+    @Test
+    public void testRemoveAllLongOccurences() {        
+        long[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, 2));
+
+        a = new long[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new long[] { 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new long[] { 2, 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new long[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new long[] { 1, 3 }, ArrayUtils.removeAllOccurences(a, 2)));
+
+        a = new long[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new long[] { 1, 2, 2, 3, 2 }, ArrayUtils.removeAllOccurences(a,
4)));
+    }
+
+    @Test
+    public void testRemoveAllFloatOccurences() {    
+        float[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, 2));
+
+        a = new float[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new float[] { 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new float[] { 2, 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new float[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new float[] { 1, 3 }, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new float[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new float[] { 1, 2, 2, 3, 2 }, ArrayUtils.removeAllOccurences(a,
4)));
+    }
+
+    @Test
+    public void testRemoveAllDoubleOccurences() {    
+        double[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, 2));
+
+        a = new double[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new double[] { 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new double[] { 2, 2 };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new double[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new double[] { 1, 3 }, ArrayUtils.removeAllOccurences(a,
2)));
+
+        a = new double[] { 1, 2, 2, 3, 2 };
+        assertTrue(Arrays.equals(new double[] { 1, 2, 2, 3, 2 }, ArrayUtils.removeAllOccurences(a,
4)));
+    }
+
+    @Test
+    public void testRemoveAllObjectOccurences() {    
+        String[] a = null;
+        assertNull(ArrayUtils.removeAllOccurences(a, "2"));
+
+        a = new String[0];
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurences(a,
"2")));
+
+        a = new String[] { "2" };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurences(a,
"2")));
+
+        a = new String[] { "2", "2" };
+        assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurences(a,
"2")));
+
+        a = new String[] { "1", "2", "2", "3", "2" };
+        assertTrue(Arrays.equals(new String[] { "1", "3" }, ArrayUtils.removeAllOccurences(a,
"2")));
+
+        a = new String[] { "1", "2", "2", "3", "2" };
+        assertTrue(Arrays.equals(new String[] { "1", "2", "2", "3", "2" }, ArrayUtils.removeAllOccurences(a,
"4")));
+    }
 }



Mime
View raw message