Skip to content

Commit 24978ac

Browse files
committed
add new api to array utils
1 parent b3f78b9 commit 24978ac

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

rlib-common/src/main/java/com/ss/rlib/common/util/ArrayUtils.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,56 @@ public final class ArrayUtils {
3232
public static final long[] EMPTY_LONG_ARRAY = new long[0];
3333
public static final char[] EMPTY_CHAR_ARRAY = new char[0];
3434

35+
/**
36+
* Convert an object integer array to primitive int array.
37+
*
38+
* @param array the object integer array.
39+
* @return the primitive int array.
40+
*/
41+
public static @NotNull int[] toIntArray(@NotNull Integer[] array) {
42+
43+
if (array.length < 1) {
44+
return ArrayUtils.EMPTY_INT_ARRAY;
45+
}
46+
47+
var intArray = new int[array.length];
48+
49+
for (int i = 0; i < array.length; i++) {
50+
intArray[i] = array[i];
51+
}
52+
53+
return intArray;
54+
}
55+
56+
/**
57+
* Convert an string to primitive int array by regex.
58+
*
59+
* @param string the string.
60+
* @param regex the regex.
61+
* @return the primitive int array.
62+
* @throws NumberFormatException if some elements in the string are not an integer.
63+
*/
64+
public static @NotNull int[] toIntArray(@NotNull String string, @NotNull String regex) {
65+
66+
if (string.isBlank()) {
67+
return ArrayUtils.EMPTY_INT_ARRAY;
68+
}
69+
70+
var elements = string.split(regex);
71+
72+
if (elements.length < 1) {
73+
return ArrayUtils.EMPTY_INT_ARRAY;
74+
}
75+
76+
var intArray = new int[elements.length];
77+
78+
for (int i = 0; i < elements.length; i++) {
79+
intArray[i] = Integer.parseInt(elements[i].trim());
80+
}
81+
82+
return intArray;
83+
}
84+
3585
/**
3686
* Add the element to the array and extend or create the array if need.
3787
*
@@ -279,7 +329,7 @@ public static boolean contains(@NotNull Object[] array, @NotNull Object object)
279329
}
280330

281331
/**
282-
* Copy and extend if need a native array array.
332+
* Copy and extend if need a native array.
283333
*
284334
* @param <T> the array component's type.
285335
* @param original the original array.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
11
package com.ss.rlib.common.test.util;
22

3+
import com.ss.rlib.common.util.ArrayUtils;
4+
import com.ss.rlib.common.util.array.ArrayFactory;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
38
/**
49
* Test of methods in ArrayUtils class.
510
*
611
* @author JavaSaBr
712
*/
813
public class ArrayUtilsTests {
914

15+
@Test
16+
void convertIntegerArrayToIntArrayTest() {
17+
18+
Integer[] integers = ArrayFactory.toArray(1, 2, 3, 4, 5);
19+
int[] ints = ArrayUtils.toIntArray(integers);
20+
int[] empty = ArrayUtils.toIntArray(new Integer[0]);
21+
22+
Assertions.assertArrayEquals(ints, ArrayFactory.toIntArray(1, 2, 3, 4, 5));
23+
Assertions.assertArrayEquals(empty, ArrayFactory.toIntArray());
24+
}
25+
26+
@Test
27+
void convertStringToIntArrayTest() {
28+
29+
Assertions.assertArrayEquals(
30+
ArrayUtils.toIntArray("1,2,3,4,5", ","),
31+
ArrayFactory.toIntArray(1, 2, 3, 4, 5)
32+
);
33+
Assertions.assertArrayEquals(
34+
ArrayUtils.toIntArray(" 1 ,2 , 3,4, 5", ","),
35+
ArrayFactory.toIntArray(1, 2, 3, 4, 5)
36+
);
37+
Assertions.assertArrayEquals(
38+
ArrayUtils.toIntArray(" 1 ,2 , 3,4, 5", ","),
39+
ArrayFactory.toIntArray(1, 2, 3, 4, 5)
40+
);
41+
42+
Assertions.assertThrows(NumberFormatException.class,
43+
() -> ArrayUtils.toIntArray(" 1 ,,2 , 3,4, 5,", ","));
44+
Assertions.assertThrows(NumberFormatException.class,
45+
() -> ArrayUtils.toIntArray(" 1 ,qwd , 3,4, 5", ","));
46+
Assertions.assertThrows(NumberFormatException.class,
47+
() -> ArrayUtils.toIntArray(" 1 ,2.5 , 3,4, 5", ","));
48+
}
1049
}

0 commit comments

Comments
 (0)