BinaryOperation.java 1.53 KB
package kernel.operation;

import kernel.Cell;
import kernel.Formula;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

abstract public class BinaryOperation implements Formula, Serializable {
	
	private static final long serialVersionUID = 1L;
	protected Cell leftCell;
	protected Cell rightCell;
	
	public BinaryOperation(Cell leftCell, Cell rightCell) {
		this.leftCell = leftCell;
		this.rightCell = rightCell;
	}
	
	abstract public double eval();
	
	abstract public String getOperator();
	
	public String getDevelopedFormula() {
		return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")";
	}
	
	public String toString() {
		return "(" + this.leftCell.getId() + this.getOperator() + this.rightCell.getId() + ")";
	}
	
	public boolean createCycle(Cell cell) {
		if (this.leftCell.containFormula() && !this.rightCell.containFormula())
			return this.leftCell.getFormula().createCycle(cell);
		
		if (!this.leftCell.containFormula() && this.rightCell.containFormula())
			return this.rightCell.getFormula().createCycle(cell);
		
		if (this.leftCell.containFormula() && this.rightCell.containFormula())
			return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell);
		
		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;
	}
}