package com.github.qrokodial.sparkle.utilities.collections;

import com.github.qrokodial.sparkle.utilities.casting.SafeCast;

import java.lang.reflect.Array;
import java.util.Optional;

public class ArrayUtils {
    /**
     * Checks to see if an array contains any specified elements.
     *
     * @param haystack
     * @param needles
     * @return true if a match was found, false otherwise
     */
    public static boolean contains(Object[] haystack, Object... needles) {
        for (Object element : haystack) {
            for (Object needle : needles) {
                if (element.equals(needle)) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Gets the classes backing the array.
     *
     * @param objects
     * @return the class array
     */
    public static Class[] getTypes(Object... objects) {
        Class[] types = new Class[objects.length];

        for (int i = 0; i < objects.length; i++) {
            types[i] = objects[i].getClass();
        }

        return types;
    }

    /**
     * Attempts to cast an array to a given type.
     *
     * @param type
     * @param objects
     * @param <T>
     * @return the optional-wrapped version of the casted array, if the cast was successful
     */
    public static <T> Optional<T[]> cast(Class<T> type, Object... objects) {
        T[] casted = (T[])Array.newInstance(type, objects.length);
        for (int i = 0; i < objects.length; i++) {
            Optional<T> cast = SafeCast.toType(objects[i], type);

            if (cast.isPresent()) {
                casted[i] = cast.get();
            } else {
                return Optional.empty();
            }
        }
        return Optional.of(casted);
    }
}

@@ -1762,11 +1762,496 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Reverses an array.%0A     *%0A     * @param objects%0A     * @param %3CT%3E%0A     * @return the reversed array%0A     */%0A    public static %3CT%3E T%5B%5D reverse(T... objects) %7B%0A        if (objects.length == 0) %7B%0A            return objects;%0A        %7D%0A%0A        T%5B%5D reversed = (T%5B%5D)Array.newInstance(objects%5B0%5D.getClass(), objects.length);%0A        for (int i = 0; i %3C objects.length; i++) %7B%0A            reversed%5Bi%5D = objects%5Bobjects.length - 1 - i%5D;%0A        %7D%0A        return reversed;%0A    %7D
 %0A%7D%0A

Add ArrayUtils#reverse