package ModSQL;
import java.sql.*;
import java.util.ArrayList;

/* $Id: Operator.java,v 1.7 2003/05/29 04:58:40 cvs Exp $
 *
 * Copyright (c) 2003 Chris Studholme <chris.studholme@utoronto.ca>
 *
 * May be copied or modified under the terms of the GNU General Public
 * License.  See COPYING for more information.
 */

/**
 * Abstract implementation of Function to simplify the implementation
 * of operators.
 *
 * @author chris.studholme@utoronto.ca
 */
public abstract class Operator extends AbstractFunction {

  /**
   * Remove all instances of double NOT operators if x is an Operator_Not
   * object.  Otherwise, this method just returns x.
   *
   * @param x function to check for double NOT
   * @return either x or x with all double NOT's removed
   */
  public static Function removeDoubleNot(Function x) {
    while (x instanceof Operator_Not && x.getParameterCount()==1 &&
	   x.getParameter(0) instanceof Operator_Not &&
	   x.getParameter(0).getParameterCount()==1) {
      x = x.getParameter(0).getParameter(0);
    }
    return x;
  }

  /**
   * Remove all instances of double negation operators if x is an 
   * Operator_Negate object.  Otherwise, this method just returns x.
   *
   * @param x function to check for double negation
   * @return either x or x with all double negation removed
   */
  public static Function removeDoubleNegation(Function x) {
    while (x instanceof Operator_Negate && x.getParameterCount()==1 &&
	   x.getParameter(0) instanceof Operator_Negate &&
	   x.getParameter(0).getParameterCount()==1) {
      x = x.getParameter(0).getParameter(0);
    }
    return x;
  }

  /**
   * Add all of x's parameters to this function.
   *
   * @param x function who's parameters are to be added 
   * @throws SQLException if an error occurs
   */
  public void addAllParameters(Function x) throws SQLException {
    for (int i=0; i<x.getParameterCount(); ++i)
      addParameter(x.getParameter(i));
  }

  /**
   * Add all of x's parameters to this function but apply the NOT operator
   * to each before adding them.
   *
   * @param x function who's parameters are to be added 
   * @throws SQLException if an error occurs
   */
  public void addAllParametersNot(Function x) throws SQLException {
    for (int i=0; i<x.getParameterCount(); ++i)
      addParameter(new Operator_Not(x.getParameter(i)));
  }

  /**
   * Add all of x's parameters to this function but apply the negation operator
   * to each before adding them.
   *
   * @param x function who's parameters are to be added 
   * @throws SQLException if an error occurs
   */
  public void addAllParametersNegate(Function x) throws SQLException {
    for (int i=0; i<x.getParameterCount(); ++i)
      addParameter(new Operator_Negate(x.getParameter(i)));
  }

};
