package context.arch.storage;

import java.util.Vector;

import context.arch.comm.DataObject;

/**
 * This class is a container for a group of related Condition objects.
 * Condition objects can be added and found in the container.
 */
public class Conditions extends Vector {

  /**
   * Tag for conditions
   */
  public static final String CONDITIONS = "conditions";

  /**
   * Empty constructor 
   */
  public Conditions() {
    super();
  }

  /**
   * Constructor that takes a DataObject as a parameter.  The DataObject
   * must contain the <CONDITIONS> tag.  It stores the encoded data.
   *
   * @param data DataObject that contains condition info for retrieval
   */
  public Conditions(DataObject data) {
    super();
    DataObject conds = data.getDataObject(CONDITIONS);
    Vector v = conds.getChildren();
    for (int i=0; i<v.size(); i++) {
      addElement(new Condition((DataObject)v.elementAt(i)));
    }
  }

  /**
   * Converts the Condition objects to a DataObject
   *
   * @return Conditions object converted to a <CONDITIONS> DataObject
   */
  public DataObject toDataObject() {
    Vector conditions = new Vector();
    for (int i=0; i<size(); i++) {
      conditions.addElement(getConditionAt(i).toDataObject());
    }
    return new DataObject(CONDITIONS, conditions);
  }

  /**
   * Adds the given Condition object to the container.
   *
   * @param cond Condition to add
   */
  public void addCondition(Condition cond) {
    addElement(cond);
  }

  /**
   * Adds the given attribute name, comparison and comparison value to the container.
   *
   * @param attribute Name of the attribute to add
   * @param compare Comparison to use
   * @param value Value for comparison
   */
  public void addCondition(String attribute, int compare, Object value) {
    addElement(new Condition(attribute, compare, value));
  }

  /**
   * Returns the Condition object at the given index
   *
   * @param index Index into the container
   * @return Condition at the specified index
   */
  public Condition getConditionAt(int index) {
    return (Condition)elementAt(index);
  }

  /**
   * Determines whether the given Condition object is in the container
   *
   * @param cond Condition to check
   * @return whether Condition is in the container
   */
  public boolean hasCondition(Condition cond) {
    return contains(cond);
  }

  /**
   * Determines whether the given attribute name and value are in the container,
   * using the default datatype.
   *
   * @param attribute Name of the attribute to check
   * @param compare Comparison to check
   * @param value Comparison value of the attribute to check
   * @return whether the given Condition name, comparison, and value are in the container
   */
  public boolean hasCondition(String attribute, int compare, Object value) {
    return contains(new Condition(attribute, compare, value));
  }

  /**
   * Returns the index at which the Condition object occurs
   *
   * @param att Condition to look for
   * @return index of the specified Condition
   */
  public int indexOfCondition(Condition cond) {
    return indexOf(cond);
  }

  /**
   * Returns the index at which the given Condition attribute name, comparison 
   * and value occurs.
   *
   * @param attribute Name of the attribute to look for
   * @param compare Comparison to look for
   * @param value Comparison value to look for
   * @return index of the specified Condition
   */
  public int indexOfCondition(String name, int compare, Object value) {
    return indexOf(new Condition(name,compare,value));
  }

  /**
   * Returns the number of Condition objects in the container
   *
   * return the number of Condition objects in the container
   */
  public int numConditions() {
    return size();
  }

}
