Skip to content

Commit 5fffdc0

Browse files
committed
checked all exceptions
1 parent 9cff639 commit 5fffdc0

File tree

13 files changed

+63
-32
lines changed

13 files changed

+63
-32
lines changed

GA-Maven/src/main/java/org/agroplanner/Main.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ public static void main(String[] args) {
1515
/*
1616
* OBIETTIVI
1717
* 1. RISCRIVI I COMMENTI
18-
* 2. CONTROLLARE CODE SMELLS
19-
* 3. ricontrollare i flussi di exception
2018
*/

GA-Maven/src/main/java/org/agroplanner/domainsystem/controllers/DomainService.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@
2424
*/
2525
public class DomainService {
2626

27-
private final DomainFactory factory;
28-
2927
/**
3028
* Initializes the service and retrieves the singleton instance of the Factory.
3129
*/
32-
public DomainService() {
33-
this.factory = DomainFactory.getInstance();
34-
}
30+
public DomainService() { }
3531

3632
/**
3733
* Retrieves the list of supported domain types.
@@ -62,7 +58,7 @@ public List<DomainType> getAvailableDomainTypes() {
6258
* @return A new, fully validated instance of {@link Domain}.
6359
*/
6460
public Domain createDomain(DomainType type, Map<String, Double> params) {
65-
return factory.createDomain(type, params);
61+
return DomainFactory.getInstance().createDomain(type, params);
6662
}
6763

6864
/**

GA-Maven/src/main/java/org/agroplanner/domainsystem/model/DomainFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
*
2121
* <p><strong>Pattern:</strong> Singleton (Initialization-on-demand holder idiom).</p>
2222
*/
23-
public final class DomainFactory {
24-
23+
public class DomainFactory {
2524

2625
/**
2726
* Private constructor to prevent direct instantiation.

GA-Maven/src/main/java/org/agroplanner/exportsystem/controllers/ExportConsoleController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public void runExportWizard(Individual solution, Domain domain, PlantInventory i
5656
while (true) {
5757

5858
// STEP 1: Select Format
59-
view.showAvailableExports(service.getAvailableTypes());
60-
Optional<ExportType> selectedType = view.askForExportType(service.getAvailableTypes());
59+
view.showAvailableExports(service.getAvailableExportTypes());
60+
Optional<ExportType> selectedType = view.askForExportType(service.getAvailableExportTypes());
6161

6262
// If user cancels (returns Empty), exit the wizard.
6363
if (selectedType.isEmpty()) return;

GA-Maven/src/main/java/org/agroplanner/exportsystem/controllers/ExportService.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,16 @@
2929
*/
3030
public class ExportService {
3131

32-
private final ExporterFactory factory;
33-
3432
/**
3533
* Initializes the service by retrieving the Factory instance.
3634
*/
37-
public ExportService() {
38-
this.factory = ExporterFactory.getInstance();
39-
}
35+
public ExportService() { }
4036

4137
/**
4238
* Retrieves the list of supported export formats.
4339
* @return A list of all available {@link ExportType}s.
4440
*/
45-
public List<ExportType> getAvailableTypes() {
41+
public List<ExportType> getAvailableExportTypes() {
4642
return Arrays.asList(ExportType.values());
4743
}
4844

@@ -86,7 +82,7 @@ public String performExport(Individual solution, Domain domain, PlantInventory i
8682
// --- EXECUTION & ERROR WRAPPING ---
8783
try {
8884
// Get the specific strategy from the factory
89-
BaseExporter exporter = factory.createExporter(type);
85+
BaseExporter exporter = ExporterFactory.getInstance().createExporter(type);
9086

9187
// Execute the template method.
9288
// If export() throws IOException, we catch it here.

GA-Maven/src/main/java/org/agroplanner/exportsystem/model/ExporterFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public class ExporterFactory {
2020
// ------------------- SINGLETON PATTERN -------------------
2121

2222
/** Private constructor to prevent direct instantiation. */
23-
private ExporterFactory() {}
23+
private ExporterFactory() {
24+
25+
}
2426

2527
/**
2628
* Static Inner Class (Lazy Holder).

GA-Maven/src/main/java/org/agroplanner/exportsystem/model/types/CSVExporter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ protected void performExport(Individual individual, Domain domain, PlantInventor
7474

7575
for (InventoryEntry entry : inventory.getEntries()) {
7676
// Esempio output: # - TOMATO (🍅): 50 units, r=1.50
77-
writer.write(String.format(Locale.US, "# - %s (%s): %d units, r=%.2f",
78-
entry.getType().name(), // Nome (ex key)
79-
entry.getType().getLabel(), // Emoji (ex key)
77+
writer.write(String.format(Locale.US, "# - %s (%s): %d units, r=%.2fm",
78+
entry.getType().name(), // Nome
79+
entry.getType().getLabel(), // Emoji
8080
entry.getQuantity(), // Quantità
8181
entry.getRadius())); // Raggio specificato per questa riga
8282

GA-Maven/src/main/java/org/agroplanner/exportsystem/model/types/PdfExporter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected void performExport(Individual individual, Domain domain, PlantInventor
8888
for (InventoryEntry item : inventory.getEntries()) {
8989

9090
// E.g.: " - 🍅 TOMATO: 50 units (r=1.50)"
91-
String line = String.format(Locale.US, " - %s %s: %d units (r=%.2f)",
91+
String line = String.format(Locale.US, " - %s %s: %d units (r=%.2fm)",
9292
item.getType().getLabel(), // Emoji (recuperato da getType)
9393
item.getType().name(), // Nome (recuperato da getType)
9494
item.getQuantity(), // Quantità diretta dell'entry
@@ -122,9 +122,9 @@ protected void performExport(Individual individual, Domain domain, PlantInventor
122122
// Headers
123123
addHeaderCell(table, "ID");
124124
addHeaderCell(table, "Type");
125-
addHeaderCell(table, "X");
126-
addHeaderCell(table, "Y");
127-
addHeaderCell(table, "Radius");
125+
addHeaderCell(table, "X(m)");
126+
addHeaderCell(table, "Y(m)");
127+
addHeaderCell(table, "Radius(m)");
128128

129129
// Data Population
130130
int index = 0;

GA-Maven/src/main/java/org/agroplanner/exportsystem/model/types/TxtExporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected void performExport(Individual individual, Domain domain, PlantInventor
6565
// Iteriamo sulla lista degli inserimenti
6666
for (InventoryEntry entry : inventory.getEntries()) {
6767
// e.g. " - 🍅 TOMATO: 50 units (r=1.50)"
68-
String line = String.format(Locale.US, " - %s %s: %d units (r=%.2f)",
68+
String line = String.format(Locale.US, " - %s %s: %d units (r=%.2fm)",
6969
entry.getType().getLabel(), // Emoji
7070
entry.getType().name(), // Nome
7171
entry.getQuantity(), // Quantità
@@ -89,7 +89,7 @@ protected void performExport(Individual individual, Domain domain, PlantInventor
8989
// --- DATA LIST SECTION ---
9090
writer.write("--- Chromosomes (Coordinates) ---");
9191
writer.newLine();
92-
writer.write("Format: [ID] TYPE | X | Y | Radius");
92+
writer.write("Format: [ID] TYPE | X(m) | Y(m) | Radius(m)");
9393
writer.newLine();
9494

9595
int index = 0;

GA-Maven/src/main/java/org/agroplanner/gasystem/controllers/EvolutionConsoleController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public Individual runEvolution() throws EvolutionTimeoutException, MaxAttemptsEx
8585
// EXECUTION: Delegate heavy lifting to the Service
8686
// This blocks until the cycle finishes or timeouts.
8787
lastSolution = service.executeEvolutionCycle();
88+
// EXECUTION & TIMEOUT MANAGEMENT
89+
// We purposefully do NOT catch EvolutionTimeoutException here.
90+
// Logic:
91+
// 1. If the Service enforces a timeout, it means the calculation is stuck.
92+
// 2. We apply the "Fail Fast" principle: immediately bubble up the exception
93+
// to the MainController to abort the session and free resources.
94+
// 3. Retrying locally (looping) after a timeout is usually futile and risks freezing the UI.
8895

8996
Instant end = Instant.now();
9097
double durationMs = Duration.between(start, end).toMillis();
@@ -105,6 +112,21 @@ public Individual runEvolution() throws EvolutionTimeoutException, MaxAttemptsEx
105112
} while (currentAttempt < MAX_RETRY_ATTEMPTS);
106113

107114

115+
// ==================================================================================
116+
// CRITICAL FAILURE HANDLING STRATEGY (Bubble-Up)
117+
// ==================================================================================
118+
// Why do we throw exceptions here instead of handling them?
119+
//
120+
// 1. EXHAUSTED RECOVERY: The 'do-while' loop above IS the local recovery mechanism.
121+
// If we exit the loop, it means the algorithm failed to converge even after
122+
// MAX_RETRY_ATTEMPTS. The controller has done all it can.
123+
//
124+
// 2. FLOW CONTROL: By throwing the exception to the MainController, we trigger
125+
// a clean "Session Abort" and reset. If we caught it here, we would have to
126+
// return null/Optional, forcing the MainController to perform null-checks.
127+
// Exceptions provide a cleaner control flow for fatal errors.
128+
// ==================================================================================
129+
108130
// CRITICAL FAILURE (Exhausted Attempts)
109131
// If we exit the loop, it means we failed N times.
110132
// We construct a detailed error report and abort the session.

0 commit comments

Comments
 (0)