diff --git a/src/main/java/net/minestom/server/utils/collection/ObjectArray.java b/src/main/java/net/minestom/server/utils/collection/ObjectArray.java index 77ddb07ee..d5dcdbeee 100644 --- a/src/main/java/net/minestom/server/utils/collection/ObjectArray.java +++ b/src/main/java/net/minestom/server/utils/collection/ObjectArray.java @@ -38,4 +38,6 @@ public sealed interface ObjectArray } void trim(); + + @UnknownNullability T @NotNull [] arrayCopy(@NotNull Class type); } diff --git a/src/main/java/net/minestom/server/utils/collection/ObjectArrayImpl.java b/src/main/java/net/minestom/server/utils/collection/ObjectArrayImpl.java index aa10e0335..2d984f5bc 100644 --- a/src/main/java/net/minestom/server/utils/collection/ObjectArrayImpl.java +++ b/src/main/java/net/minestom/server/utils/collection/ObjectArrayImpl.java @@ -1,5 +1,6 @@ package net.minestom.server.utils.collection; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnknownNullability; @@ -36,6 +37,12 @@ final class ObjectArrayImpl { public void trim() { this.array = Arrays.copyOf(array, max + 1); } + + @Override + public @UnknownNullability T @NotNull [] arrayCopy(@NotNull Class type) { + //noinspection unchecked,rawtypes + return (T[]) Arrays.copyOf(array, max + 1, (Class) type.arrayType()); + } } static final class Concurrent implements ObjectArray { @@ -68,5 +75,11 @@ final class ObjectArrayImpl { public synchronized void trim() { this.array = Arrays.copyOf(array, max + 1); } + + @Override + public @UnknownNullability T @NotNull [] arrayCopy(@NotNull Class type) { + //noinspection unchecked,rawtypes + return (T[]) Arrays.copyOf(array, max + 1, (Class) type.arrayType()); + } } } diff --git a/src/test/java/net/minestom/server/utils/collection/ObjectArrayTest.java b/src/test/java/net/minestom/server/utils/collection/ObjectArrayTest.java index 55da362a3..ee2f95440 100644 --- a/src/test/java/net/minestom/server/utils/collection/ObjectArrayTest.java +++ b/src/test/java/net/minestom/server/utils/collection/ObjectArrayTest.java @@ -3,14 +3,13 @@ package net.minestom.server.utils.collection; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; public class ObjectArrayTest { @ParameterizedTest @ValueSource(booleans = {false, true}) - public void testArray(boolean concurrent) { + public void objectArray(boolean concurrent) { ObjectArray array = concurrent ? ObjectArray.concurrent() : ObjectArray.singleThread(); array.set(50, "Hey"); @@ -35,4 +34,18 @@ public class ObjectArrayTest { assertNull(array.get(251)); assertNull(array.get(Integer.MAX_VALUE)); } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + public void arrayCopy(boolean concurrent) { + ObjectArray array = concurrent ? ObjectArray.concurrent() : ObjectArray.singleThread(); + + array.set(1, "Hey"); + String[] copyCache = array.arrayCopy(String.class); + assertArrayEquals(new String[]{null, "Hey"}, copyCache); + + array.set(2, "Hey2"); + assertArrayEquals(new String[]{null, "Hey", "Hey2"}, array.arrayCopy(String.class)); + assertArrayEquals(new String[]{null, "Hey"}, copyCache, "The copy cache should not be modified"); + } }