Skip to content

Commit a33357d

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

2 files changed

Lines changed: 477 additions & 0 deletions

File tree

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

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

0 commit comments

Comments
 (0)