]> Git Server - utils.git/commitdiff
reworked ArrayUtils
authorRobin Cheney <cheneyr@eternal.ddnss.de>
Tue, 25 Nov 2025 13:49:24 +0000 (14:49 +0100)
committerRobin Cheney <cheneyr@eternal.ddnss.de>
Tue, 25 Nov 2025 13:49:24 +0000 (14:49 +0100)
pom.xml
src/main/java/de/ddnss/eternal/utils/ArrayUtils.java
src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java

diff --git a/pom.xml b/pom.xml
index 8bab3be91c33eae817fda70801fcfb6151b2d689..3e18f3edb68d904e585d243a82f862e33611ac7c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
 
     <groupId>de.ddnss.eternal.utils</groupId>
     <artifactId>utils</artifactId>
-    <version>1.4.0</version>
+    <version>1.4.1</version>
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
index e9540a42c0a0cb8062f81769129c549baf5fd599..ce46a25149f2346768447dbbe4d09c73a6e95a9f 100644 (file)
@@ -7,18 +7,73 @@ import java.util.ArrayList;
  * 
  * @author Robin Cheney
  * 
- * @since 1.1
+ * @since 1.4.1
  */
-public abstract class ArrayUtils<E> {
-    public E[] reverse(E[] array) {
+public abstract class ArrayUtils {
+
+    /**
+     * Reverse and return {@code array}
+     * 
+     * @apiNote This reassigns the original array that is passed to the function as
+     *          the parameter. If you don't want this, use
+     *          {@link #reversedCopy(Object[])}
+     * 
+     * @param <E>   The class/datatype of the array
+     * @param array The array to reverse
+     * @return the reversed array with the same datatype as the original
+     */
+    public static <E> E[] reverse(E[] array) {
 
         ArrayList<E> outArray = new ArrayList<E>();
 
-        for (int i = 0; i < array.length; i++) {
+        for (E i : array) {
+            // System.out.println(i);
+            outArray.add(i);
+        }
+
+        return outArray.reversed().toArray(array);
+    }
+
+    /**
+     * Reverse and return a copy of {@code array}
+     * 
+     * @apiNote If you also want to reverse the original array that was passed as an
+     *          argument, use {@link #reverse(Object[])}
+     * 
+     * @param <E>   The class/datatype of the array
+     * @param array The array to reverse
+     * @return the reversed array with the same datatype as the original
+     */
+    public static <E> E[] reversedCopy(E[] array) {
+        E[] arrayCopy = array.clone();
+
+        ArrayList<E> outArray = new ArrayList<E>();
 
-            // prepend each character
-            outArray.add(array[i]);
+        for (E i : array) {
+            // System.out.println(i);
+            outArray.add(i);
         }
-        return outArray.toArray(array);
+
+        return outArray.reversed().toArray(arrayCopy);
+    }
+
+    /**
+     * Reverse and return an {@code array}
+     * 
+     * @param <E>   The class/datatype of the array
+     * @param array The array to reverse
+     * @return the reversed array with the same datatype as the original
+     * 
+     * @since 1.4.1
+     */
+    public static <E> ArrayList<E> toArrayList(E[] array) {
+
+        ArrayList<E> outArray = new ArrayList<E>();
+
+        for (E i : array) {
+            outArray.add(i);
+        }
+
+        return outArray;
     }
 }
index 0eb07bb1488aff4c34290d3f6b8bf637bab02c85..f5b14c3c282b294f9db8a375cf04c705cd37730f 100644 (file)
@@ -4,8 +4,7 @@ import de.ddnss.eternal.utils.io.input.GenericCommandOption;
 
 /**
  * Thrown when an {@link GenericCommandOption} object does not fit the
- * requirements of
- * the function it was passed to
+ * requirements of the function it was passed to
  * 
  * @author Robin Cheney
  *