Function.java 1.57 KB
package kernel.function;

import kernel.Cell;
import kernel.Formula;
import kernel.Grid;
import kernel.LanguageEnum;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

abstract public class Function implements Formula,Serializable {

    public Map<LanguageEnum, String> names = new HashMap<>();
    public List<Cell> listCells;

    public Function(List<Cell> listCells) {
        this.listCells = listCells;
    }

    abstract public Double eval();

    public String getDevelopedFormula() {
        return names.get(Grid.language) + "("
                + this.listCells.stream()
                .map(Cell::getDevelopedFormula)
                .collect(Collectors.joining(",")) + ")";
    }

    public String toString() {
        return names.get(Grid.language) + "("
                + this.listCells.stream()
                .map(Cell::getId)
                .collect(Collectors.joining(",")) + ")";
    }

    public Boolean createCycle(Cell cell) {
        boolean cycle = false;
        if (!this.listCells.contains(cell)) {
            Iterator<Cell> it = listCells.iterator();
            while (it.hasNext() && !cycle) {
                Cell currentCell = it.next();
                if (currentCell.containFormula()) {
                    cycle = currentCell.getFormula().createCycle(cell);
                }
            }
            return cycle;
        } else
            return true;
    }

    public List<Cell> getUtilisedCells() {
        return this.listCells;
    }
}