/* This file was generated by SableCC (http://www.sablecc.org/). */

package org.sablecc.sablecc.node;

import java.util.*;
import org.sablecc.sablecc.analysis.*;

public final class AAstAlt extends PAstAlt
{
  private TId _altName_;
  private final LinkedList _elems_ = new TypedLinkedList(new Elems_Cast());

  public AAstAlt()
  {}

  public AAstAlt(
    TId _altName_,
    List _elems_)
  {
    setAltName(_altName_);

    {
      this._elems_.clear();
      this._elems_.addAll(_elems_);
    }

  }
  public Object clone()
  {
    return new AAstAlt(
             (TId) cloneNode(_altName_),
             cloneList(_elems_));
  }

  public void apply(Switch sw)
  {
    ((Analysis) sw).caseAAstAlt(this);
  }

  public TId getAltName()
  {
    return _altName_;
  }

  public void setAltName(TId node)
  {
    if(_altName_ != null)
    {
      _altName_.parent(null);
    }

    if(node != null)
    {
      if(node.parent() != null)
      {
        node.parent().removeChild(node);
      }

      node.parent(this);
    }

    _altName_ = node;
  }

  public LinkedList getElems()
  {
    return _elems_;
  }

  public void setElems(List list)
  {
    _elems_.clear();
    _elems_.addAll(list);
  }

  public String toString()
  {
    return ""
           + toString(_altName_)
           + toString(_elems_);
  }

  void removeChild(Node child)
  {
    if(_altName_ == child)
    {
      _altName_ = null;
      return;
    }

    if(_elems_.remove(child))
    {
      return;
    }

  }

  void replaceChild(Node oldChild, Node newChild)
  {
    if(_altName_ == oldChild)
    {
      setAltName((TId) newChild);
      return;
    }

    for(ListIterator i = _elems_.listIterator(); i.hasNext();)
    {
      if(i.next() == oldChild)
      {
        if(newChild != null)
        {
          i.set(newChild);
          oldChild.parent(null);
          return;
        }

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

  }

  private class Elems_Cast implements Cast
  {
    public Object cast(Object o)
    {
      PElem node = (PElem) o;

      if((node.parent() != null) &&
          (node.parent() != AAstAlt.this))
      {
        node.parent().removeChild(node);
      }

      if((node.parent() == null) ||
          (node.parent() != AAstAlt.this))
      {
        node.parent(AAstAlt.this);
      }

      return node;
    }
  }
}
