Skip to content

Commit e3e66e6

Browse files
committed
update some utils methods and add new one
1 parent 77ee578 commit e3e66e6

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ public static boolean isEmpty(@Nullable double[] array) {
18901890
}
18911891

18921892
/**
1893-
* Create a new array with mapped each element from source array.
1893+
* Convert T array to R array if a source array is not null.
18941894
*
18951895
* @param <T> the source component type.
18961896
* @param <M> the mapped element's type.
@@ -1922,6 +1922,28 @@ public static boolean isEmpty(@Nullable double[] array) {
19221922
return resultArray;
19231923
}
19241924

1925+
/**
1926+
* Convert long array to int array.
1927+
*
1928+
* @param source the source array.
1929+
* @return the int array.
1930+
* @since 9.3.0
1931+
*/
1932+
public static int @NotNull [] longsToInts(long @NotNull [] source) {
1933+
1934+
if (source.length == 0) {
1935+
return ArrayUtils.EMPTY_INT_ARRAY;
1936+
}
1937+
1938+
int[] resultArray = new int[source.length];
1939+
1940+
for (int i = 0; i < source.length; i++) {
1941+
resultArray[i] = (int) source[i];
1942+
}
1943+
1944+
return resultArray;
1945+
}
1946+
19251947
private ArrayUtils() {
19261948
throw new RuntimeException();
19271949
}

rlib-common/src/test/java/com/ss/rlib/common/test/util/ArrayUtilsTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,18 @@ void mapNullableTest() {
6767
ArrayFactory.toArray(8, 1, 6), ArrayUtils.mapNullable(strings, Integer::parseInt, Integer.class)
6868
);
6969
}
70+
71+
@Test
72+
void longsToIntsTest() {
73+
74+
Assertions.assertArrayEquals(
75+
ArrayFactory.toIntArray(1, 5, 3),
76+
ArrayUtils.longsToInts(ArrayFactory.toLongArray(1, 5, 3))
77+
);
78+
79+
Assertions.assertArrayEquals(
80+
ArrayUtils.EMPTY_INT_ARRAY,
81+
ArrayUtils.longsToInts(ArrayUtils.EMPTY_LONG_ARRAY)
82+
);
83+
}
7084
}

0 commit comments

Comments
 (0)