BinaryOperation.java 1.66 KB
package kernel.operation;

import kernel.Cell;
import kernel.Formula;

import java.util.ArrayList;
import java.util.List;

abstract public class BinaryOperation implements Formula {

    protected Cell leftCell;
    protected Cell rightCell;
    protected String operator;

    public BinaryOperation(Cell leftCell, Cell rightCell) {
        this.leftCell = leftCell;
        this.rightCell = rightCell;
    }

    abstract public Double eval();

    public String getDevelopedFormula() {
        return "(" + this.leftCell.getDevelopedFormula() + this.operator + this.rightCell.getDevelopedFormula()+")";
    }

    public String toString() {
        return this.leftCell.getId() + this.operator + this.rightCell.getId();
    }

    public Boolean createCycle(Cell cell) {
        if (this.leftCell.containFormula() && !this.rightCell.containFormula())
            return this.leftCell.getFormula().createCycle(cell);
        else {
            if (!this.leftCell.containFormula() && this.rightCell.containFormula())
                return this.rightCell.getFormula().createCycle(cell);
            else {
                if (this.leftCell.containFormula() && this.rightCell.containFormula())
                    return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell);
                else {
                    return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId()));
                }
            }

        }
    }

    public List<Cell> getUtilisedCells() {
        List<Cell> cells = new ArrayList<>();
        cells.add(this.leftCell);
        cells.add(this.rightCell);

        return cells;
    }
}