refactor: Made Task::id nullable/Optional for semantic reasons
better use of builder semantics in task and crop creation
This commit is contained in:
@@ -103,10 +103,9 @@ public class JsonTaskList implements TaskList {
|
||||
if(taskMap.isEmpty()) {
|
||||
loadTaskListFromFile();
|
||||
}
|
||||
if(task.getId() == 0) {
|
||||
task.withId(idProvider.incrementAndGet());
|
||||
}
|
||||
taskMap.put(task.getId(), task);
|
||||
long id = task.getId().orElse(idProvider.incrementAndGet());
|
||||
|
||||
taskMap.put(id, task.withId(id));
|
||||
writeTaskListToFile();
|
||||
}
|
||||
|
||||
@@ -122,8 +121,9 @@ public class JsonTaskList implements TaskList {
|
||||
if(taskMap.isEmpty()) {
|
||||
loadTaskListFromFile();
|
||||
}
|
||||
if(taskMap.containsKey(task.getId())){
|
||||
taskMap.remove(task.getId());
|
||||
Long taskId = task.getId().orElseThrow(IOException::new);
|
||||
if(taskMap.containsKey(taskId)){
|
||||
taskMap.remove(taskId);
|
||||
writeTaskListToFile();
|
||||
}
|
||||
}
|
||||
@@ -163,7 +163,7 @@ public class JsonTaskList implements TaskList {
|
||||
|
||||
taskMap = result.stream()
|
||||
.collect(HashMap::new,
|
||||
(res, task) -> res.put(task.getId(), task),
|
||||
(res, task) -> res.put(task.getId().orElse(0L), task),
|
||||
(existing, replacement) -> {});
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.Optional;
|
||||
* May be created using the builder pattern.
|
||||
*/
|
||||
public class Task {
|
||||
private long id;
|
||||
private Long id;
|
||||
private final String name;
|
||||
private final String description;
|
||||
private final LocalDate startDate;
|
||||
@@ -62,7 +62,7 @@ public class Task {
|
||||
}
|
||||
|
||||
// Getters
|
||||
public long getId() { return id; }
|
||||
public Optional<Long> getId() { return Optional.ofNullable(id); }
|
||||
public String getName() { return name; }
|
||||
public String getDescription() { return description; }
|
||||
public LocalDate getStartDate() { return startDate; }
|
||||
|
||||
@@ -34,6 +34,7 @@ public class TaskTemplate {
|
||||
public void setRelativeEndDate(Integer relativeEndDate) {
|
||||
this.relativeEndDate = relativeEndDate;
|
||||
}
|
||||
|
||||
public void setInterval(Integer interval) {
|
||||
this.interval = interval;
|
||||
}
|
||||
@@ -45,13 +46,10 @@ public class TaskTemplate {
|
||||
}
|
||||
|
||||
public Task generateTask(LocalDate realStartDate, long cropId) {
|
||||
Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId);
|
||||
if (relativeEndDate != null) {
|
||||
task.withEndDate(realStartDate.plusDays(relativeEndDate));
|
||||
}
|
||||
if (interval != null) {
|
||||
task.withInterval(interval);
|
||||
}
|
||||
return task;
|
||||
LocalDate endDate = relativeEndDate != null ? realStartDate.plusDays(relativeEndDate) : null;
|
||||
|
||||
return new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId)
|
||||
.withInterval(interval)
|
||||
.withEndDate(endDate);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user