diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 45bfc4f61..7569bf7f2 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -3254,6 +3254,59 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given object in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param objectToFind the object to find, may be {@code null} + * @return a BitSet of all the indices of the object within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final Object[] array, final Object objectToFind) { + return indexesOf(array, objectToFind, 0); + } + + /** + * Finds the indices of the given object in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param objectToFind the object to find, may be {@code null} + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the object within the array starting at the index, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final Object[] array, final Object objectToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, objectToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + + + /** *

Finds the last index of the given object within the array. * @@ -3365,6 +3418,57 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final long[] array, final long valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final long[] array, final long valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -3468,6 +3572,57 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final int[] array, final int valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final int[] array, final int valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -3571,6 +3726,57 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final short[] array, final short valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final short[] array, final short valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -3676,6 +3882,57 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final char[] array, final char valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final char[] array, final char valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -3782,6 +4039,57 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final byte[] array, final byte valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final byte[] array, final byte valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -3936,6 +4244,121 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final double[] array, final double valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value within a given tolerance in the array. + * + *

+ * This method will return all the indices of the value which fall between the region + * defined by valueToFind - tolerance and valueToFind + tolerance, each time between the nearest integers. + *

+ * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param tolerance tolerance of the search + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final double[] array, final double valueToFind, final double tolerance) { + return indexesOf(array, valueToFind, 0, tolerance); + } + + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final double[] array, final double valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

+ * This method will return the indices of the values which fall between the region + * defined by valueToFind - tolerance and valueToFind + tolerance, between the nearest integers. + *

+ * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @param tolerance tolerance of the search + * @return a BitSet of the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final double[] array, final double valueToFind, int startIndex, final double tolerance) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex, tolerance); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -4109,6 +4532,57 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final float[] array, final float valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return empty BitSet.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final float[] array, final float valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -4213,6 +4687,58 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } + /** + * Finds the indices of the given value in the array. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return a BitSet of all the the indices of the value within the array, + * an empty BitSet if not found or {@code null} array input + * @since 3.10 + */ + public static BitSet indexesOf(final boolean[] array, final boolean valueToFind) { + return indexesOf(array, valueToFind, 0); + } + + /** + * Finds the indices of the given value in the array starting at the given index. + * + *

This method returns an empty BitSet for a {@code null} input array.

+ * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return an empty BitSet ({@code -1}).

+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return a BitSet of all the indices of the value within the array, + * an empty BitSet if not found or {@code null} + * array input + * @since 3.10 + */ + public static BitSet indexesOf(final boolean[] array, final boolean valueToFind, int startIndex) { + BitSet bitSet = new BitSet(); + + if (array == null) { + return bitSet; + } + + while (startIndex < array.length) { + startIndex = indexOf(array, valueToFind, startIndex); + + if (startIndex == INDEX_NOT_FOUND) { + break; + } + + bitSet.set(startIndex); + ++startIndex; + } + + return bitSet; + } + /** *

Finds the last index of the given value within the array. * @@ -7720,6 +8246,10 @@ public class ArrayUtils { */ // package protected for access by unit tests static Object removeAll(final Object array, final BitSet indices) { + if (array == null) { + return null; + } + final int srcLength = getLength(array); // No need to check maxIndex here, because method only currently called from removeElements() // which guarantee to generate on;y valid bit entries. @@ -8010,20 +8540,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (boolean[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8042,20 +8559,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (char[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8074,20 +8578,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (byte[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8106,20 +8597,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (short[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8138,20 +8616,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (int[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8170,20 +8635,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (long[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8202,20 +8654,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (float[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8234,20 +8673,7 @@ public class ArrayUtils { * @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); - } - - final 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)); + return (double[]) removeAll((Object) array, indexesOf(array, element)); } /** @@ -8267,20 +8693,7 @@ public class ArrayUtils { * @since 3.5 */ public static T[] removeAllOccurences(final T[] array, final T element) { - int index = indexOf(array, element); - if (index == INDEX_NOT_FOUND) { - return clone(array); - } - - final 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)); + return (T[]) removeAll((Object) array, indexesOf(array, element)); } /** diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 069466ea1..3eb76c583 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -31,6 +31,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collections; +import java.util.BitSet; import java.util.Comparator; import java.util.Date; import java.util.Map; @@ -3142,6 +3143,61 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, "0", 8)); } + @Test + public void testIndexesOf() { + final Object[] array = new Object[]{"0", "1", "2", "3", null, "0"}; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf((Object[]) null, null)); + assertEquals(emptySet, ArrayUtils.indexesOf(new Object[0], "0")); + testSet.set(5); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, "0")); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, "2")); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, "3")); + testSet.clear(); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, null)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, "notInArray")); + } + + @Test + public void testIndexesOfWithStartIndex() { + final Object[] array = new Object[]{"0", "1", "2", "3", "2", "3", "1", null, "0"}; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(null, null, 2)); + assertEquals(emptySet, ArrayUtils.indexesOf(new Object[0], "0", 0)); + assertEquals(emptySet, ArrayUtils.indexesOf(null, "0", 2)); + testSet.set(8); + assertEquals(testSet, ArrayUtils.indexesOf(array, "0", 8)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, "0", 0)); + testSet.clear(); + testSet.set(6); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, "1", 0)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, "1", 9)); + testSet.clear(); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, "2", 3)); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, "2", 0)); + testSet.clear(); + testSet.set(3); + testSet.set(5); + assertEquals(testSet, ArrayUtils.indexesOf(array, "3", 0)); + testSet.clear(); + testSet.set(7); + assertEquals(testSet, ArrayUtils.indexesOf(array, null, 0)); + + } + + @Test public void testLastIndexOf() { final Object[] array = new Object[]{"0", "1", "2", "3", null, "0"}; @@ -3229,6 +3285,52 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, 0, 6)); } + @Test + public void testIndexesOfLong() { + final long[] array = new long[]{0, 1, 2, 3}; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf((long[]) null, 0)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 4)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3)); + } + + @Test + public void testIndexesOfLongWithStartIndex() { + final long[] array = new long[]{0, 1, 2, 3, 2, 1, 0, 1}; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf((long[]) null, 0, 0)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 4, 0)); + testSet.set(6); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 1)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 0)); + testSet.clear(); + testSet.set(1); + testSet.set(5); + testSet.set(7); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1, 0)); + testSet.clear(); + testSet.set(2); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2, 0)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3, 0)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 3, 8)); + } + @Test public void testLastIndexOfLong() { long[] array = null; @@ -3294,6 +3396,52 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, 0, 6)); } + @Test + public void textIndexesOfInt() { + int[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 0)); + array = new int[]{0, 1, 2, 3, 0}; + testSet.set(0); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 99)); + } + + @Test + public void testIndexesOfIntWithStartIndex() { + int[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 0, 2)); + array = new int[]{0, 1, 2, 3, 0}; + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 2)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2, 0)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3, 0)); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3, -1)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 99, 0)); + } + @Test public void testLastIndexOfInt() { int[] array = null; @@ -3359,6 +3507,52 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, (short) 0, 6)); } + @Test + public void testIndexesOfShort() { + short[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (short) 0)); + array = new short[]{0, 1, 2, 3, 0}; + testSet.set(0); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 2)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 3)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (short) 99)); + } + + @Test + public void testIndexesOfShortWithStartIndex() { + short[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (short) 0, 2)); + array = new short[]{0, 1, 2, 3, 0}; + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 0, 2)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 0, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 1, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 2, 0)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 3, 0)); + assertEquals(testSet, ArrayUtils.indexesOf(array, (short) 3, -1)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (short) 99, 0)); + } + @Test public void testLastIndexOfShort() { short[] array = null; @@ -3424,6 +3618,53 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, 'a', 6)); } + @Test + public void testIndexesOfChar() { + char[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 'a')); + array = new char[]{'a', 'b', 'c', 'd', 'a'}; + testSet.set(0); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'a')); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'b')); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'c')); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'd')); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 'e')); + } + + @Test + public void testIndexesOfCharWithStartIndex() { + char[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 'a', 0)); + array = new char[]{'a', 'b', 'c', 'd', 'a'}; + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'a', 2)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'a', 0)); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'a', -1)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'b', 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'c', 0)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 'd', 0)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 'd', 5)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 'e', 0)); + } + @Test public void testLastIndexOfChar() { char[] array = null; @@ -3489,6 +3730,52 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, (byte) 0, 6)); } + @Test + public void testIndexesOfByte() { + byte[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (byte) 0)); + array = new byte[]{0, 1, 2, 3, 0}; + testSet.set(0); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 2)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 3)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (byte) 99)); + } + + @Test + public void testIndexesOfByteWithStartIndex() { + byte[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (byte) 0, 2)); + array = new byte[]{0, 1, 2, 3, 0}; + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 0, 2)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 0, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 1, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 2, 0)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 3, 0)); + assertEquals(testSet, ArrayUtils.indexesOf(array, (byte) 3, -1)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (byte) 99, 0)); + } + @Test public void testLastIndexOfByte() { byte[] array = null; @@ -3592,6 +3879,103 @@ public class ArrayUtilsTest { assertEquals(1, ArrayUtils.indexOf(array, 1.00001324, -300, 0.0001)); } + @SuppressWarnings("cast") + @Test + public void testIndexesOfDouble() { + double[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 0)); + array = new double[]{0, 1, 2, 3, 0}; + testSet.set(0); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 99)); + } + + @SuppressWarnings("cast") + @Test + public void testIndexesOfDoubleWithStartIndex() { + double[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 0, 2)); + array = new double[]{0, 1, 2, 3, 0}; + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 2)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2, 0)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3, 0)); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3, -1)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 99, 0)); + } + + @SuppressWarnings("cast") + @Test + public void testIndexesOfDoubleTolerance() { + double[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (double) 0, (double) 0)); + array = new double[0]; + assertEquals(emptySet, ArrayUtils.indexesOf(array, (double) 0, (double) 0)); + array = new double[]{0, 1, 2, 3, 0}; + testSet.set(0); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, (double) 0, 0.3)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 4.15, 2.0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1.00001324, 0.0001)); + } + + @SuppressWarnings("cast") + @Test + public void testIndexesOfDoubleWithStartIndexTolerance() { + double[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, (double) 0, 0, (double) 0)); + array = new double[0]; + assertEquals(emptySet, ArrayUtils.indexesOf(array, (double) 0, 0, (double) 0)); + array = new double[]{0, 1, 2, 3, 0}; + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, (double) 0, 1, 0.3)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, (double) 0, 0, 0.3)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2, 0, 0.35)); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2, 2, 0.35)); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2, -1, 0.35)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 2, 3, 0.35)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 4.15, 0, 2.0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1.00001324, 0, 0.0001)); + } + @SuppressWarnings("cast") @Test public void testLastIndexOfDouble() { @@ -3714,6 +4098,54 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, (float) 0, 6)); } + @SuppressWarnings("cast") + @Test + public void testIndexesOfFloat() { + float[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 0)); + array = new float[]{0, 1, 2, 3, 0}; + testSet.set(0); + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 99)); + } + + @SuppressWarnings("cast") + @Test + public void testIndexesOfFloatWithStartIndex() { + float[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 0, 2)); + array = new float[]{0, 1, 2, 3, 0}; + testSet.set(4); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 2)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, 0, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, 1, 1)); + testSet.clear(); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, 2, 0)); + testSet.clear(); + testSet.set(3); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3, 0)); + assertEquals(testSet, ArrayUtils.indexesOf(array, 3, -1)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, 99, 0)); + } + @SuppressWarnings("cast") @Test public void testLastIndexOfFloat() { @@ -3789,6 +4221,46 @@ public class ArrayUtilsTest { assertEquals(-1, ArrayUtils.indexOf(array, false, -1)); } + @Test + public void testIndexesOfBoolean() { + boolean[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, true)); + array = new boolean[0]; + assertEquals(emptySet, ArrayUtils.indexesOf(array, true)); + array = new boolean[]{true, false, true}; + testSet.set(0); + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, true)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, false)); + array = new boolean[]{true, true}; + assertEquals(emptySet, ArrayUtils.indexesOf(array, false)); + } + + @Test + public void testIndexesOfBooleanWithStartIndex() { + boolean[] array = null; + BitSet emptySet = new BitSet(); + BitSet testSet = new BitSet(); + assertEquals(emptySet, ArrayUtils.indexesOf(array, true, 0)); + array = new boolean[0]; + assertEquals(emptySet, ArrayUtils.indexesOf(array, true, 0)); + array = new boolean[]{true, false, true}; + testSet.set(2); + assertEquals(testSet, ArrayUtils.indexesOf(array, true, 1)); + testSet.set(0); + assertEquals(testSet, ArrayUtils.indexesOf(array, true, 0)); + testSet.clear(); + testSet.set(1); + assertEquals(testSet, ArrayUtils.indexesOf(array, false, 1)); + array = new boolean[]{true, true}; + assertEquals(emptySet, ArrayUtils.indexesOf(array, false, 0)); + assertEquals(emptySet, ArrayUtils.indexesOf(array, false, -1)); + } + @Test public void testLastIndexOfBoolean() { boolean[] array = null;