/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.biomoby.shared.registry;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Represents an abstract Moby Central data structure (that can be either "input" or "output")
 * 
 * @author Dmitry Repchevsky
 */

@XmlTransient
public abstract class Data implements Serializable, Cloneable
{
    private ArrayList<Simple> simples;
    private ArrayList<Collection> collections;

    public void addSimple(Simple simple)
    {
        getSimples().add(simple);
    }

    public void addCollection(Collection collection)
    {
        getCollections().add(collection);
    }
    
    @XmlElement(name="simple")
    public List<Simple> getSimples()
    {
        if (simples == null)
        {
            simples = new ArrayList<Simple>();
        }

        return simples;
    }

    @XmlElement(name="collection")
    public List<Collection> getCollections()
    {
        if (collections == null)
        {
            collections = new ArrayList<Collection>();
        }

        return collections;
    }

    @Override
    public Data clone()
    {
        Data clone;

        try
        {
            clone = (Data)super.clone();

            if (simples != null)
            {
                clone.simples = (ArrayList<Simple>)simples.clone();

                for (int i = 0, n = clone.simples.size(); i < n; i++)
                {
                    Simple simple = clone.simples.get(i);
                    clone.simples.set(i, simple.clone());
                }
            }

            if (collections != null)
            {
                clone.collections = (ArrayList<Collection>)collections.clone();
                
                for (int i = 0, n = clone.collections.size(); i < n; i++)
                {
                    Collection collection = clone.collections.get(i);
                    clone.collections.set(i, collection.clone());
                }
            }
        }
        catch(CloneNotSupportedException ex)
        {
            clone = null;
        }

        return clone;
    }
}
