Skip to content

Commit e11de7c

Browse files
committed
LANG-1707 add concat methods to ArrayUtils
1 parent ac91ddd commit e11de7c

2 files changed

Lines changed: 485 additions & 0 deletions

File tree

src/main/java/org/apache/commons/lang3/ArrayUtils.java

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9332,6 +9332,302 @@ public static String[] toStringArray(final Object[] array, final String valueFor
93329332
return map(array, String.class, e -> Objects.toString(e, valueForNullElements));
93339333
}
93349334

9335+
/**
9336+
* Concatenates multiple boolean arrays into a single array.
9337+
* <p>
9338+
* This method combines all input arrays in the order they are provided,
9339+
* creating a new array that contains all elements from the input arrays.
9340+
* The resulting array length is the sum of lengths of all non-null input arrays.
9341+
* </p>
9342+
*
9343+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9344+
* or be null itself (treated as empty varargs).
9345+
* @return a new boolean array containing all elements from the input arrays
9346+
* in the order they appear, or an empty array if no elements are present.
9347+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9348+
* @since 3.21.0
9349+
*/
9350+
public static boolean[] concat(boolean[]... arrays) {
9351+
int totalLength = 0;
9352+
for (boolean[] array : arrays) {
9353+
if (array != null) {
9354+
try {
9355+
totalLength = Math.addExact(totalLength, array.length);
9356+
} catch (ArithmeticException exception) {
9357+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9358+
}
9359+
}
9360+
}
9361+
boolean[] result = new boolean[totalLength];
9362+
int currentPos = 0;
9363+
for (boolean[] array : arrays) {
9364+
if (array != null && array.length > 0) {
9365+
System.arraycopy(array, 0, result, currentPos, array.length);
9366+
currentPos += array.length;
9367+
}
9368+
}
9369+
return result;
9370+
}
9371+
9372+
/**
9373+
* Concatenates multiple char arrays into a single array.
9374+
* <p>
9375+
* This method combines all input arrays in the order they are provided,
9376+
* creating a new array that contains all elements from the input arrays.
9377+
* The resulting array length is the sum of lengths of all non-null input arrays.
9378+
* </p>
9379+
*
9380+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9381+
* or be null itself (treated as empty varargs).
9382+
* @return a new char array containing all elements from the input arrays
9383+
* in the order they appear, or an empty array if no elements are present.
9384+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9385+
* @since 3.21.0
9386+
*/
9387+
public static char[] concat(char[]... arrays) {
9388+
int totalLength = 0;
9389+
for (char[] array : arrays) {
9390+
if (array != null) {
9391+
try {
9392+
totalLength = Math.addExact(totalLength, array.length);
9393+
} catch (ArithmeticException exception) {
9394+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9395+
}
9396+
}
9397+
}
9398+
char[] result = new char[totalLength];
9399+
int currentPos = 0;
9400+
for (char[] array : arrays) {
9401+
if (array != null && array.length > 0) {
9402+
System.arraycopy(array, 0, result, currentPos, array.length);
9403+
currentPos += array.length;
9404+
}
9405+
}
9406+
return result;
9407+
}
9408+
9409+
/**
9410+
* Concatenates multiple byte arrays into a single array.
9411+
* <p>
9412+
* This method combines all input arrays in the order they are provided,
9413+
* creating a new array that contains all elements from the input arrays.
9414+
* The resulting array length is the sum of lengths of all non-null input arrays.
9415+
* </p>
9416+
*
9417+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9418+
* or be null itself (treated as empty varargs).
9419+
* @return a new byte array containing all elements from the input arrays
9420+
* in the order they appear, or an empty array if no elements are present.
9421+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9422+
* @since 3.21.0
9423+
*/
9424+
public static byte[] concat(byte[]... arrays) {
9425+
int totalLength = 0;
9426+
for (byte[] array : arrays) {
9427+
if (array != null) {
9428+
try {
9429+
totalLength = Math.addExact(totalLength, array.length);
9430+
} catch (ArithmeticException exception) {
9431+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9432+
}
9433+
}
9434+
}
9435+
byte[] result = new byte[totalLength];
9436+
int currentPos = 0;
9437+
for (byte[] array : arrays) {
9438+
if (array != null && array.length > 0) {
9439+
System.arraycopy(array, 0, result, currentPos, array.length);
9440+
currentPos += array.length;
9441+
}
9442+
}
9443+
return result;
9444+
}
9445+
9446+
/**
9447+
* Concatenates multiple short arrays into a single array.
9448+
* <p>
9449+
* This method combines all input arrays in the order they are provided,
9450+
* creating a new array that contains all elements from the input arrays.
9451+
* The resulting array length is the sum of lengths of all non-null input arrays.
9452+
* </p>
9453+
*
9454+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9455+
* or be null itself (treated as empty varargs).
9456+
* @return a new short array containing all elements from the input arrays
9457+
* in the order they appear, or an empty array if no elements are present.
9458+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9459+
* @since 3.21.0
9460+
*/
9461+
public static short[] concat(short[]... arrays) {
9462+
int totalLength = 0;
9463+
for (short[] array : arrays) {
9464+
if (array != null) {
9465+
try {
9466+
totalLength = Math.addExact(totalLength, array.length);
9467+
} catch (ArithmeticException exception) {
9468+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9469+
}
9470+
}
9471+
}
9472+
short[] result = new short[totalLength];
9473+
int currentPos = 0;
9474+
for (short[] array : arrays) {
9475+
if (array != null && array.length > 0) {
9476+
System.arraycopy(array, 0, result, currentPos, array.length);
9477+
currentPos += array.length;
9478+
}
9479+
}
9480+
return result;
9481+
}
9482+
9483+
/**
9484+
* Concatenates multiple int arrays into a single array.
9485+
* <p>
9486+
* This method combines all input arrays in the order they are provided,
9487+
* creating a new array that contains all elements from the input arrays.
9488+
* The resulting array length is the sum of lengths of all non-null input arrays.
9489+
* </p>
9490+
*
9491+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9492+
* or be null itself (treated as empty varargs).
9493+
* @return a new int array containing all elements from the input arrays
9494+
* in the order they appear, or an empty array if no elements are present.
9495+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9496+
* @since 3.21.0
9497+
*/
9498+
public static int[] concat(int[]... arrays) {
9499+
int totalLength = 0;
9500+
for (int[] array : arrays) {
9501+
if (array != null) {
9502+
try {
9503+
totalLength = Math.addExact(totalLength, array.length);
9504+
} catch (ArithmeticException exception) {
9505+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9506+
}
9507+
}
9508+
}
9509+
int[] result = new int[totalLength];
9510+
int currentPos = 0;
9511+
for (int[] array : arrays) {
9512+
if (array != null && array.length > 0) {
9513+
System.arraycopy(array, 0, result, currentPos, array.length);
9514+
currentPos += array.length;
9515+
}
9516+
}
9517+
return result;
9518+
}
9519+
9520+
/**
9521+
* Concatenates multiple long arrays into a single array.
9522+
* <p>
9523+
* This method combines all input arrays in the order they are provided,
9524+
* creating a new array that contains all elements from the input arrays.
9525+
* The resulting array length is the sum of lengths of all non-null input arrays.
9526+
* </p>
9527+
*
9528+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9529+
* or be null itself (treated as empty varargs).
9530+
* @return a new long array containing all elements from the input arrays
9531+
* in the order they appear, or an empty array if no elements are present.
9532+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9533+
* @since 3.21.0
9534+
*/
9535+
public static long[] concat(long[]... arrays) {
9536+
int totalLength = 0;
9537+
for (long[] array : arrays) {
9538+
if (array != null) {
9539+
try {
9540+
totalLength = Math.addExact(totalLength, array.length);
9541+
} catch (ArithmeticException exception) {
9542+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9543+
}
9544+
}
9545+
}
9546+
long[] result = new long[totalLength];
9547+
int currentPos = 0;
9548+
for (long[] array : arrays) {
9549+
if (array != null && array.length > 0) {
9550+
System.arraycopy(array, 0, result, currentPos, array.length);
9551+
currentPos += array.length;
9552+
}
9553+
}
9554+
return result;
9555+
}
9556+
9557+
/**
9558+
* Concatenates multiple float arrays into a single array.
9559+
* <p>
9560+
* This method combines all input arrays in the order they are provided,
9561+
* creating a new array that contains all elements from the input arrays.
9562+
* The resulting array length is the sum of lengths of all non-null input arrays.
9563+
* </p>
9564+
*
9565+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9566+
* or be null itself (treated as empty varargs).
9567+
* @return a new float array containing all elements from the input arrays
9568+
* in the order they appear, or an empty array if no elements are present.
9569+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9570+
* @since 3.21.0
9571+
*/
9572+
public static float[] concat(float[]... arrays) {
9573+
int totalLength = 0;
9574+
for (float[] array : arrays) {
9575+
if (array != null) {
9576+
try {
9577+
totalLength = Math.addExact(totalLength, array.length);
9578+
} catch (ArithmeticException exception) {
9579+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9580+
}
9581+
}
9582+
}
9583+
float[] result = new float[totalLength];
9584+
int currentPos = 0;
9585+
for (float[] array : arrays) {
9586+
if (array != null && array.length > 0) {
9587+
System.arraycopy(array, 0, result, currentPos, array.length);
9588+
currentPos += array.length;
9589+
}
9590+
}
9591+
return result;
9592+
}
9593+
9594+
/**
9595+
* Concatenates multiple double arrays into a single array.
9596+
* <p>
9597+
* This method combines all input arrays in the order they are provided,
9598+
* creating a new array that contains all elements from the input arrays.
9599+
* The resulting array length is the sum of lengths of all non-null input arrays.
9600+
* </p>
9601+
*
9602+
* @param arrays the arrays to concatenate. Can be empty, contain nulls,
9603+
* or be null itself (treated as empty varargs).
9604+
* @return a new double array containing all elements from the input arrays
9605+
* in the order they appear, or an empty array if no elements are present.
9606+
* @throws IllegalArgumentException if total arrays length exceed {@link ArrayUtils#SAFE_MAX_ARRAY_LENGTH}.
9607+
* @since 3.21.0
9608+
*/
9609+
public static double[] concat(double[]... arrays) {
9610+
int totalLength = 0;
9611+
for (double[] array : arrays) {
9612+
if (array != null) {
9613+
try {
9614+
totalLength = Math.addExact(totalLength, array.length);
9615+
} catch (ArithmeticException exception) {
9616+
throw new IllegalArgumentException("Total arrays length exceed " + SAFE_MAX_ARRAY_LENGTH);
9617+
}
9618+
}
9619+
}
9620+
double[] result = new double[totalLength];
9621+
int currentPos = 0;
9622+
for (double[] array : arrays) {
9623+
if (array != null && array.length > 0) {
9624+
System.arraycopy(array, 0, result, currentPos, array.length);
9625+
currentPos += array.length;
9626+
}
9627+
}
9628+
return result;
9629+
}
9630+
93359631
/**
93369632
* ArrayUtils instances should NOT be constructed in standard programming. Instead, the class should be used as {@code ArrayUtils.clone(new int[] {2})}.
93379633
* <p>

0 commit comments

Comments
 (0)