package jxl.biff.formula;

import java.util.Stack;

abstract class BinaryOperator extends Operator implements ParsedThing {
  public int read(byte[] data, int pos) {
    return 0;
  }
  
  public void getOperands(Stack s) {
    ParseItem o1 = s.pop();
    ParseItem o2 = s.pop();
    add(o1);
    add(o2);
  }
  
  public void getString(StringBuffer buf) {
    ParseItem[] operands = getOperands();
    operands[1].getString(buf);
    buf.append(getSymbol());
    operands[0].getString(buf);
  }
  
  public void adjustRelativeCellReferences(int colAdjust, int rowAdjust) {
    ParseItem[] operands = getOperands();
    operands[1].adjustRelativeCellReferences(colAdjust, rowAdjust);
    operands[0].adjustRelativeCellReferences(colAdjust, rowAdjust);
  }
  
  void columnInserted(int sheetIndex, int col, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    operands[1].columnInserted(sheetIndex, col, currentSheet);
    operands[0].columnInserted(sheetIndex, col, currentSheet);
  }
  
  void columnRemoved(int sheetIndex, int col, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    operands[1].columnRemoved(sheetIndex, col, currentSheet);
    operands[0].columnRemoved(sheetIndex, col, currentSheet);
  }
  
  void rowInserted(int sheetIndex, int row, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    operands[1].rowInserted(sheetIndex, row, currentSheet);
    operands[0].rowInserted(sheetIndex, row, currentSheet);
  }
  
  void rowRemoved(int sheetIndex, int row, boolean currentSheet) {
    ParseItem[] operands = getOperands();
    operands[1].rowRemoved(sheetIndex, row, currentSheet);
    operands[0].rowRemoved(sheetIndex, row, currentSheet);
  }
  
  byte[] getBytes() {
    ParseItem[] operands = getOperands();
    byte[] data = new byte[0];
    for (int i = operands.length - 1; i >= 0; i--) {
      byte[] opdata = operands[i].getBytes();
      byte[] arrayOfByte1 = new byte[data.length + opdata.length];
      System.arraycopy(data, 0, arrayOfByte1, 0, data.length);
      System.arraycopy(opdata, 0, arrayOfByte1, data.length, opdata.length);
      data = arrayOfByte1;
    } 
    byte[] newdata = new byte[data.length + 1];
    System.arraycopy(data, 0, newdata, 0, data.length);
    newdata[data.length] = getToken().getCode();
    return newdata;
  }
  
  abstract String getSymbol();
  
  abstract Token getToken();
}
