/* Generated by JTB 1.5.1 */
package grammars.fm.syntaxtree;

import java.util.ArrayList;
import java.util.Iterator;
import grammars.fm.visitor.IVis2Visitor;
import grammars.fm.visitor.IVoidVisitor;

@SuppressWarnings("javadoc")
public class NodeListOptional implements INodeList {

  public ArrayList<INode> nodes;

  private static final int allocTb[] = {0, 1, 2, 3, 4, 5, 10, 20, 50};

  private int allocNb = 0;

  private static final long serialVersionUID = 151L;

  public NodeListOptional() {
    nodes = new ArrayList<>(allocTb[allocNb]);
  }

  public NodeListOptional(final int sz) {
    nodes = new ArrayList<>(sz);
  }

  public NodeListOptional(final INode firstNode) {
    nodes = new ArrayList<>(allocTb[allocNb]);
    addNode(firstNode);
  }

  public NodeListOptional(final int sz, final INode firstNode) {
    nodes = new ArrayList<>(sz);
    addNode(firstNode);
  }

  @Override
  public void addNode(final INode n) {
    if (++allocNb < allocTb.length)
      nodes.ensureCapacity(allocTb[allocNb]);
    else
      nodes.ensureCapacity((allocNb - allocTb.length + 2) * allocTb[(allocTb.length - 1)]);
    nodes.add(n);
  }

  @Override
  public INode elementAt(final int i) {
    return nodes.get(i); }

  @Override
  public Iterator<INode> elements() {
    return nodes.iterator(); }

  @Override
  public int size() {
    return nodes.size(); }

  public boolean present() {
    return (nodes.size() != 0); }

  /*
   * Visitors accept methods (no -novis option, visitors specification : Void,void,None;Vis2,int,java.lang.String)
   */

  @Override
  public int accept(final IVis2Visitor vis, final String argu) {
    return vis.visit(this, argu);
  }

  @Override
  public void accept(final IVoidVisitor vis) {
    vis.visit(this);
  }

}
