Commit e0bdcd838a5cc919297b084b9ea02b67535c77fd

Authored by Remi
1 parent 35c209df

refacto

src/app/Application.java
... ... @@ -12,31 +12,22 @@ import java.io.File;
12 12 import java.io.FileOutputStream;
13 13 import java.io.IOException;
14 14 import java.io.ObjectOutputStream;
15   -import java.io.Serializable;
16 15 import java.util.ArrayList;
17 16 import java.util.List;
18 17  
  18 +public class Application {
19 19  
20   -public class Application implements Serializable{
  20 + public static void main(String[] args) throws IOException {
  21 + File fichier = new File("essai.ser");
21 22  
  23 + // ouverture d'un flux sur un fichier
  24 + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fichier));
22 25  
23   - //private static final long serialVersionUID = -5923230541404119541L;
24   -
25   - private static final long serialVersionUID = 1L;
26   -
27   - public static void main(String[] args) throws IOException {
28   -
29   - File fichier = new File("essai.ser") ;
30   -
31   - // ouverture d'un flux sur un fichier
32   - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fichier)) ;
33   -
34   - Grid grid = new Grid();
  26 + Grid grid = new Grid();
35 27  
36 28 // Création
37 29 System.out.println("Création de quelques cases...");
38 30  
39   -
40 31 try {
41 32 grid.createCell("A", 1, 10.);
42 33 grid.createCell("B", 1, 0.);
... ... @@ -52,14 +43,9 @@ public class Application implements Serializable{
52 43 averageList.add(grid.getCell("B", 1));
53 44  
54 45 grid.createCell("B", 2, new Average(averageList));
55   - } catch (CellNotFoundException exception) {
  46 + } catch (CellNotFoundException | CreateCycleException | InvalidIntervalLineColumnEception exception) {
56 47 System.out.println(exception.getMessage());
57   - } catch (CreateCycleException exception) {
58   - System.out.println(exception.toString());
59 48 }
60   - catch (InvalidIntervalLineColumnEception exception) {
61   - System.out.println(exception.toString());
62   - }
63 49  
64 50 // Affichage
65 51 List<Cell> cells = grid.getCells();
... ... @@ -86,23 +72,9 @@ public class Application implements Serializable{
86 72 System.out.println("Affichage des valeurs après modification :");
87 73 for (Cell cell : cells)
88 74 System.out.println(cell.getId() + ": " + cell.getValue());
89   -
90 75  
91 76 // sérialization de l'objet
92   -
93 77 oos.writeObject(grid);
94 78 oos.close();
95 79 }
96   -
97   -
98   -
99   -
100   -
101   -
102   -
103   -
104   -
105   -
106   -
107   -
108 80 }
... ...
src/kernel/Cell.java
... ... @@ -9,98 +9,94 @@ import java.util.List;
9 9  
10 10 public class Cell implements Serializable {
11 11  
12   - private static final long serialVersionUID = 1L;
13   -
14   - final int MAX_LIGNES = 20;
15   -
16   - private String column;
17   - private int line;
18   - private double value;
19   - private Formula formula;
20   - private List<Cell> usedIn = new ArrayList<>();
21   -
22   - public boolean IntervallCondition(String column, int line){
23   -
24   - return line >= 1 && line <= MAX_LIGNES && column.compareTo("A") >= 0 && column.compareTo("Z") <= 0;
25   -
26   - }
27   -
28   - public Cell(String column,int line, double value) throws InvalidIntervalLineColumnEception {
29   - column = column.toUpperCase();
30   - if (IntervallCondition(column,line)) {
31   - this.column = column;
32   - this.line = line;
33   - this.setValue(value);
34   - } else
35   - throw new InvalidIntervalLineColumnEception();
36   - }
37   -
38   - public Cell(String column, int line, Formula formula)
39   - throws CreateCycleException, InvalidIntervalLineColumnEception {
40   - column = column.toUpperCase();
41   - if (IntervallCondition(column,line)) {
42   - this.column = column;
43   - this.line = line;
44   - this.setFormula(formula);
45   - } else
46   - throw new InvalidIntervalLineColumnEception();
47   -
48   - }
49   -
50   - public double getValue() {
51   - return this.value;
52   - }
53   -
54   - public Formula getFormula() {
55   - return this.formula;
56   - }
57   -
58   - public String getDevelopedFormula() {
59   - return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId();
60   - }
61   -
62   - public String getId() {
63   - return this.column + this.line;
64   - }
65   -
66   - public List<Cell> getUsedIn() {
67   - return this.usedIn;
68   - }
69   -
70   - public String toString() {
71   - return this.containFormula() ? this.formula.toString() : this.getId();
72   - }
73   -
74   - public void updateValue() {
75   - if (this.containFormula())
76   - this.value = this.formula.eval();
77   - }
78   -
79   - public Boolean containFormula() {
80   - return this.formula != null;
81   - }
82   -
83   - public void setFormula(Formula formula) throws CreateCycleException {
84   - if (formula.createCycle(this))
85   - throw new CreateCycleException();
86   -
87   - this.formula = formula;
88   - for (Cell cell : this.formula.getUtilisedCells())
89   - cell.usedIn.add(this);
90   - this.updateValue();
91   - this.spreadValue();
92   - }
93   -
94   - public void setValue(Double value) {
95   - this.value = value;
96   - this.formula = null;
97   - this.spreadValue();
98   - }
99   -
100   - private void spreadValue() {
101   - for (Cell cell : this.usedIn) {
102   - cell.updateValue();
103   - cell.spreadValue();
104   - }
105   - }
  12 + private static final long serialVersionUID = 1L;
  13 + private static final int MAX_LIGNES = 20;
  14 +
  15 + private String column;
  16 + private int line;
  17 + private double value;
  18 + private Formula formula;
  19 + private List<Cell> usedIn = new ArrayList<>();
  20 +
  21 + public Cell(String column, int line, double value) throws InvalidIntervalLineColumnEception {
  22 + column = column.toUpperCase();
  23 + if (!validateInterval(column, line))
  24 + throw new InvalidIntervalLineColumnEception();
  25 +
  26 + this.column = column;
  27 + this.line = line;
  28 + this.setValue(value);
  29 + }
  30 +
  31 + public Cell(String column, int line, Formula formula)
  32 + throws CreateCycleException, InvalidIntervalLineColumnEception {
  33 + column = column.toUpperCase();
  34 + if (!validateInterval(column, line))
  35 + throw new InvalidIntervalLineColumnEception();
  36 +
  37 + this.column = column;
  38 + this.line = line;
  39 + this.setFormula(formula);
  40 + }
  41 +
  42 + public double getValue() {
  43 + return this.value;
  44 + }
  45 +
  46 + public Formula getFormula() {
  47 + return this.formula;
  48 + }
  49 +
  50 + public String getDevelopedFormula() {
  51 + return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId();
  52 + }
  53 +
  54 + public String getId() {
  55 + return this.column + this.line;
  56 + }
  57 +
  58 + public List<Cell> getUsedIn() {
  59 + return this.usedIn;
  60 + }
  61 +
  62 + public String toString() {
  63 + return this.containFormula() ? this.formula.toString() : this.getId();
  64 + }
  65 +
  66 + public void updateValue() {
  67 + if (this.containFormula())
  68 + this.value = this.formula.eval();
  69 + }
  70 +
  71 + public boolean containFormula() {
  72 + return this.formula != null;
  73 + }
  74 +
  75 + public void setFormula(Formula formula) throws CreateCycleException {
  76 + if (formula.createCycle(this))
  77 + throw new CreateCycleException();
  78 +
  79 + this.formula = formula;
  80 + for (Cell cell : this.formula.getUtilisedCells())
  81 + cell.usedIn.add(this);
  82 + this.updateValue();
  83 + this.spreadValue();
  84 + }
  85 +
  86 + public void setValue(double value) {
  87 + this.value = value;
  88 + this.formula = null;
  89 + this.spreadValue();
  90 + }
  91 +
  92 + private void spreadValue() {
  93 + for (Cell cell : this.usedIn) {
  94 + cell.updateValue();
  95 + cell.spreadValue();
  96 + }
  97 + }
  98 +
  99 + private boolean validateInterval(String column, int line) {
  100 + return line >= 1 && line <= MAX_LIGNES && column.compareTo("A") >= 0 && column.compareTo("Z") <= 0;
  101 + }
106 102 }
... ...
src/kernel/Grid.java
... ... @@ -12,9 +12,9 @@ import java.util.Map;
12 12  
13 13 public class Grid implements Serializable {
14 14  
15   - private static final long serialVersionUID = 1L;
16   -
17   - private Map<String, Cell> cells = new HashMap<>();
  15 + private static final long serialVersionUID = 1L;
  16 +
  17 + private Map<String, Cell> cells = new HashMap<>();
18 18 public static LanguageEnum language = LanguageEnum.FR;
19 19  
20 20 public String createCell(String column, int line, double value) throws InvalidIntervalLineColumnEception {
... ... @@ -57,7 +57,7 @@ public class Grid implements Serializable {
57 57 return new ArrayList<>(this.cells.values());
58 58 }
59 59  
60   - public Double getValue(String column, int line) throws CellNotFoundException {
  60 + public double getValue(String column, int line) throws CellNotFoundException {
61 61 return this.getCell(column, line).getValue();
62 62 }
63 63  
... ...
src/kernel/function/Average.java
... ... @@ -8,16 +8,15 @@ import java.util.OptionalDouble;
8 8  
9 9 public class Average extends Function {
10 10  
11   - private static final long serialVersionUID = 1L;
  11 + private static final long serialVersionUID = 1L;
12 12  
13   - public Average(List<Cell> listCells) {
  13 + public Average(List<Cell> listCells) {
14 14 super(listCells);
15 15 this.names.put(LanguageEnum.FR, "MOYENNE");
16 16 this.names.put(LanguageEnum.EN, "AVERAGE");
17 17 }
18 18  
19 19 public double eval() {
20   -
21 20 OptionalDouble average = this.listCells.stream()
22 21 .mapToDouble(Cell::getValue)
23 22 .average();
... ...
src/kernel/function/Function.java
... ... @@ -11,11 +11,10 @@ import java.util.List;
11 11 import java.util.Map;
12 12 import java.util.stream.Collectors;
13 13  
14   -abstract public class Function implements Formula,Serializable {
  14 +abstract public class Function implements Formula, Serializable {
15 15  
16   -
17   - private static final long serialVersionUID = 1L;
18   - public Map<LanguageEnum, String> names = new HashMap<>();
  16 + private static final long serialVersionUID = 1L;
  17 + public Map<LanguageEnum, String> names = new HashMap<>();
19 18 public List<Cell> listCells;
20 19  
21 20 public Function(List<Cell> listCells) {
... ... @@ -39,32 +38,15 @@ abstract public class Function implements Formula,Serializable {
39 38 }
40 39  
41 40 public boolean createCycle(Cell cell) {
42   -
43   - if (!this.listCells.contains(cell))
44   - for(Cell currentCell:this.listCells)
45   - return currentCell.containFormula() && currentCell.getFormula().createCycle(cell);
  41 + if (!this.listCells.contains(cell))
  42 + for (Cell currentCell : this.listCells)
  43 + if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell))
  44 + return true;
46 45  
47   - return true;
  46 + return true;
48 47 }
49 48  
50   - /*
51   - public Boolean createCycle(Cell cell) {
52   - boolean cycle = false;
53   - if (!this.listCells.contains(cell)) {
54   - Iterator<Cell> it = listCells.iterator();
55   - while (it.hasNext() && !cycle) {
56   - Cell currentCell = it.next();
57   - if (currentCell.containFormula()) {
58   - cycle = currentCell.getFormula().createCycle(cell);
59   - }
60   - }
61   - return cycle;
62   - } else
63   - return true;
64   - }
65   -
66   - */
67 49 public List<Cell> getUtilisedCells() {
68   - return this.listCells;
69   - }
  50 + return this.listCells;
  51 + }
70 52 }
... ...
src/kernel/function/Sum.java
... ... @@ -7,10 +7,9 @@ import java.util.List;
7 7  
8 8 public class Sum extends Function {
9 9  
  10 + private static final long serialVersionUID = 1L;
10 11  
11   - private static final long serialVersionUID = 1L;
12   -
13   - public Sum(List<Cell> listCells) {
  12 + public Sum(List<Cell> listCells) {
14 13 super(listCells);
15 14 this.names.put(LanguageEnum.FR, "SOMME");
16 15 this.names.put(LanguageEnum.EN, "SUM");
... ...
src/kernel/operation/Addition.java
... ... @@ -4,18 +4,16 @@ import kernel.Cell;
4 4  
5 5 public class Addition extends BinaryOperation {
6 6  
  7 + private static final long serialVersionUID = 1L;
7 8  
8   - private static final long serialVersionUID = 1L;
9   -
10   - public Addition(Cell leftCell, Cell rightCell) {
  9 + public Addition(Cell leftCell, Cell rightCell) {
11 10 super(leftCell, rightCell);
12   -
13 11 }
14   -
15   - public String getOperator(){
16   - return "+";
17   - }
18   -
  12 +
  13 + public String getOperator() {
  14 + return "+";
  15 + }
  16 +
19 17 public double eval() {
20 18 return this.leftCell.getValue() + this.rightCell.getValue();
21 19 }
... ...
src/kernel/operation/BinaryOperation.java
... ... @@ -7,12 +7,11 @@ import java.io.Serializable;
7 7 import java.util.ArrayList;
8 8 import java.util.List;
9 9  
10   -abstract public class BinaryOperation implements Formula,Serializable {
  10 +abstract public class BinaryOperation implements Formula, Serializable {
11 11  
12   - private static final long serialVersionUID = 1L;
13   - protected Cell leftCell;
  12 + private static final long serialVersionUID = 1L;
  13 + protected Cell leftCell;
14 14 protected Cell rightCell;
15   -
16 15  
17 16 public BinaryOperation(Cell leftCell, Cell rightCell) {
18 17 this.leftCell = leftCell;
... ... @@ -20,33 +19,30 @@ abstract public class BinaryOperation implements Formula,Serializable {
20 19 }
21 20  
22 21 abstract public double eval();
  22 +
23 23 abstract public String getOperator();
  24 +
24 25 public String getDevelopedFormula() {
25   - return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula()+")";
  26 + return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")";
26 27 }
27 28  
28 29 public String toString() {
29 30 return this.leftCell.getId() + this.getOperator() + this.rightCell.getId();
30 31 }
31 32  
32   -
33 33 public boolean createCycle(Cell cell) {
34 34 if (this.leftCell.containFormula() && !this.rightCell.containFormula())
35 35 return this.leftCell.getFormula().createCycle(cell);
36   -
  36 +
37 37 if (!this.leftCell.containFormula() && this.rightCell.containFormula())
38 38 return this.rightCell.getFormula().createCycle(cell);
39   -
  39 +
40 40 if (this.leftCell.containFormula() && this.rightCell.containFormula())
41 41 return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell);
42   -
43   - return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId()));
44 42  
  43 + return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId()));
45 44 }
46 45  
47   -
48   -
49   -
50 46 public List<Cell> getUtilisedCells() {
51 47 List<Cell> cells = new ArrayList<>();
52 48 cells.add(this.leftCell);
... ...
src/kernel/operation/Division.java
... ... @@ -4,21 +4,16 @@ import kernel.Cell;
4 4  
5 5 public class Division extends BinaryOperation {
6 6  
  7 + private static final long serialVersionUID = 1L;
7 8  
8   - private static final long serialVersionUID = 1L;
9   -
10   - public Division(Cell leftCell, Cell rightCell) {
  9 + public Division(Cell leftCell, Cell rightCell) {
11 10 super(leftCell, rightCell);
12   -
13 11 }
14 12  
15   -
16   -
17   - public String getOperator(){
18   - return "/";
19   - }
20   -
21   -
  13 + public String getOperator() {
  14 + return "/";
  15 + }
  16 +
22 17 public double eval() {
23 18 return this.leftCell.getValue() / this.rightCell.getValue();
24 19 }
... ...
src/kernel/operation/Multiplication.java
... ... @@ -4,18 +4,16 @@ import kernel.Cell;
4 4  
5 5 public class Multiplication extends BinaryOperation {
6 6  
7   - private static final long serialVersionUID = 1L;
  7 + private static final long serialVersionUID = 1L;
8 8  
9   - public Multiplication(Cell leftCell, Cell rightCell) {
  9 + public Multiplication(Cell leftCell, Cell rightCell) {
10 10 super(leftCell, rightCell);
  11 + }
11 12  
  13 + public String getOperator() {
  14 + return "*";
12 15 }
13   -
14   - public String getOperator(){
15   - return "*";
16   - }
17   -
18   -
  16 +
19 17 public double eval() {
20 18 return this.leftCell.getValue() * this.rightCell.getValue();
21 19 }
... ...
src/kernel/operation/Subtraction.java
... ... @@ -4,19 +4,17 @@ import kernel.Cell;
4 4  
5 5 public class Subtraction extends BinaryOperation {
6 6  
7   - private static final long serialVersionUID = 1L;
  7 + private static final long serialVersionUID = 1L;
8 8  
9   - public Subtraction(Cell leftCell, Cell rightCell) {
  9 + public Subtraction(Cell leftCell, Cell rightCell) {
10 10 super(leftCell, rightCell);
  11 + }
  12 +
11 13  
  14 + public String getOperator() {
  15 + return "-";
12 16 }
13 17  
14   -
15   - public String getOperator(){
16   - return "-";
17   - }
18   -
19   -
20 18 public double eval() {
21 19 return this.leftCell.getValue() - this.rightCell.getValue();
22 20 }
... ...