Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 256 additions & 0 deletions src/main/java/org/apache/commons/lang3/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9305,6 +9305,262 @@ 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
*/
public static boolean[] concat(boolean[]... arrays) {
int totalLength = 0;
for (boolean[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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 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
*/
public static char[] concat(char[]... arrays) {
int totalLength = 0;
for (char[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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 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
*/
public static byte[] concat(byte[]... arrays) {
int totalLength = 0;
for (byte[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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 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
*/
public static short[] concat(short[]... arrays) {
int totalLength = 0;
for (short[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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

/**
* 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
*/
public static int[] concat(int[]... arrays) {
int totalLength = 0;
for (int[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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
*/
public static long[] concat(long[]... arrays) {
int totalLength = 0;
for (long[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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 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
*/
public static float[] concat(float[]... arrays) {
int totalLength = 0;
for (float[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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 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
*/
public static double[] concat(double[]... arrays) {
int totalLength = 0;
for (double[] array : arrays) {
if (array != null) {
totalLength += array.length;
}
}

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

/**
* ArrayUtils instances should NOT be constructed in standard programming. Instead, the class should be used as {@code ArrayUtils.clone(new int[] {2})}.
* <p>
Expand Down
Loading