Cell.java 1.58 KB
package kernel;

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

public class Cell {

    private String column;
    private Integer line;
    private Double value;
    private Formula formula;
    private List<Cell> usedIn = new ArrayList<>();

    public Cell(String column, Integer line, Double value) {
        this.column = column;
        this.line = line;
        this.value = value;
    }

    public Cell(String column, Integer line, Formula formula) {
        this.column = column;
        this.line = line;
        this.setFormula(formula);
    }

    public Double getValue() {
        return this.value;
    }

    public Formula getFormula() {
        return this.formula;
    }

    public String getDevelopedFormula() {
        return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId();
    }

    public String getId() {
        return this.column + this.line.toString();
    }

    public List<Cell> getUsedIn() {
        return this.usedIn;
    }

    public String toString() {
        return this.containFormula() ? this.formula.toString() : this.getId();
    }

    public void updateValue() {
        this.value = this.formula.eval();
    }

    public Boolean containFormula() {
        return this.formula != null;
    }

    public void setFormula(Formula formula) {
    	
        this.formula = formula;
        this.updateValue();
        this.spread();
    }

    public void setValue(Double value) {
        this.value = value;
        this.spread();
    }

    private void spread() {
        for (Cell cell : this.usedIn)
            cell.updateValue();
    }
}