Blame view

src/kernel/function/Function.java 1.43 KB
2e8fbd04   Remi   global refactor
1
2
3
4
  package kernel.function;
  
  import kernel.Cell;
  import kernel.Formula;
94ba86ff   Remi   Auto stash before...
5
6
  import kernel.Grid;
  import kernel.LanguageEnum;
2e8fbd04   Remi   global refactor
7
  
eb07e5e3   [mandjemb]   avec serialisation
8
  import java.io.Serializable;
94ba86ff   Remi   Auto stash before...
9
  import java.util.HashMap;
94ba86ff   Remi   Auto stash before...
10
11
12
  import java.util.List;
  import java.util.Map;
  import java.util.stream.Collectors;
2e8fbd04   Remi   global refactor
13
  
e0bdcd83   Remi   refacto
14
  abstract public class Function implements Formula, Serializable {
4186cd92   Remi   fix tests
15
16
  	
  	private static final long serialVersionUID = 1L;
080a0e68   Remi   change return
17
18
  	private Map<LanguageEnum, String> names = new HashMap<>();
  	private List<Cell> listCells;
4186cd92   Remi   fix tests
19
20
21
22
23
24
25
  	
  	public Function(List<Cell> listCells) {
  		this.listCells = listCells;
  	}
  	
  	abstract public double eval();
  	
080a0e68   Remi   change return
26
27
28
29
30
  	public Map<LanguageEnum, String> getNames() {
  		return this.names;
  	}
  	
  	@Override
4186cd92   Remi   fix tests
31
32
33
34
35
36
37
  	public String getDevelopedFormula() {
  		return names.get(Grid.language) + "("
  				+ this.listCells.stream()
  				.map(Cell::getDevelopedFormula)
  				.collect(Collectors.joining(",")) + ")";
  	}
  	
080a0e68   Remi   change return
38
  	@Override
4186cd92   Remi   fix tests
39
40
41
42
43
44
45
  	public String toString() {
  		return names.get(Grid.language) + "("
  				+ this.listCells.stream()
  				.map(Cell::getId)
  				.collect(Collectors.joining(",")) + ")";
  	}
  	
080a0e68   Remi   change return
46
  	@Override
4186cd92   Remi   fix tests
47
  	public boolean createCycle(Cell cell) {
080a0e68   Remi   change return
48
  		return this.listCells.contains(cell) || this.generateIndirectCycle(cell);
4186cd92   Remi   fix tests
49
50
  	}
  	
080a0e68   Remi   change return
51
  	@Override
4186cd92   Remi   fix tests
52
53
54
  	public List<Cell> getUtilisedCells() {
  		return this.listCells;
  	}
080a0e68   Remi   change return
55
56
57
58
59
60
61
62
  	
  	private boolean generateIndirectCycle(Cell cell) {
  		for (Cell currentCell : this.listCells)
  			if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell))
  				return true;
  		
  		return false;
  	}
2e8fbd04   Remi   global refactor
63
  }