package ast.node;

import java.util.*;
import ast.visitor.*;

@SuppressWarnings("nls")
public final class StatementList extends IStatement
{
    private final LinkedList<IStatement> _statements_ = new LinkedList<IStatement>();

    public StatementList(List<IStatement> _statements_)
    {
        super();
        setStatements(_statements_);
    }

    public StatementList()
    {
        super();
        setStatements(new LinkedList<IStatement>());
    }
    
    public int getNumExpChildren() { return 0; }
    
    @Override
    public Object clone()
    {
        return new StatementList(cloneList(this._statements_));
    }

    public void accept(Visitor v)
    {
        v.visitStatementList(this);
    }

    public LinkedList<IStatement> getStatements()
    {
        return this._statements_;
    }

    public void setStatements(List<IStatement> list)
    {
        this._statements_.clear();
        this._statements_.addAll(list);
        for(IStatement e : list)
        {
            if(e.parent() != null)
            {
                e.parent().removeChild(e);
            }

            e.parent(this);
        }
    }

    @Override
    void removeChild(Node child)
    {
        // Remove child
        if(this._statements_.remove(child))
        {
            return;
        }

        throw new RuntimeException("Not a child.");
    }

    @Override
    void replaceChild(Node oldChild, Node newChild)
    {
        // Replace child
        for(ListIterator<IStatement> i = this._statements_.listIterator(); i.hasNext();)
        {
            if(i.next() == oldChild)
            {
                if(newChild != null)
                {
                    i.set((IStatement) newChild);
                    newChild.parent(this);
                    oldChild.parent(null);
                    return;
                }

                i.remove();
                oldChild.parent(null);
                return;
            }
        }

        throw new RuntimeException("Not a child.");
    }
}
