/**
   * Adds a production pattern element to this alternative. The multiplicity values in the element
   * will be overridden with the specified values. The element is appended to the end of the element
   * list.
   *
   * @param elem the production pattern element
   * @param min the minimum number of occurancies
   * @param max the maximum number of occurancies, or -1 for infinite
   */
  public void addElement(ProductionPatternElement elem, int min, int max) {

    if (elem.isToken()) {
      addToken(elem.getId(), min, max);
    } else {
      addProduction(elem.getId(), min, max);
    }
  }
  /**
   * Returns the minimum number of elements needed to satisfy this alternative. The value returned
   * is the sum of all the elements minimum count.
   *
   * @return the minimum number of elements
   */
  public int getMinElementCount() {
    ProductionPatternElement elem;
    int min = 0;

    for (int i = 0; i < elements.size(); i++) {
      elem = (ProductionPatternElement) elements.get(i);
      min += elem.getMinCount();
    }
    return min;
  }
  /**
   * Checks if this alternative is recursive on the right-hand side. This method checks all the
   * possible right side elements and returns true if the pattern itself is among them.
   *
   * @return true if the alternative is right side recursive, or false otherwise
   */
  public boolean isRightRecursive() {
    ProductionPatternElement elem;

    for (int i = elements.size() - 1; i >= 0; i--) {
      elem = (ProductionPatternElement) elements.get(i);
      if (elem.getId() == pattern.getId()) {
        return true;
      } else if (elem.getMinCount() > 0) {
        break;
      }
    }
    return false;
  }
  /**
   * Returns the maximum number of elements needed to satisfy this alternative. The value returned
   * is the sum of all the elements maximum count.
   *
   * @return the maximum number of elements
   */
  public int getMaxElementCount() {
    ProductionPatternElement elem;
    int max = 0;

    for (int i = 0; i < elements.size(); i++) {
      elem = (ProductionPatternElement) elements.get(i);
      if (elem.getMaxCount() >= Integer.MAX_VALUE) {
        return Integer.MAX_VALUE;
      } else {
        max += elem.getMaxCount();
      }
    }
    return max;
  }