From 878b9ac656aa21727bcd42e4835d0e7f067983ea Mon Sep 17 00:00:00 2001 From: TimeConqueror Date: Tue, 3 Aug 2021 21:03:45 +0300 Subject: Well, the name 'Arrays' seems to be not so good, since it collides with java.util.Arrays --- .../gregtech/api/util/extensions/ArrayExt.java | 59 ++++++++++++++++++++++ .../java/gregtech/api/util/extensions/Arrays.java | 32 ------------ 2 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 src/main/java/gregtech/api/util/extensions/ArrayExt.java delete mode 100644 src/main/java/gregtech/api/util/extensions/Arrays.java (limited to 'src/main/java/gregtech/api/util/extensions') diff --git a/src/main/java/gregtech/api/util/extensions/ArrayExt.java b/src/main/java/gregtech/api/util/extensions/ArrayExt.java new file mode 100644 index 0000000000..faa01cb368 --- /dev/null +++ b/src/main/java/gregtech/api/util/extensions/ArrayExt.java @@ -0,0 +1,59 @@ +package gregtech.api.util.extensions; + +import java.util.function.IntFunction; + +public class ArrayExt { + public static int[] of(int... objects) { + return objects; + } + + public static float[] of(float... objects) { + return objects; + } + + public static double[] of(double... objects) { + return objects; + } + + public static char[] of(char... objects) { + return objects; + } + + public static byte[] of(byte... objects) { + return objects; + } + + public static long[] of(long... objects) { + return objects; + } + + @SafeVarargs + public static T[] of(T... objects) { + return objects; + } + + public static T[] withoutTrailingNulls(T[] array, IntFunction arrayFactory) { + int firstNull = -1; + for (int i = 0; i < array.length; i++) { + if (array[i] == null) { + if (firstNull == -1) { + firstNull = i; + } + } else { + firstNull = -1; + } + } + + if (firstNull == -1) { + T[] newArray = arrayFactory.apply(array.length); + System.arraycopy(array, 0, newArray, 0, array.length); + return newArray; + } else if (firstNull == 0) { + return arrayFactory.apply(0); + } else { + T[] newArray = arrayFactory.apply(firstNull); + System.arraycopy(array, 0, newArray, 0, firstNull); + return newArray; + } + } +} diff --git a/src/main/java/gregtech/api/util/extensions/Arrays.java b/src/main/java/gregtech/api/util/extensions/Arrays.java deleted file mode 100644 index 41efec5587..0000000000 --- a/src/main/java/gregtech/api/util/extensions/Arrays.java +++ /dev/null @@ -1,32 +0,0 @@ -package gregtech.api.util.extensions; - -public class Arrays { - public static int[] of(int... objects) { - return objects; - } - - public static float[] of(float... objects) { - return objects; - } - - public static double[] of(double... objects) { - return objects; - } - - public static char[] of(char... objects) { - return objects; - } - - public static byte[] of(byte... objects) { - return objects; - } - - public static long[] of(long... objects) { - return objects; - } - - @SafeVarargs - public static T[] of(T... objects) { - return objects; - } -} -- cgit