<gh_stars>0
/* Copyright 2004 - 2008 <NAME> <<EMAIL>>
 * Licensed under the Apache 2.0 License. */
package org.codehaus.cake.internal.util;

import java.lang.reflect.Array;

public class ArrayUtils {
    public static <T> T[] copyOf(T[] original) {
        return (T[]) copyOf(original, original.length, original.getClass());
    }

    public static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    }

    @SuppressWarnings("cast")
    public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        T[] copy = (Object) newType == (Object) Object[].class ? (T[]) new Object[newLength]
                : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }

    public static <T> T[] reverse(T[] array) {
        for (int i = 0, j = array.length - 1; i < j; i++, j--) {
            T temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
        return array;
    }
}
