This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new c5b1a8762 [LANG-1707"] Add ArrayUtils.concat() methods for
concatenating multiple arrays #1519
c5b1a8762 is described below
commit c5b1a87621fe341f343e7bc93249b4fd49a09ff4
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Jan 25 09:13:05 2026 -0500
[LANG-1707"] Add ArrayUtils.concat() methods for concatenating multiple
arrays #1519
Sort new members
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/ArrayUtils.java | 720 ++++++++++-----------
2 files changed, 361 insertions(+), 360 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 53efcf0ac..5a607a2a1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -116,6 +116,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1810" type="add" dev="ggregory" due-to="Yelim Koo,
Gary Gregory">Deprecate ArrayUtils.SOFT_MAX_ARRAY_LENGTH in favor of
SAFE_MAX_ARRAY_LENGTH #1559.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory,
Theodora Anastasia Lazaridou">Add long support to BitField #1561.</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add ArrayUtils.getDimensions(Object).</action>
+ <action issue="LANG-1707" type="add" dev="ggregory" due-to="Ivan Malutin,
Gilles Sadowski, Gary Gregory">Add ArrayUtils.concat() methods for
concatenating multiple arrays #1519.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">Bump org.apache.commons:commons-parent from 92 to 96
#1498.</action>
<action type="update" dev="ggregory" due-to="Gary
Gregory">[test] Bump org.apache.commons:commons-text from 1.14.0 to
1.15.0.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index 2526811ba..b6039adcf 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -60,6 +60,15 @@
*/
public class ArrayUtils {
+ /**
+ * Bridge class to {@link Math} methods for testing purposes.
+ */
+ static class MathBridge {
+ static int addExact(final int a, final int b) {
+ return Math.addExact(a, b);
+ }
+ }
+
/**
* An empty immutable {@code boolean} array.
*/
@@ -1151,6 +1160,27 @@ public static <T> T[] addAll(final T[] array1,
@SuppressWarnings("unchecked") fi
return joinedArray;
}
+ /**
+ * Safely adds the length of an array to a running total, checking for
overflow.
+ *
+ * @param totalLength the current accumulated length
+ * @param array the array whose length should be added (can be {@code
null},
+ * in which case its length is considered 0)
+ * @return the new total length after adding the array's length
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ */
+ private static int addExact(final int totalLength, final Object array) {
+ try {
+ final int length = MathBridge.addExact(totalLength,
getLength(array));
+ if (length > SAFE_MAX_ARRAY_LENGTH) {
+ throw new IllegalArgumentException("Total arrays length exceed
" + SAFE_MAX_ARRAY_LENGTH);
+ }
+ return length;
+ } catch (final ArithmeticException exception) {
+ throw new IllegalArgumentException("Total arrays length exceed " +
SAFE_MAX_ARRAY_LENGTH);
+ }
+ }
+
/**
* Copies the given array and adds the given element at the beginning of
the new array.
* <p>
@@ -1573,122 +1603,378 @@ public static <T> T[] clone(final T[] array) {
}
/**
- * Checks if the value is in the given array.
+ * Concatenates multiple boolean arrays into a single array.
* <p>
- * The method returns {@code false} if a {@code null} array is passed in.
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
* </p>
*
- * @param array the array to search.
- * @param valueToFind the value to find.
- * @return {@code true} if the array contains the object.
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new boolean array containing all elements from the input
arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
*/
- public static boolean contains(final boolean[] array, final boolean
valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static boolean[] concat(boolean[]... arrays) {
+ int totalLength = 0;
+ for (boolean[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final boolean[] result = new boolean[totalLength];
+ int currentPos = 0;
+ for (boolean[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
}
/**
- * Checks if the value is in the given array.
- * <p>
- * The method returns {@code false} if a {@code null} array is passed in.
- * </p>
+ * Concatenates multiple byte arrays into a single array.
* <p>
- * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
- * {@link Arrays#sort(byte[])} and {@link Arrays#binarySearch(byte[],
byte)}.
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
* </p>
*
- * @param array the array to search.
- * @param valueToFind the value to find.
- * @return {@code true} if the array contains the object.
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new byte array containing all elements from the input arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
*/
- public static boolean contains(final byte[] array, final byte valueToFind)
{
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static byte[] concat(byte[]... arrays) {
+ int totalLength = 0;
+ for (byte[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final byte[] result = new byte[totalLength];
+ int currentPos = 0;
+ for (byte[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
}
/**
- * Checks if the value is in the given array.
- * <p>
- * The method returns {@code false} if a {@code null} array is passed in.
- * </p>
+ * Concatenates multiple char arrays into a single array.
* <p>
- * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
- * {@link Arrays#sort(char[])} and {@link Arrays#binarySearch(char[],
char)}.
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
* </p>
*
- * @param array the array to search.
- * @param valueToFind the value to find.
- * @return {@code true} if the array contains the object.
- * @since 2.1
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new char array containing all elements from the input arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
*/
- public static boolean contains(final char[] array, final char valueToFind)
{
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static char[] concat(char[]... arrays) {
+ int totalLength = 0;
+ for (char[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final char[] result = new char[totalLength];
+ int currentPos = 0;
+ for (char[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
}
/**
- * Checks if the value is in the given array.
- * <p>
- * The method returns {@code false} if a {@code null} array is passed in.
- * </p>
+ * Concatenates multiple double arrays into a single array.
* <p>
- * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
- * {@link Arrays#sort(double[])} and {@link Arrays#binarySearch(double[],
double)}.
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
* </p>
*
- * @param array the array to search.
- * @param valueToFind the value to find.
- * @return {@code true} if the array contains the object.
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new double array containing all elements from the input arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
*/
- public static boolean contains(final double[] array, final double
valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static double[] concat(double[]... arrays) {
+ int totalLength = 0;
+ for (double[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final double[] result = new double[totalLength];
+ int currentPos = 0;
+ for (double[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
}
/**
- * Checks if a value falling within the given tolerance is in the
- * given array. If the array contains a value within the inclusive range
- * defined by (value - tolerance) to (value + tolerance).
- * <p>
- * The method returns {@code false} if a {@code null} array
- * is passed in.
- * </p>
+ * Concatenates multiple float arrays into a single array.
* <p>
- * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
- * {@link Arrays#sort(double[])} and {@link Arrays#binarySearch(double[],
double)}.
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
* </p>
*
- * @param array the array to search.
- * @param valueToFind the value to find.
- * @param tolerance the array contains the tolerance of the search.
- * @return true if value falling within tolerance is in array.
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new float array containing all elements from the input arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
*/
- public static boolean contains(final double[] array, final double
valueToFind, final double tolerance) {
- return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
+ public static float[] concat(float[]... arrays) {
+ int totalLength = 0;
+ for (float[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final float[] result = new float[totalLength];
+ int currentPos = 0;
+ for (float[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
}
/**
- * Checks if the value is in the given array.
- * <p>
- * The method returns {@code false} if a {@code null} array is passed in.
- * </p>
+ * Concatenates multiple int arrays into a single array.
* <p>
- * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
- * {@link Arrays#sort(float[])} and {@link Arrays#binarySearch(float[],
float)}.
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
* </p>
*
- * @param array the array to search.
- * @param valueToFind the value to find.
- * @return {@code true} if the array contains the object.
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new int array containing all elements from the input arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
*/
- public static boolean contains(final float[] array, final float
valueToFind) {
- return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ public static int[] concat(int[]... arrays) {
+ int totalLength = 0;
+ for (int[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final int[] result = new int[totalLength];
+ int currentPos = 0;
+ for (int[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
}
/**
- * Checks if the value is in the given array.
+ * Concatenates multiple long arrays into a single array.
* <p>
- * The method returns {@code false} if a {@code null} array is passed in.
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
* </p>
- * <p>
- * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
- * {@link Arrays#sort(int[])} and {@link Arrays#binarySearch(int[], int)}.
+ *
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new long array containing all elements from the input arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
+ */
+ public static long[] concat(long[]... arrays) {
+ int totalLength = 0;
+ for (long[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final long[] result = new long[totalLength];
+ int currentPos = 0;
+ for (long[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Concatenates multiple short arrays into a single array.
+ * <p>
+ * This method combines all input arrays in the order they are provided,
+ * creating a new array that contains all elements from the input arrays.
+ * The resulting array length is the sum of lengths of all non-null input
arrays.
+ * </p>
+ *
+ * @param arrays the arrays to concatenate. Can be empty, contain nulls,
+ * or be null itself (treated as empty varargs).
+ * @return a new short array containing all elements from the input arrays
+ * in the order they appear, or an empty array if no elements are
present.
+ * @throws NullPointerException if the input array of arrays is null.
+ * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
+ * @since 3.21.0
+ */
+ public static short[] concat(short[]... arrays) {
+ int totalLength = 0;
+ for (short[] array : arrays) {
+ totalLength = addExact(totalLength, array);
+ }
+ final short[] result = new short[totalLength];
+ int currentPos = 0;
+ for (short[] array : arrays) {
+ if (array != null && array.length > 0) {
+ System.arraycopy(array, 0, result, currentPos, array.length);
+ currentPos += array.length;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ * <p>
+ * The method returns {@code false} if a {@code null} array is passed in.
+ * </p>
+ *
+ * @param array the array to search.
+ * @param valueToFind the value to find.
+ * @return {@code true} if the array contains the object.
+ */
+ public static boolean contains(final boolean[] array, final boolean
valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ * <p>
+ * The method returns {@code false} if a {@code null} array is passed in.
+ * </p>
+ * <p>
+ * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
+ * {@link Arrays#sort(byte[])} and {@link Arrays#binarySearch(byte[],
byte)}.
+ * </p>
+ *
+ * @param array the array to search.
+ * @param valueToFind the value to find.
+ * @return {@code true} if the array contains the object.
+ */
+ public static boolean contains(final byte[] array, final byte valueToFind)
{
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ * <p>
+ * The method returns {@code false} if a {@code null} array is passed in.
+ * </p>
+ * <p>
+ * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
+ * {@link Arrays#sort(char[])} and {@link Arrays#binarySearch(char[],
char)}.
+ * </p>
+ *
+ * @param array the array to search.
+ * @param valueToFind the value to find.
+ * @return {@code true} if the array contains the object.
+ * @since 2.1
+ */
+ public static boolean contains(final char[] array, final char valueToFind)
{
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ * <p>
+ * The method returns {@code false} if a {@code null} array is passed in.
+ * </p>
+ * <p>
+ * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
+ * {@link Arrays#sort(double[])} and {@link Arrays#binarySearch(double[],
double)}.
+ * </p>
+ *
+ * @param array the array to search.
+ * @param valueToFind the value to find.
+ * @return {@code true} if the array contains the object.
+ */
+ public static boolean contains(final double[] array, final double
valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if a value falling within the given tolerance is in the
+ * given array. If the array contains a value within the inclusive range
+ * defined by (value - tolerance) to (value + tolerance).
+ * <p>
+ * The method returns {@code false} if a {@code null} array
+ * is passed in.
+ * </p>
+ * <p>
+ * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
+ * {@link Arrays#sort(double[])} and {@link Arrays#binarySearch(double[],
double)}.
+ * </p>
+ *
+ * @param array the array to search.
+ * @param valueToFind the value to find.
+ * @param tolerance the array contains the tolerance of the search.
+ * @return true if value falling within tolerance is in array.
+ */
+ public static boolean contains(final double[] array, final double
valueToFind, final double tolerance) {
+ return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ * <p>
+ * The method returns {@code false} if a {@code null} array is passed in.
+ * </p>
+ * <p>
+ * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
+ * {@link Arrays#sort(float[])} and {@link Arrays#binarySearch(float[],
float)}.
+ * </p>
+ *
+ * @param array the array to search.
+ * @param valueToFind the value to find.
+ * @return {@code true} if the array contains the object.
+ */
+ public static boolean contains(final float[] array, final float
valueToFind) {
+ return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
+ }
+
+ /**
+ * Checks if the value is in the given array.
+ * <p>
+ * The method returns {@code false} if a {@code null} array is passed in.
+ * </p>
+ * <p>
+ * If the {@code array} elements you are searching implement {@link
Comparator}, consider whether it is worth using
+ * {@link Arrays#sort(int[])} and {@link Arrays#binarySearch(int[], int)}.
* </p>
*
* @param array the array to search.
@@ -3306,7 +3592,7 @@ public static boolean isEmpty(final short[] array) {
return isArrayEmpty(array);
}
- /**
+ /**
* Tests whether two arrays have equal content, using equals(), handling
multidimensional arrays
* correctly.
* <p>
@@ -3424,7 +3710,7 @@ public static <T> boolean isNotEmpty(final T[] array) {
return !isEmpty(array);
}
- /**
+ /**
* Tests whether two arrays are the same length, treating {@code null}
arrays as length {@code 0}.
*
* @param array1 the first array, may be {@code null}.
@@ -9340,283 +9626,6 @@ public static String[] toStringArray(final Object[]
array, final String valueFor
return map(array, String.class, e -> Objects.toString(e,
valueForNullElements));
}
- /**
- * Concatenates multiple boolean arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new boolean array containing all elements from the input
arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static boolean[] concat(boolean[]... arrays) {
- int totalLength = 0;
- for (boolean[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final boolean[] result = new boolean[totalLength];
- int currentPos = 0;
- for (boolean[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Concatenates multiple byte arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new byte array containing all elements from the input arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static byte[] concat(byte[]... arrays) {
- int totalLength = 0;
- for (byte[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final byte[] result = new byte[totalLength];
- int currentPos = 0;
- for (byte[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Concatenates multiple char arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new char array containing all elements from the input arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static char[] concat(char[]... arrays) {
- int totalLength = 0;
- for (char[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final char[] result = new char[totalLength];
- int currentPos = 0;
- for (char[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Concatenates multiple double arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new double array containing all elements from the input arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static double[] concat(double[]... arrays) {
- int totalLength = 0;
- for (double[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final double[] result = new double[totalLength];
- int currentPos = 0;
- for (double[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Concatenates multiple float arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new float array containing all elements from the input arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static float[] concat(float[]... arrays) {
- int totalLength = 0;
- for (float[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final float[] result = new float[totalLength];
- int currentPos = 0;
- for (float[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Concatenates multiple int arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new int array containing all elements from the input arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static int[] concat(int[]... arrays) {
- int totalLength = 0;
- for (int[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final int[] result = new int[totalLength];
- int currentPos = 0;
- for (int[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Concatenates multiple long arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new long array containing all elements from the input arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static long[] concat(long[]... arrays) {
- int totalLength = 0;
- for (long[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final long[] result = new long[totalLength];
- int currentPos = 0;
- for (long[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Concatenates multiple short arrays into a single array.
- * <p>
- * This method combines all input arrays in the order they are provided,
- * creating a new array that contains all elements from the input arrays.
- * The resulting array length is the sum of lengths of all non-null input
arrays.
- * </p>
- *
- * @param arrays the arrays to concatenate. Can be empty, contain nulls,
- * or be null itself (treated as empty varargs).
- * @return a new short array containing all elements from the input arrays
- * in the order they appear, or an empty array if no elements are
present.
- * @throws NullPointerException if the input array of arrays is null.
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- * @since 3.21.0
- */
- public static short[] concat(short[]... arrays) {
- int totalLength = 0;
- for (short[] array : arrays) {
- totalLength = addExact(totalLength, array);
- }
- final short[] result = new short[totalLength];
- int currentPos = 0;
- for (short[] array : arrays) {
- if (array != null && array.length > 0) {
- System.arraycopy(array, 0, result, currentPos, array.length);
- currentPos += array.length;
- }
- }
- return result;
- }
-
- /**
- * Safely adds the length of an array to a running total, checking for
overflow.
- *
- * @param totalLength the current accumulated length
- * @param array the array whose length should be added (can be {@code
null},
- * in which case its length is considered 0)
- * @return the new total length after adding the array's length
- * @throws IllegalArgumentException if total arrays length exceed {@link
ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
- */
- private static int addExact(final int totalLength, final Object array) {
- try {
- final int length = MathBridge.addExact(totalLength,
getLength(array));
- if (length > SAFE_MAX_ARRAY_LENGTH) {
- throw new IllegalArgumentException("Total arrays length exceed
" + SAFE_MAX_ARRAY_LENGTH);
- }
- return length;
- } catch (final ArithmeticException exception) {
- throw new IllegalArgumentException("Total arrays length exceed " +
SAFE_MAX_ARRAY_LENGTH);
- }
- }
-
/**
* ArrayUtils instances should NOT be constructed in standard programming.
Instead, the class should be used as {@code ArrayUtils.clone(new int[] {2})}.
* <p>
@@ -9629,13 +9638,4 @@ private static int addExact(final int totalLength, final
Object array) {
public ArrayUtils() {
// empty
}
-
- /**
- * Bridge class to {@link Math} methods for testing purposes.
- */
- static class MathBridge {
- static int addExact(final int a, final int b) {
- return Math.addExact(a, b);
- }
- }
}