package com.treelight.krnl;

/**
 * The "atomic unit" of a knowledge repository.
 *
 * @version 0.1
 * @author Eric Armstrong
 * @see ../NodesAndLists.html
 */
public class StructureNode extends AbstractEntity
  static final String nodetype = STRUCTURE_NODE;

  // Defined when the node is constructed
  private String name;
  public String getName() { return name; }

  // The list of lists contained by this node.
  protected List lists = new LinkedList(); 

  /**
   * Add a list to the list of lists that
   * this node contains.
   */
  public void add(List list) 
  throws ListExistsException { 
    Iterator it = lists.iterator();
    while (it.hasNext()) {
      List list = (List) it.next();
      if (listType.equals(list.getType())) {
        throw new ListExistsException();
      }
    }    
    lists.add(list); 
  }

  /**
   * Return the named list from the list of lists that
   * this node contains.
   *
   * @exception ListNotFoundException if the named list does
   *            not exist.
   */
  public List get(String listType) 
  throws ListNotFoundException { 
    Iterator it = lists.iterator();
    while (it.hasNext()) {
      List list = (List) it.next();
      if (listType.equals(list.getType())) {
        return list;
      }
    }
    throw new ListNotFoundException();
  }

  /**
   * See if the named list exists in the list of
   * lists that this node contains.
   *
   * @exception ListNotFoundException if the named list does not exist.
   */
  public boolean hasList(String listType) { 
    Iterator it = lists.iterator();
    while (it.hasNext()) {
      List list = (List) it.next();
      if (listType.equals(list.getType())) {
        return true;
      }
    }
    return false;
  }

  public void remove(String listType)
  throws ListNotFoundException { 
    Iterator lit = lists.listIterator();
    while (lit.hasNext()) {
      List list = (List) lit.next();
      if (listType.equals(list.getType())) {
        lit.remove();
        return; // successful
      }
    }
    throw new ListNotFoundException();
  }

  /**
   * Return the node's content.
   */
  public String getContent() {
    return contentList.getContent();
  }  

  /** 
   * Construct a node, specifying the kind of node it is.
   * (In XML, the specification would be the element name.)
   */
  public StructureNode(String name) {
    this.name = name;
  }

  // IMPLEMENTATION NOTE:
  // When adding behaviors to this class, be sure the
  // ContainedNode class delegates them properly.

}//StructureNode
