Blame view

src/kernel/Cell.java 2.34 KB
2e8fbd04   Remi   global refactor
1
  package kernel;
d8507aee   [mandjemb]   Fichiers
2
  
5731029f   Remi   merge
3
  import kernel.exception.CreateCycleException;
5731029f   Remi   merge
4
  
eb07e5e3   [mandjemb]   avec serialisation
5
  import java.io.Serializable;
8112a2dd   Remi   merge
6
7
  import java.util.ArrayList;
  import java.util.List;
080a0e68   Remi   change return
8
  import java.util.Objects;
d8507aee   [mandjemb]   Fichiers
9
  
218ff5c4   [mandjemb]   SERIALIZATION 2.0
10
  public class Cell implements Serializable {
4186cd92   Remi   fix tests
11
12
  	
  	private static final long serialVersionUID = 1L;
4186cd92   Remi   fix tests
13
14
15
16
17
18
  	private String column;
  	private int line;
  	private double value;
  	private Formula formula;
  	private List<Cell> usedIn = new ArrayList<>();
  	
23982881   [mandjemb]   update ihm
19
  	public Cell(String column, int line, double value) {
4186cd92   Remi   fix tests
20
21
22
23
24
25
  		this.column = column;
  		this.line = line;
  		this.setValue(value);
  	}
  	
  	public Cell(String column, int line, Formula formula)
23982881   [mandjemb]   update ihm
26
  			throws CreateCycleException {
4186cd92   Remi   fix tests
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  		this.column = column;
  		this.line = line;
  		this.setFormula(formula);
  	}
  	
  	public double getValue() {
  		return this.value;
  	}
  	
  	public Formula getFormula() {
  		return this.formula;
  	}
  	
  	public String getDevelopedFormula() {
  		return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId();
  	}
  	
  	public String getId() {
  		return this.column + this.line;
  	}
  	
  	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();
  	}
  	
02c44758   Remi   finish ihm
61
62
63
64
65
66
67
  	public void updateUsedIn() {
  		if (this.containFormula()) {
  			for (Cell cell : this.formula.getUtilisedCells())
  				cell.getUsedIn().remove(this);
  		}
  	}
  	
4186cd92   Remi   fix tests
68
69
70
71
72
73
  	public boolean containFormula() {
  		return this.formula != null;
  	}
  	
  	public void setFormula(Formula formula) throws CreateCycleException {
  		if (formula.createCycle(this))
080a0e68   Remi   change return
74
  			throw new CreateCycleException("L'assignation de la formule " + formula.toString() + " créée un cycle.");
4186cd92   Remi   fix tests
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  		
  		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();
  		}
  	}
  	
080a0e68   Remi   change return
96
97
98
99
100
101
102
103
104
105
106
  	@Override
  	public boolean equals(Object o) {
  		if (this == o)
  			return true;
  		if (!(o instanceof Cell))
  			return false;
  		
  		Cell cell = (Cell) o;
  		
  		return line == cell.line && column.equals(cell.column);
  	}
2747b3d2   Remi   update tests
107
  	
080a0e68   Remi   change return
108
109
110
111
  	@Override
  	public int hashCode() {
  		return Objects.hash(column, line);
  	}
d8507aee   [mandjemb]   Fichiers
112
  }