package vermilion;

import java.util.ArrayList;

/**
 * A generic list wrapper for UsefulFileObjects, which is itself a UsefulFileObject.
 * This class allows you to place UsefulFileObjects into a list, and when
 * serialised it serialises all the objects in the list as well as itself. This
 * means the list can be pulled out of the document and operated on, then placed
 * back into the document.
 */
public class UsefulFileObjectList extends UsefulFileObject
{
    /**
     * The list of objects stored as part of this object.
     */
    public ArrayList<UsefulFileObject> arrayValue;

    /**
     * The serialisation type for this object is 'LIST'.
     */
    @Override
    public String getType() { return "LIST"; }

    /**
     * The current serialisation version for this object is 1.
     */
    @Override
    public int getVersion() { return 1; }

    /**
     * Creates a new empty list object.
     */
    public UsefulFileObjectList()
    {
        arrayValue = new ArrayList<UsefulFileObject>();
    }

    /**
     * Loads the object from serialised form.
     */
    @Override
    public UsefulFileObject loadImpl(String str, int version)
    {
        if (version == 1)
        {
            UsefulFileObjectList listVal = new UsefulFileObjectList();

            ArrayList<String> subs = stringToList(str);

            for (String sub : subs)
            {
                listVal.arrayValue.add(UsefulFileObject.load(sub));
            }

            return listVal;
        }

        return null;
    }

    /**
     * Turns the object into serialised form.
     */
    @Override
    public String saveImpl()
    {
        ArrayList<String> list = new ArrayList<String>();

        for (int x = 0; x < arrayValue.size(); x++)
        {
            list.add(arrayValue.get(x).save());
        }

        return listToString(list);
    }

    /**
     * Provides mapped access to the array.
     */
    public UsefulFileObject get(int x)
    {
        return arrayValue.get(x);
    }

    /**
     * Provides mapped access to the array.
     */
    public int size()
    {
        return arrayValue.size();
    }

    /**
     * Provides mapped access to the array.
     */
    public void remove(int x)
    {
        arrayValue.remove(x);
    }

    /**
     * Provides mapped access to the array.
     */
    public void add(UsefulFileObject o)
    {
        arrayValue.add(o);
    }

    /**
     * Provides mapped access to the array.
     */
    void remove(UsefulFileObject o)
    {
        arrayValue.remove(o);
    }
}
