/* 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 AStateList extends PStateList
{
  private TId _id_;
  private PTransition _transition_;
  private final LinkedList _stateLists_ = new TypedLinkedList(new StateLists_Cast());

  public AStateList()
  {}

  public AStateList(
    TId _id_,
    PTransition _transition_,
    List _stateLists_)
  {
    setId(_id_);

    setTransition(_transition_);

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

  }
  public Object clone()
  {
    return new AStateList(
             (TId) cloneNode(_id_),
             (PTransition) cloneNode(_transition_),
             cloneList(_stateLists_));
  }

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

  public TId getId()
  {
    return _id_;
  }

  public void setId(TId node)
  {
    if(_id_ != null)
    {
      _id_.parent(null);
    }

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

      node.parent(this);
    }

    _id_ = node;
  }

  public PTransition getTransition()
  {
    return _transition_;
  }

  public void setTransition(PTransition node)
  {
    if(_transition_ != null)
    {
      _transition_.parent(null);
    }

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

      node.parent(this);
    }

    _transition_ = node;
  }

  public LinkedList getStateLists()
  {
    return _stateLists_;
  }

  public void setStateLists(List list)
  {
    _stateLists_.clear();
    _stateLists_.addAll(list);
  }

  public String toString()
  {
    return ""
           + toString(_id_)
           + toString(_transition_)
           + toString(_stateLists_);
  }

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

    if(_transition_ == child)
    {
      _transition_ = null;
      return;
    }

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

  }

  void replaceChild(Node oldChild, Node newChild)
  {
    if(_id_ == oldChild)
    {
      setId((TId) newChild);
      return;
    }

    if(_transition_ == oldChild)
    {
      setTransition((PTransition) newChild);
      return;
    }

    for(ListIterator i = _stateLists_.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 StateLists_Cast implements Cast
  {
    public Object cast(Object o)
    {
      PStateListTail node = (PStateListTail) o;

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

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

      return node;
    }
  }
}
