BinaryOperation.java
1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
private Cell leftCell;
private Cell rightCell;
public BinaryOperation(Cell leftCell, Cell rightCell) {
this.leftCell = leftCell;
this.rightCell = rightCell;
}
abstract public double eval();
abstract public String getOperator();
public Cell getLeftCell() {
return this.leftCell;
}
public Cell getRightCell() {
return this.rightCell;
}
@Override
public String getDevelopedFormula() {
return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")";
}
@Override
public String toString() {
return "(" + this.leftCell.getId() + this.getOperator() + this.rightCell.getId() + ")";
}
@Override
public boolean createCycle(Cell cell) {
if (this.createCycleWith(this.leftCell, cell) || this.createCycleWith(this.rightCell, cell))
return true;
return this.rightCell.equals(cell) || this.leftCell.equals(cell);
}
@Override
public List<Cell> getUtilisedCells() {
List<Cell> cells = new ArrayList<>();
cells.add(this.leftCell);
cells.add(this.rightCell);
return cells;
}
private boolean createCycleWith(Cell cell, Cell current) {
return cell.containFormula() && cell.getFormula().createCycle(current);
}
}