From d0959f535bbf833ded3c9e59a30037b6f0fea96a Mon Sep 17 00:00:00 2001 From: David Guler Date: Mon, 28 Nov 2022 11:35:17 +0100 Subject: [PATCH 1/2] refactor: javadoc and dependencies in HashMap --- .../gartenverwaltung/HelloController.java | 14 ------ .../bootstrap/AfterInject.java | 2 +- .../gartenverwaltung/bootstrap/AppLoader.java | 49 ++++++++++--------- .../bootstrap/ChangeViewEvent.java | 9 ++++ .../gartenverwaltung/bootstrap/Inject.java | 2 +- .../ch/zhaw/gartenverwaltung/io/CropList.java | 4 ++ .../zhaw/gartenverwaltung/io/IdProvider.java | 8 +++ .../gartenverwaltung/io/JsonCropList.java | 7 ++- .../gartenverwaltung/io/JsonPlantList.java | 14 ++++-- .../gartenverwaltung/io/JsonTaskList.java | 12 ++++- .../zhaw/gartenverwaltung/io/PlantList.java | 2 +- .../ch/zhaw/gartenverwaltung/io/TaskList.java | 4 +- .../json/GrowthPhaseTypeDeserializer.java | 3 ++ .../json/HardinessZoneDeserializer.java | 3 ++ .../json/PlantImageDeserializer.java | 3 ++ 15 files changed, 88 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/ch/zhaw/gartenverwaltung/HelloController.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/HelloController.java b/src/main/java/ch/zhaw/gartenverwaltung/HelloController.java deleted file mode 100644 index 7e952ac..0000000 --- a/src/main/java/ch/zhaw/gartenverwaltung/HelloController.java +++ /dev/null @@ -1,14 +0,0 @@ -package ch.zhaw.gartenverwaltung; - -import javafx.fxml.FXML; -import javafx.scene.control.Label; - -public class HelloController { - @FXML - private Label welcomeText; - - @FXML - protected void onHelloButtonClick() { - welcomeText.setText("Welcome to JavaFX Application!"); - } -} \ No newline at end of file diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AfterInject.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AfterInject.java index 72a88fd..23772bd 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AfterInject.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AfterInject.java @@ -6,7 +6,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Annotates a method to be executed after all dependencies annotates with {@link Inject} + * Annotates a method to be executed after all dependencies annotated with {@link Inject} * have been injected. */ @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java index 9c14010..9610471 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/AppLoader.java @@ -23,6 +23,10 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; +/** + * Class responsible for bootstrapping the application wide dependencies + * and injecting them into JavaFX Controllers. + */ public class AppLoader { /** * Caching the panes @@ -32,17 +36,24 @@ public class AppLoader { /** * Application-wide dependencies */ - private final PlantList plantList = new JsonPlantList(); - private final CropList cropList = new JsonCropList(); - private final TaskList taskList = new JsonTaskList(); - - private final GardenSchedule gardenSchedule = new GardenSchedule(taskList, plantList); - private final Garden garden = new Garden(gardenSchedule, cropList); - + private final Map dependencies = new HashMap<>(); public AppLoader() throws IOException { - } + PlantList plantList = new JsonPlantList(); + CropList cropList = new JsonCropList(); + TaskList taskList = new JsonTaskList(); + GardenSchedule gardenSchedule = new GardenSchedule(taskList, plantList); + dependencies.put(PlantList.class.getSimpleName(), plantList); + dependencies.put(CropList.class.getSimpleName(), cropList); + dependencies.put(TaskList.class.getSimpleName(), taskList); + + dependencies.put(PlantListModel.class.getSimpleName(), new PlantListModel(plantList)); + + dependencies.put(GardenSchedule.class.getSimpleName(), gardenSchedule); + dependencies.put(Garden.class.getSimpleName(), new Garden(gardenSchedule, cropList)); + dependencies.put(AppLoader.class.getSimpleName(), this); + } /** * Loads and returns a {@link Pane} (cached). @@ -135,26 +146,18 @@ public class AppLoader { }); Arrays.stream(controller.getClass().getMethods()) - .filter(method -> method.isAnnotationPresent(AfterInject.class)) + .filter(method -> method.isAnnotationPresent(AfterInject.class) && method.getParameterCount() == 0) .forEach(afterInjectMethod -> { - if (afterInjectMethod.getParameterCount() == 0) { - try { - afterInjectMethod.invoke(controller); - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } + try { + afterInjectMethod.invoke(controller); + } catch (IllegalAccessException | InvocationTargetException e) { + // TODO: Log + e.printStackTrace(); } }); } private Object getAppDependency(Class type) { - return switch (type.getSimpleName()) { - case "Garden" -> garden; - case "PlantList" -> plantList; - case "PlantListModel" -> new PlantListModel(plantList); - case "GardenSchedule" -> gardenSchedule; - case "AppLoader" -> this; - default -> null; - }; + return dependencies.get(type.getSimpleName()); } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/ChangeViewEvent.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/ChangeViewEvent.java index 4388ebe..f8242d3 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/ChangeViewEvent.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/ChangeViewEvent.java @@ -3,11 +3,20 @@ package ch.zhaw.gartenverwaltung.bootstrap; import javafx.event.Event; import javafx.event.EventType; +/** + * Represents an event that should lead to a view being changed. + */ public class ChangeViewEvent extends Event { private final String view; public static final EventType CHANGE_MAIN_VIEW = new EventType<>("CHANGE_MAIN_VIEW"); + /** + * Creates an Event that should lead to the main view being changed. + * + * @param eventType The {@link EventType} specifying which view should be changed. + * @param view The filename of the View to be changed to + */ public ChangeViewEvent(EventType eventType, String view) { super(eventType); this.view = view; diff --git a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/Inject.java b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/Inject.java index 02409d5..803844f 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/Inject.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/bootstrap/Inject.java @@ -6,7 +6,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Annotates a Field to be injected from the application-dependencies + * Annotates a Field to be injected from the application-dependencies. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/CropList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/CropList.java index 7b9fd47..0805c99 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/CropList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/CropList.java @@ -6,6 +6,10 @@ import java.io.IOException; import java.util.List; import java.util.Optional; +/** + * Represents a List of {@link Crop}s. + * The interface specifies the operations to add/update and remove entries. + */ public interface CropList { /** * Yields a list of all {@link Crop}s in the database. diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/IdProvider.java b/src/main/java/ch/zhaw/gartenverwaltung/io/IdProvider.java index 5842f0d..49fd868 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/IdProvider.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/IdProvider.java @@ -1,10 +1,18 @@ package ch.zhaw.gartenverwaltung.io; +/** + * Provides an sequential ID starting from the given initial Value. + */ public class IdProvider { private long currentId; public IdProvider(long initialValue) { currentId = initialValue; } + + /** + * Yields the next ID in the sequence. + * @return The next ID + */ public long incrementAndGet() { return ++currentId; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonCropList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonCropList.java index c7fd0ea..aa30391 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonCropList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonCropList.java @@ -19,6 +19,11 @@ import java.util.List; import java.util.Map; import java.util.Optional; +/** + * Implements the {@link CropList} interface for reading and writing {@link Crop} objects + * from and to a local JSON file. + * The reads are cached to minimize file-io operations. + */ public class JsonCropList implements CropList { private final URL dataSource; @@ -40,7 +45,7 @@ public class JsonCropList implements CropList { } /** - * Default constructor + * Default constructor. Uses a file from the app resources. */ public JsonCropList() { this.dataSource = getClass().getResource("user-crops.json"); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantList.java index 7b76f70..ec46d67 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantList.java @@ -20,8 +20,8 @@ import java.util.Map; import java.util.Optional; /** - * Implements the {@link PlantList} interface for loading {@link Plant} objects - * from a JSON file. + * Implements the {@link PlantList} interface for reading {@link Plant} objects + * from a local JSON file. * The reads are cached to minimize file-io operations. */ public class JsonPlantList implements PlantList { @@ -44,9 +44,17 @@ public class JsonPlantList implements PlantList { imageModule.addDeserializer(Image.class, new PlantImageDeserializer()); } + /** + * Default constructor. Uses a file from the app resources. + */ public JsonPlantList() { this.dataSource = getClass().getResource("plantdb.json"); } + + /** + * Constructor to use a specified {@link URL} as a {@link #dataSource} + * @param dataSource A {@link URL} to the file to be used as a data source + */ public JsonPlantList(URL dataSource) { this.dataSource = dataSource; } @@ -67,7 +75,7 @@ public class JsonPlantList implements PlantList { } /** - * @see PlantList#getPlantById(long) + * @see PlantList#getPlantById(HardinessZone, long) */ @Override public Optional getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java index 7f6d36b..90d54f3 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java @@ -20,8 +20,8 @@ import java.util.List; import java.util.Map; /** - * Implements the {@link TaskList} interface for loading and writing {@link Task} objects - * from and to a JSON file. + * Implements the {@link TaskList} interface for reading and writing {@link Task} objects + * from and to a local JSON file. * The reads are cached to minimize file-io operations. */ public class JsonTaskList implements TaskList { @@ -45,9 +45,17 @@ public class JsonTaskList implements TaskList { timeModule.addSerializer(LocalDate.class, dateSerializer); } + /** + * Default constructor. Uses a file from the app resources. + */ public JsonTaskList() { this.dataSource = getClass().getResource("taskdb.json"); } + + /** + * Constructor to use a specified {@link URL} as a {@link #dataSource} + * @param dataSource A {@link URL} to the file to be used as a data source + */ public JsonTaskList(URL dataSource) { this.dataSource = dataSource; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantList.java index 8650429..f5c4dc9 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantList.java @@ -8,7 +8,7 @@ import java.util.List; import java.util.Optional; /** - * A database of {@link Plant}s. + * A List of {@link Plant}s. * The interface specifies the minimal required operations. */ public interface PlantList { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java index 95f5b07..60e745c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/TaskList.java @@ -7,8 +7,8 @@ import java.time.LocalDate; import java.util.List; /** - * A database of {@link Task}s. - * The interface specifies the minimal required operations. + * Represents a List of {@link Task}s. + * The interface specifies the operations to add/update and remove entries. */ public interface TaskList { /** diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java index 35956a1..9da78da 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/GrowthPhaseTypeDeserializer.java @@ -7,6 +7,9 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import java.io.IOException; +/** + * Used by the Jackson Library to deserialize a String to a {@link GrowthPhaseType} + */ public class GrowthPhaseTypeDeserializer extends StdDeserializer { public GrowthPhaseTypeDeserializer(Class vc) { super(vc); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java index 2a8ec1b..8170436 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/HardinessZoneDeserializer.java @@ -7,6 +7,9 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import java.io.IOException; +/** + * Used by the Jackson Library to deserialize a String to a {@link HardinessZone} + */ public class HardinessZoneDeserializer extends StdDeserializer { public HardinessZoneDeserializer(Class vc) { super(vc); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java index d40b882..29e055a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/json/PlantImageDeserializer.java @@ -13,6 +13,9 @@ import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; +/** + * Used by the Jackson Library to deserialize a String to a {@link Image} + */ public class PlantImageDeserializer extends JsonDeserializer { @Override From ec42a6e75ad85cca2cc24a1c7cd402fd154f3f3d Mon Sep 17 00:00:00 2001 From: giavaphi Date: Mon, 28 Nov 2022 12:38:29 +0100 Subject: [PATCH 2/2] fix nullpointer exception check date garden schedule --- .../gartenverwaltung/models/GardenSchedule.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java index a3eccfa..6f63f11 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/models/GardenSchedule.java @@ -147,12 +147,14 @@ public class GardenSchedule { final int finalI = i; weekTasks.forEach(task -> { LocalDate checkDate = task.getNextExecution(); - do { - if (date.equals(checkDate) && !date.isAfter(task.getEndDate().orElse(LocalDate.MIN))) { - dayTaskList.get(finalI).add(task); - } - checkDate = checkDate.plusDays(task.getInterval().orElse(0)); - } while (!(task.getInterval().orElse(0) == 0) && checkDate.isBefore(LocalDate.now().plusDays(listLength))); + if (checkDate != null) { + do { + if (date.equals(checkDate) && !date.isAfter(task.getEndDate().orElse(LocalDate.MIN))) { + dayTaskList.get(finalI).add(task); + } + checkDate = checkDate.plusDays(task.getInterval().orElse(0)); + } while (!(task.getInterval().orElse(0) == 0) && checkDate.isBefore(LocalDate.now().plusDays(listLength))); + } }); } return dayTaskList;