// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.util;

import java.util.List;

/*
    Array Utilities
*/

public final class ArrayUtil
{

    /** keep people from creating this class */
    private ArrayUtil ()
    {
    }
    
    /**
        Convert a List into an Object array with null check.
        
        @param vector a List to pull the contents from

        @return a new Object array containing all elements of
        <b>vector</b>, or a new empty Object array if <b>vector</b> was
        <b>null</b>
    */
    public static Object[] array (List list)
    {
        if (list == null) {
            return new Object[0];
        }
        return list.toArray();
    }

    /**
        Helper function to create a new empty Object array

        @return a new empty Object array
    */
    public static Object[] array ()
    {
        return new Object[] { };
    }

    /**
        Helper function to create a new Object array containing one
        Object.

        @param a the first Object to add to the array

        @return a new Object array containing the specified object
    */
    public static Object[] array (Object a)
    {
        return new Object[] { a };
    }

    /**
        Helper function to create a new Object array containing two
        Objects.

        @param a the first Object to add to the array
        @param b the second Object to add to the array

        @return a new Object array containing the specified objects
    */
    public static Object[] array (Object a, Object b)
    {
        return new Object[] { a, b };
    }

    /**
        Helper function to create a new Object array containing three
        Objects.

        @param a the first Object to add to the array
        @param b the second Object to add to the array
        @param c the third Object to add to the array

        @return a new Object array containing the specified objects
    */
    public static Object[] array (Object a, Object b, Object c)
    {
        return new Object[] { a, b, c };
    }

    /**
        Helper function to create a new Object array containing four
        Objects.

        @param a the first Object to add to the array
        @param b the second Object to add to the array
        @param c the third Object to add to the array
        @param d the fourth Object to add to the array

        @return a new Object array containing the specified objects
    */
    public static Object[] array (Object a, Object b, Object c, Object d)
    {
        return new Object[] { a, b, c, d };
    }

    /**
        Helper function to create a new Object array containing five
        Objects.

        @param a the first Object to add to the array
        @param b the second Object to add to the array
        @param c the third Object to add to the array
        @param d the fourth Object to add to the array
        @param e the fifth Object to add to the array

        @return a new Object array containing the specified objects
    */
    public static Object[] array (Object a, Object b, Object c, Object d,
                                  Object e)
    {
        return new Object[] { a, b, c, d, e };
    }

    /**
        Helper function to create a new Object array containing six
        Objects.

        @param a the first Object to add to the array
        @param b the second Object to add to the array
        @param c the third Object to add to the array
        @param d the fourth Object to add to the array
        @param e the fifth Object to add to the array
        @param f the sixth Object to add to the array

        @return a new Object array containing the specified objects
    */
    public static Object[] array (Object a, Object b, Object c, Object d,
                                  Object e, Object f)
    {
        return new Object[] { a, b, c, d, e, f };
    }

    /**
        Helper function to create a new Object array containing seven
        Objects.

        @param a the first Object to add to the array
        @param b the second Object to add to the array
        @param c the third Object to add to the array
        @param d the fourth Object to add to the array
        @param e the fifth Object to add to the array
        @param f the sixth Object to add to the array
        @param g the seventh Object to add to the array

        @return a new Object array containing the specified objects
    */
    public static Object[] array (Object a, Object b, Object c, Object d,
                                  Object e, Object f, Object g)
    {
        return new Object[] { a, b, c, d, e, f, g };
    }

    /**
        Helper function to create a new Object array containing eight
        Objects.

        @param a the first Object to add to the array
        @param b the second Object to add to the array
        @param c the third Object to add to the array
        @param d the fourth Object to add to the array
        @param e the fifth Object to add to the array
        @param f the sixth Object to add to the array
        @param g the seventh Object to add to the array
        @param h the eighth Object to add to the array

        @return a new Object array containing the specified objects
    */
    public static Object[] array (Object a, Object b, Object c, Object d,
                                  Object e, Object f, Object g, Object h)
    {
        return new Object[] { a, b, c, d, e, f, g, h };
    }

    /**
        Determine if an Object array is null or empty.
        
        @param array the Object array to check

        @return <b>true</b> if <B>array</B> is null or empty,
        <b>false</b> otherwise
    */
    public static boolean nullOrEmptyArray (Object[] array)
    {
        return (array == null || array.length == 0);
    }

    /**
        Determine if an int array is null or empty.

        @param array the int array to check

        @return <b>true</b> if <B>array</B> is null or empty,
        <b>false</b> otherwise
    */
    public static boolean nullOrEmptyIntArray (int[] array)
    {
        return (array == null || array.length == 0);
    }
}
