Grid.java 2.73 KB
package kernel;

import kernel.exception.CellNotFoundException;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalException;

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

public class Grid implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Map<String, Cell> cells = new HashMap<>();
	public static LanguageEnum language = LanguageEnum.FR;
	private List<Integer> listLine = new ArrayList<>();
	private List<Integer> listColumn = new ArrayList<>();
	
	public void saveDifferentLineColumn(String column, int line) {
		if (!this.listLine.contains(line))
			listLine.add(line);
		if (!this.listColumn.contains((int) column.charAt(0)))
			listColumn.add((int) column.charAt(0) - (int) 'A' + 1);
	}
	
	public String createCell(String column, int line, double value) throws InvalidIntervalException {
		String id = this.getCellId(column, line);
		Cell cell = new Cell(column, line, value);
		
		this.cells.put(id, cell);
		saveDifferentLineColumn(column, line);
		return id;
	}
	
	public String createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException {
		String id = this.getCellId(column, line);
		Cell cell = new Cell(column, line, formula);
		
		this.cells.put(id, cell);
		saveDifferentLineColumn(column, line);
		return id;
	}
	
	public void setValue(String column, int line, double value) throws CellNotFoundException {
		this.getCell(column, line).setValue(value);
	}
	
	public void setFormula(String column, int line, Formula formula) throws CellNotFoundException,
			CreateCycleException {
		this.getCell(column, line).setFormula(formula);
	}
	
	public Cell getCell(String column, int line) throws CellNotFoundException {
		Cell cell = this.cells.get(this.getCellId(column, line));
		
		if (cell != null)
			return cell;
		else
			throw new CellNotFoundException();
	}
	
	public List<Cell> getCells() {
		return new ArrayList<>(this.cells.values());
	}
	
	public List<String> getCellsId() {
		return new ArrayList<>(this.cells.keySet());
	}
	
	public double getValue(String column, int line) throws CellNotFoundException {
		return this.getCell(column, line).getValue();
	}
	
	public String getFormulaAsString(String column, int line) throws CellNotFoundException {
		return this.getCell(column, line).toString();
	}
	
	public String getDevelopedFormula(String column, int line) throws CellNotFoundException {
		return this.getCell(column, line).getDevelopedFormula();
	}
	
	private String getCellId(String column, int line) {
		return column + line;
	}
	
	public List<Integer> getTotalColumn() {
		return listColumn;
	}
	
	public List<Integer> getTotalLine() {
		return listLine;
	}
}