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.List; import java.util.Map; import java.util.stream.Collectors; abstract public class Function implements Formula, Serializable { private static final long serialVersionUID = 1L; private Map names = new HashMap<>(); private List listCells; public Function(List listCells) { this.listCells = listCells; } abstract public double eval(); public Map getNames() { return this.names; } @Override public String getDevelopedFormula() { return names.get(Grid.language) + "(" + this.listCells.stream() .map(Cell::getDevelopedFormula) .collect(Collectors.joining(",")) + ")"; } @Override public String toString() { return names.get(Grid.language) + "(" + this.listCells.stream() .map(Cell::getId) .collect(Collectors.joining(",")) + ")"; } @Override public boolean createCycle(Cell cell) { return this.listCells.contains(cell) || this.generateIndirectCycle(cell); } @Override public List getUtilisedCells() { return this.listCells; } private boolean generateIndirectCycle(Cell cell) { for (Cell currentCell : this.listCells) if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) return true; return false; } }