/* Generated by JTB 1.5.1 */
package examples.java.ex1jtb.syntaxtree;

import java.util.ArrayList;
import java.util.List;
import examples.java.ex1jtb.visitor.IGenVisitor;

@SuppressWarnings("javadoc")
public class NodeOptional implements INode {

  public INode node;

  private INode parent;

  private static final long serialVersionUID = 151L;

  public NodeOptional() {
    node = null;
  }

  public NodeOptional(final INode n) {
    addNode(n);
  }

  public void addNode(final INode n) {
    if (node != null)
      throw new Error("Attempt to set optional node twice"); //$NON-NLS-1$
    node = n;
    if (n != null)
      n.setParent(this);
  }

  public boolean present() {
    return (node != null); }

  /*
   * Visitors accept methods (no -novis option, visitors specification : Gen,void,java.lang.String)
   */

  @Override
  public void accept(final IGenVisitor vis, final String argu) {
    vis.visit(this, argu);
  }

  /*
   * Parent pointer getter and setter (-pp option)
   */

  @Override
  public INode getParent() {
    return parent;
  }

  @Override
  public void setParent(final INode n) {
    parent = n;
  }

  /*
   * Children methods (-chm option)
   */

  private List<INode> lac = null;

  private List<INode> lbc = null;

  private List<INode> luc = null;

  private int nbLbc = -1;

  private int nbLuc = -1;

  @Override
  public int getNbAllChildren() {
    return node == null ? 0 : 1;
  }

  @Override
  public int getNbBaseChildren() {
    if (lbc == null)
      getBaseChildren();
    return nbLbc;
  }

  @Override
  public int getNbUserChildren() {
    if (luc == null)
      getUserChildren();
    return nbLuc;
  }

  @Override
  public List<INode> getAllChildren() {
    if (lac == null) {
      if (node == null) {
        lac = new ArrayList<>(0);
      } else {
        lac = new ArrayList<>(1);
        lac.add(node);
      }
    }
    return lac;
  }

  @Override
  public List<INode> getBaseChildren() {
    if (lbc == null) {
      nbLbc = 0;
      if (node != null && node.isBaseNode())
        nbLbc++;
      lbc = new ArrayList<>(nbLbc);
      if (node != null && node.isBaseNode())
        lbc.add(node);
    }
    return lbc;
  }

  @Override
  public List<INode> getUserChildren() {
    if (luc == null) {
      nbLuc = 0;
      if (node != null && !node.isBaseNode())
        nbLuc++;
      luc = new ArrayList<>(nbLuc);
      if (node != null && !node.isBaseNode())
        luc.add(node);
    }
    return luc;
  }

  @Override
  public boolean isBaseNode() {
    return true;
  }

}
