Blame view

src/kernel/Cell.java 2.02 KB
2e8fbd04   Remi   global refactor
1
  package kernel;
d8507aee   [mandjemb]   Fichiers
2
  
5731029f   Remi   merge
3
4
  import kernel.exception.CreateCycleException;
  
8112a2dd   Remi   merge
5
6
  import java.util.ArrayList;
  import java.util.List;
d8507aee   [mandjemb]   Fichiers
7
  
2e8fbd04   Remi   global refactor
8
  public class Cell {
d8507aee   [mandjemb]   Fichiers
9
  
8112a2dd   Remi   merge
10
11
12
      private String column;
      private Integer line;
      private Double value;
2e8fbd04   Remi   global refactor
13
14
      private Formula formula;
      private List<Cell> usedIn = new ArrayList<>();
8112a2dd   Remi   merge
15
  
2e8fbd04   Remi   global refactor
16
      public Cell(String column, Integer line, Double value) {
8112a2dd   Remi   merge
17
18
          this.column = column;
          this.line = line;
5731029f   Remi   merge
19
          this.setValue(value);
8112a2dd   Remi   merge
20
21
      }
  
5731029f   Remi   merge
22
      public Cell(String column, Integer line, Formula formula) throws CreateCycleException {
8112a2dd   Remi   merge
23
24
          this.column = column;
          this.line = line;
94ba86ff   Remi   Auto stash before...
25
          this.setFormula(formula);
8112a2dd   Remi   merge
26
27
      }
  
2e8fbd04   Remi   global refactor
28
      public Double getValue() {
8112a2dd   Remi   merge
29
30
          return this.value;
      }
8112a2dd   Remi   merge
31
  
2e8fbd04   Remi   global refactor
32
33
      public Formula getFormula() {
          return this.formula;
8112a2dd   Remi   merge
34
35
      }
  
2e8fbd04   Remi   global refactor
36
      public String getDevelopedFormula() {
94ba86ff   Remi   Auto stash before...
37
          return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId();
8112a2dd   Remi   merge
38
39
40
41
42
43
      }
  
      public String getId() {
          return this.column + this.line.toString();
      }
  
2e8fbd04   Remi   global refactor
44
45
      public List<Cell> getUsedIn() {
          return this.usedIn;
8112a2dd   Remi   merge
46
47
      }
  
2e8fbd04   Remi   global refactor
48
      public String toString() {
94ba86ff   Remi   Auto stash before...
49
          return this.containFormula() ? this.formula.toString() : this.getId();
2e8fbd04   Remi   global refactor
50
      }
8112a2dd   Remi   merge
51
  
2e8fbd04   Remi   global refactor
52
      public void updateValue() {
5731029f   Remi   merge
53
54
          if (this.containFormula())
              this.value = this.formula.eval();
2e8fbd04   Remi   global refactor
55
56
57
58
      }
  
      public Boolean containFormula() {
          return this.formula != null;
8112a2dd   Remi   merge
59
60
      }
  
5731029f   Remi   merge
61
62
63
64
65
66
67
68
69
70
      public void setFormula(Formula formula) throws CreateCycleException {
          if (formula.createCycle(this))
              throw new CreateCycleException();
          else {
              this.formula = formula;
              for (Cell cell : this.formula.getUtilisedCells())
                  cell.usedIn.add(this);
              this.updateValue();
              this.spreadValue();
          }
8112a2dd   Remi   merge
71
72
      }
  
2e8fbd04   Remi   global refactor
73
74
      public void setValue(Double value) {
          this.value = value;
5731029f   Remi   merge
75
76
          this.formula = null;
          this.spreadValue();
2e8fbd04   Remi   global refactor
77
78
      }
  
5731029f   Remi   merge
79
80
      private void spreadValue() {
          for (Cell cell : this.usedIn) {
8112a2dd   Remi   merge
81
              cell.updateValue();
5731029f   Remi   merge
82
83
              cell.spreadValue();
          }
8112a2dd   Remi   merge
84
      }
d8507aee   [mandjemb]   Fichiers
85
  }