Blame view

src/kernel/Grid.java 2.09 KB
2e8fbd04   Remi   global refactor
1
  package kernel;
18e7a394   Remi   add package
2
  
2e8fbd04   Remi   global refactor
3
  import kernel.exception.CellNotFoundException;
8112a2dd   Remi   merge
4
5
6
7
  
  import java.util.HashMap;
  import java.util.Map;
  
2e8fbd04   Remi   global refactor
8
  public class Grid {
8112a2dd   Remi   merge
9
  
2e8fbd04   Remi   global refactor
10
      private Map<String, Cell> cells = new HashMap<>();
8112a2dd   Remi   merge
11
  
2e8fbd04   Remi   global refactor
12
      public String createCell(String column, Integer line, Double value) {
8112a2dd   Remi   merge
13
          String id = this.getId(column, line);
2e8fbd04   Remi   global refactor
14
          Cell cell = new Cell(column, line, value);
8112a2dd   Remi   merge
15
  
2e8fbd04   Remi   global refactor
16
          this.cells.put(id, cell);
8112a2dd   Remi   merge
17
18
19
20
  
          return id;
      }
  
2e8fbd04   Remi   global refactor
21
      public String createCell(String column, Integer line, Formula formula) {
8112a2dd   Remi   merge
22
          String id = this.getId(column, line);
2e8fbd04   Remi   global refactor
23
          Cell cell = new Cell(column, line, formula);
8112a2dd   Remi   merge
24
  
2e8fbd04   Remi   global refactor
25
          this.cells.put(id, cell);
8112a2dd   Remi   merge
26
27
28
29
30
  
          return id;
      }
  
      public void setValue(String column, Integer line, Double value) throws CellNotFoundException {
2e8fbd04   Remi   global refactor
31
          Cell cell = this.getCell(column, line);
8112a2dd   Remi   merge
32
33
34
35
36
37
38
39
  
          try {
              cell.setValue(value);
          } catch (NullPointerException exception) {
              throw new CellNotFoundException();
          }
      }
  
2e8fbd04   Remi   global refactor
40
41
      public void setFormula(String column, Integer line, Formula formula) throws CellNotFoundException {
          Cell cell = this.getCell(column, line);
8112a2dd   Remi   merge
42
43
44
45
46
47
48
49
  
          try {
              cell.setFormula(formula);
          } catch (NullPointerException exception) {
              throw new CellNotFoundException();
          }
      }
  
2e8fbd04   Remi   global refactor
50
51
      public Cell getCell(String column, Integer line) {
          return this.cells.get(this.getId(column, line));
8112a2dd   Remi   merge
52
53
      }
  
2e8fbd04   Remi   global refactor
54
55
      public Double getValue(String column, Integer line) throws CellNotFoundException {
          Cell cell = this.getCell(column, line);
8112a2dd   Remi   merge
56
57
58
59
60
61
62
63
  
          try {
              return cell.getValue();
          } catch (NullPointerException exception) {
              throw new CellNotFoundException();
          }
      }
  
2e8fbd04   Remi   global refactor
64
65
      public String getFormulaAsString(String column, Integer line) throws CellNotFoundException {
          Cell cell = this.getCell(column, line);
8112a2dd   Remi   merge
66
67
  
          try {
2e8fbd04   Remi   global refactor
68
              return cell.getFormula().toString();
8112a2dd   Remi   merge
69
70
71
72
73
74
75
76
          } catch (NullPointerException exception) {
              throw new CellNotFoundException();
          }
      }
  
      private String getId(String column, Integer line) {
          return column + line.toString();
      }
18e7a394   Remi   add package
77
  }