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