Blame view

src/kernel/Cell.java 2.38 KB
2e8fbd04   Remi   global refactor
1
  package kernel;
d8507aee   [mandjemb]   Fichiers
2
  
5731029f   Remi   merge
3
  import kernel.exception.CreateCycleException;
eb07e5e3   [mandjemb]   avec serialisation
4
  import kernel.exception.InvalidIntervalLineColumnEception;
5731029f   Remi   merge
5
  
eb07e5e3   [mandjemb]   avec serialisation
6
  import java.io.Serializable;
8112a2dd   Remi   merge
7
8
  import java.util.ArrayList;
  import java.util.List;
d8507aee   [mandjemb]   Fichiers
9
  
218ff5c4   [mandjemb]   SERIALIZATION 2.0
10
  public class Cell implements Serializable {
1ff9c9a9   [mandjemb]   Recommandation pr...
11
12
13
  
  	private static final long serialVersionUID = 1L;
  
218ff5c4   [mandjemb]   SERIALIZATION 2.0
14
15
16
  	final int MAX_LIGNES = 20;
  
  	private String column;
1ff9c9a9   [mandjemb]   Recommandation pr...
17
18
  	private int line;
  	private double value;
218ff5c4   [mandjemb]   SERIALIZATION 2.0
19
20
  	private Formula formula;
  	private List<Cell> usedIn = new ArrayList<>();
1ff9c9a9   [mandjemb]   Recommandation pr...
21
22
23
24
  	
  	public boolean IntervallCondition(String column, int line){
  		
  		return line >= 1 && line <= MAX_LIGNES && column.compareTo("A") >= 0 && column.compareTo("Z") <= 0;
218ff5c4   [mandjemb]   SERIALIZATION 2.0
25
  
1ff9c9a9   [mandjemb]   Recommandation pr...
26
27
28
  	}
  	
  	public Cell(String column,int line, double value) throws InvalidIntervalLineColumnEception {
218ff5c4   [mandjemb]   SERIALIZATION 2.0
29
  		column = column.toUpperCase();
1ff9c9a9   [mandjemb]   Recommandation pr...
30
  		if (IntervallCondition(column,line)) {
218ff5c4   [mandjemb]   SERIALIZATION 2.0
31
32
33
34
35
36
37
  			this.column = column;
  			this.line = line;
  			this.setValue(value);
  		} else
  			throw new InvalidIntervalLineColumnEception();
  	}
  
1ff9c9a9   [mandjemb]   Recommandation pr...
38
  	public Cell(String column, int line, Formula formula)
218ff5c4   [mandjemb]   SERIALIZATION 2.0
39
40
  			throws CreateCycleException, InvalidIntervalLineColumnEception {
  		column = column.toUpperCase();
1ff9c9a9   [mandjemb]   Recommandation pr...
41
  		if (IntervallCondition(column,line)) {
218ff5c4   [mandjemb]   SERIALIZATION 2.0
42
43
44
45
46
47
48
49
  			this.column = column;
  			this.line = line;
  			this.setFormula(formula);
  		} else
  			throw new InvalidIntervalLineColumnEception();
  
  	}
  
1ff9c9a9   [mandjemb]   Recommandation pr...
50
  	public double getValue() {
218ff5c4   [mandjemb]   SERIALIZATION 2.0
51
52
53
54
55
56
57
58
59
60
61
62
  		return this.value;
  	}
  
  	public Formula getFormula() {
  		return this.formula;
  	}
  
  	public String getDevelopedFormula() {
  		return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId();
  	}
  
  	public String getId() {
1ff9c9a9   [mandjemb]   Recommandation pr...
63
  		return this.column + this.line;
218ff5c4   [mandjemb]   SERIALIZATION 2.0
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  	}
  
  	public List<Cell> getUsedIn() {
  		return this.usedIn;
  	}
  
  	public String toString() {
  		return this.containFormula() ? this.formula.toString() : this.getId();
  	}
  
  	public void updateValue() {
  		if (this.containFormula())
  			this.value = this.formula.eval();
  	}
  
  	public Boolean containFormula() {
  		return this.formula != null;
  	}
  
  	public void setFormula(Formula formula) throws CreateCycleException {
  		if (formula.createCycle(this))
  			throw new CreateCycleException();
  
  		this.formula = formula;
  		for (Cell cell : this.formula.getUtilisedCells())
  			cell.usedIn.add(this);
  		this.updateValue();
  		this.spreadValue();
  	}
  
  	public void setValue(Double value) {
  		this.value = value;
  		this.formula = null;
  		this.spreadValue();
  	}
  
  	private void spreadValue() {
  		for (Cell cell : this.usedIn) {
  			cell.updateValue();
  			cell.spreadValue();
  		}
  	}
d8507aee   [mandjemb]   Fichiers
106
  }