Skip to content

Commit 5f5650d

Browse files
committed
fix Array.of() and update tests for this
1 parent 3a7a75b commit 5f5650d

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,8 @@ public static void copyTo(
436436
* @param to the last element.
437437
* @return the new array.
438438
*/
439-
public static <T> @NotNull T[] copyOfRange(@NotNull final T[] original, final int from, final int to) {
440-
441-
final Class<? extends Object[]> newType = original.getClass();
442-
final int newLength = to - from;
443-
444-
final T[] copy = ClassUtils.unsafeCast(create(newType.getComponentType(), newLength));
445-
446-
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
447-
448-
return copy;
439+
public static <T> @NotNull T[] copyOfRange(@NotNull T[] original, int from, int to) {
440+
return Arrays.copyOfRange(original, from, to);
449441
}
450442

451443
/**

rlib-common/src/main/java/com/ss/rlib/common/util/array/Array.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface Array<E> extends Collection<E>, Serializable, Reusable, Cloneab
5555
* @return the new read only array.
5656
*/
5757
static <T> @NotNull ReadOnlyArray<T> of(@NotNull Array<T> another) {
58-
return ArrayFactory.newReadOnlyArray(ArrayUtils.copyOf(another.array()));
58+
return ArrayFactory.newReadOnlyArray(ArrayUtils.copyOfRange(another.array(), 0, another.size()));
5959
}
6060

6161
/**

rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ArrayTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class ArrayTest extends BaseTest {
2020
void ofTest() {
2121

2222
var array = ArrayFactory.asArray("First", "Second", "Third", " ");
23+
array.add("Temp");
24+
array.remove("Temp");
25+
2326
var copy = Array.of(array);
2427

2528
Assertions.assertEquals(array, copy);

0 commit comments

Comments
 (0)