Cleanup ArrayUtils

This commit is contained in:
TheMode 2021-07-17 21:38:32 +02:00
parent f4d280a240
commit 2ee1470470

View File

@ -1,30 +1,28 @@
package net.minestom.server.utils;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
import java.util.Objects;
@ApiStatus.Internal
public final class ArrayUtils {
private ArrayUtils() {
}
public static int[] concatenateIntArrays(@NotNull int[]... arrays) {
public static int[] concatenateIntArrays(int @NotNull []... arrays) {
int totalLength = 0;
for (int[] array : arrays) {
totalLength += array.length;
}
int[] result = new int[totalLength];
int startingPos = 0;
for (int[] array : arrays) {
System.arraycopy(array, 0, result, startingPos, array.length);
startingPos += array.length;
}
return result;
}
@ -32,11 +30,6 @@ public final class ArrayUtils {
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
}
public static void copyToDestination(short[] src, short[] dest) {
Check.argCondition(src.length != dest.length, "The two arrays need to have the same length.");
System.arraycopy(src, 0, dest, 0, src.length);
}
/**
* Gets the differences between 2 arrays.
*
@ -44,8 +37,7 @@ public final class ArrayUtils {
* @param b the second array
* @return an array containing a's indexes that aren't in b array
*/
@NotNull
public static int[] getDifferencesBetweenArray(@NotNull long[] a, @NotNull long[] b) {
public static int @NotNull [] getDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b) {
int counter = 0;
int[] indexes = new int[Math.max(a.length, b.length)];
@ -69,50 +61,30 @@ public final class ArrayUtils {
return result;
}
@NotNull
public static int[] toArray(@NotNull IntList list) {
public static int @NotNull [] toArray(@NotNull IntList list) {
int[] array = new int[list.size()];
for (int i = 0; i < array.length; i++) {
array[i] = list.getInt(i);
}
list.getElements(0, array, 0, array.length);
return array;
}
/**
* Gets if two arrays share the same start until {@code length}.
*
* @param array1 the first array
* @param array2 the second array
* @param first the first array
* @param second the second array
* @param length the length to check (0-length)
* @param <T> the type of the arrays
* @return true if both arrays share the same start
*/
public static <T> boolean sameStart(T[] array1, T[] array2, int length) {
if (length > array1.length || length > array2.length) {
public static <T> boolean sameStart(@NotNull T[] first, @NotNull T[] second, int length) {
if (Math.min(first.length, second.length) < length) {
return false;
}
for (int i = 0; i < length; i++) {
final T value1 = array1[i];
final T value2 = array2[i];
if (!value1.equals(value2)) {
if (!Objects.equals(first[i], second[i])) {
return false;
}
}
return true;
}
/**
* Fills an array using a supplier.
*
* @param array the array to fill
* @param supplier the supplier to fill the array
* @param <T> the array type
*/
public static <T> void fill(@NotNull T[] array, @NotNull Supplier<T> supplier) {
for (int i = 0; i < array.length; i++) {
array[i] = supplier.get();
}
}
}