package javakurs.uebung11;

import java.util.List;

/**
 * {@link Rule} represents the rule of a context free grammar.
 *
 * @author Bernd Kiefer, DFKI
 * @author Joerg Steffen, DFKI
 * @version $Id$
 */
public class Rule {

  /**
   * Contains the left hand side non-terminal.
   */
  private String lhs;

  /**
   * Contains the right hand side (non-)terminals.
   */
  private List<String> rhs;


  /**
   * Creates a new instance of {@link Rule} for the given lhs and rhs.
   *
   * @param lhs
   *          the left hand side non-terminal
   * @param rhs
   *          the right hand side (non-)terminals
   */
  public Rule(String lhs, List<String> rhs) {

    this.lhs = lhs;
    this.rhs = rhs;
  }


  /**
   * Returns the left hand side of the rule.
   *
   * @return the left hand side non-terminal
   */
  public String getLhs() {

    return this.lhs;
  }


  /**
   * Returns the right hand side of the rule.
   *
   * @return the right hand side (non-)terminals
   */
  public List<String> getRhs() {

    return this.rhs;
  }


  /**
   * Returns the right hand side (non-)terminal at the given position.
   *
   * @param pos
   *          the position
   * @return the right hand side (non-)terminal;
   *         returns {@code null} if pos is out of range
   */
  public String getRhs(int pos) {

    if (pos >= 0 && pos < this.rhs.size()) {
      return this.rhs.get(pos);
    }

    return null;
  }


  /**
   * Returns the numbers of (non-)terminals at the right hand side of the
   * rule.
   *
   * @return the arity of the rhs
   */
  public int arity() {

    return this.rhs.size();
  }
}
