forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from jiakai-17/jiakai/implement-storage-tasks
feat: Implement tasks storage
- Loading branch information
Showing
20 changed files
with
648 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/main/java/seedu/address/model/task/UniqueTaskList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package seedu.address.model.task; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import javafx.collections.FXCollections; | ||
import javafx.collections.ObservableList; | ||
import seedu.address.model.task.exceptions.DuplicateTaskException; | ||
import seedu.address.model.task.exceptions.TaskNotFoundException; | ||
|
||
/** | ||
* A list of tasks that enforces uniqueness between its elements and does not allow nulls. | ||
* A task is considered unique by comparing using {@code Task#isSameTask(Task)}. As such, adding and updating | ||
* of tasks uses Task#isSameTask(Task) for equality to ensure that the task being added or updated is | ||
* unique in terms of identity in the UniqueTaskList. However, the removal of a task uses Task#equals(Object) | ||
* to ensure that the task with exactly the same fields will be removed. | ||
* Supports a minimal set of list operations. | ||
* | ||
* @see Task#isSameTask(Task) | ||
*/ | ||
public class UniqueTaskList implements Iterable<Task> { | ||
|
||
private final ObservableList<Task> internalList = FXCollections.observableArrayList(); | ||
private final ObservableList<Task> internalUnmodifiableList = | ||
FXCollections.unmodifiableObservableList(internalList); | ||
|
||
/** | ||
* Returns true if the list contains an equivalent task as the given argument. | ||
*/ | ||
public boolean contains(Task toCheck) { | ||
requireNonNull(toCheck); | ||
return internalList.stream().anyMatch(toCheck::isSameTask); | ||
} | ||
|
||
/** | ||
* Adds a task to the list. | ||
* The task must not already exist in the list. | ||
*/ | ||
public void add(Task toAdd) { | ||
requireNonNull(toAdd); | ||
if (contains(toAdd)) { | ||
throw new DuplicateTaskException(); | ||
} | ||
internalList.add(toAdd); | ||
} | ||
|
||
/** | ||
* Replaces the task {@code target} in the list with {@code editedTask}. | ||
* {@code target} must exist in the list. | ||
* The task identity of {@code editedTask} must not be the same as another existing task in the list. | ||
*/ | ||
public void setTask(Task target, Task editedTask) { | ||
requireAllNonNull(target, editedTask); | ||
|
||
int index = internalList.indexOf(target); | ||
if (index == -1) { | ||
throw new TaskNotFoundException(); | ||
} | ||
|
||
if (!target.isSameTask(editedTask) && contains(editedTask)) { | ||
throw new DuplicateTaskException(); | ||
} | ||
|
||
internalList.set(index, editedTask); | ||
} | ||
|
||
/** | ||
* Removes the equivalent task from the list. | ||
* The task must exist in the list. | ||
*/ | ||
public void remove(Task toRemove) { | ||
requireNonNull(toRemove); | ||
if (!internalList.remove(toRemove)) { | ||
throw new TaskNotFoundException(); | ||
} | ||
} | ||
|
||
/** | ||
* Replaces the contents of this list with another {@code UniqueTaskList}. | ||
* @param replacement the {@code UniqueTaskList} to replace this list with. | ||
*/ | ||
public void setTasks(UniqueTaskList replacement) { | ||
requireNonNull(replacement); | ||
internalList.setAll(replacement.internalList); | ||
} | ||
|
||
/** | ||
* Replaces the contents of this list with {@code tasks}. | ||
* {@code tasks} must not contain duplicate tasks. | ||
*/ | ||
public void setTasks(List<Task> tasks) { | ||
requireAllNonNull(tasks); | ||
if (!tasksAreUnique(tasks)) { | ||
throw new DuplicateTaskException(); | ||
} | ||
|
||
internalList.setAll(tasks); | ||
} | ||
|
||
/** | ||
* Returns the backing list as an unmodifiable {@code ObservableList}. | ||
*/ | ||
public ObservableList<Task> asUnmodifiableObservableList() { | ||
return internalUnmodifiableList; | ||
} | ||
|
||
@Override | ||
public Iterator<Task> iterator() { | ||
return internalList.iterator(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof UniqueTaskList)) { | ||
return false; | ||
} | ||
|
||
UniqueTaskList otherUniqueTaskList = (UniqueTaskList) other; | ||
return internalList.equals(otherUniqueTaskList.internalList); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return internalList.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return internalList.toString(); | ||
} | ||
|
||
/** | ||
* Returns true if {@code tasks} contains only unique tasks. | ||
*/ | ||
private boolean tasksAreUnique(List<Task> tasks) { | ||
for (int i = 0; i < tasks.size() - 1; i++) { | ||
for (int j = i + 1; j < tasks.size(); j++) { | ||
if (tasks.get(i).isSameTask(tasks.get(j))) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package seedu.address.storage; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.model.task.Note; | ||
import seedu.address.model.task.Task; | ||
import seedu.address.model.task.Title; | ||
|
||
/** | ||
* Jackson-friendly version of {@link Task}. | ||
*/ | ||
class JsonAdaptedTask { | ||
|
||
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Task's %s field is missing!"; | ||
|
||
private final String title; | ||
private final String note; | ||
|
||
/** | ||
* Constructs a {@code JsonAdaptedTask} with the given task details. | ||
*/ | ||
@JsonCreator | ||
public JsonAdaptedTask(@JsonProperty("title") String title, @JsonProperty("note") String note) { | ||
this.title = title; | ||
this.note = note; | ||
} | ||
|
||
/** | ||
* Converts a given {@code Task} into this class for Jackson use. | ||
*/ | ||
public JsonAdaptedTask(Task source) { | ||
title = source.getTitle().value; | ||
note = source.getNote().value; | ||
} | ||
|
||
/** | ||
* Converts this Jackson-friendly adapted task object into the model's {@code Task} object. | ||
* | ||
* @throws IllegalValueException if there were any data constraints violated in the adapted task. | ||
*/ | ||
public Task toModelType() throws IllegalValueException { | ||
if (title == null) { | ||
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Title.class.getSimpleName())); | ||
} | ||
if (!Title.isValidTitle(title)) { | ||
throw new IllegalValueException(Title.MESSAGE_CONSTRAINTS); | ||
} | ||
final Title modelTitle = new Title(title); | ||
|
||
if (note == null) { | ||
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Note.class.getSimpleName())); | ||
} | ||
if (!Note.isValidNote(note)) { | ||
throw new IllegalValueException(Note.MESSAGE_CONSTRAINTS); | ||
} | ||
final Note modelNote = new Note(note); | ||
|
||
return new Task(modelTitle, modelNote); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
"phone": "948asdf2424", | ||
"email": "[email protected]", | ||
"address": "4th street" | ||
} ] | ||
} ], | ||
"tasks": [] | ||
} |
Oops, something went wrong.