Skip to content
This repository has been archived by the owner on Mar 2, 2019. It is now read-only.

Commit

Permalink
Merge branch 'todo-model'
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangYiJiang committed Oct 4, 2016
2 parents 06c8b65 + 44904f8 commit 8100708
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/seedu/todo/model/ReadOnlyTodoList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package seedu.todo.model;

import java.util.List;

import seedu.todo.model.task.ReadOnlyTask;

public interface ReadOnlyTodoList {
/**
* Get an immutable list of tasks
*/
public List<ReadOnlyTask> getTasks();
}
69 changes: 69 additions & 0 deletions src/main/java/seedu/todo/model/TodoList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package seedu.todo.model;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;

import seedu.todo.commons.core.UnmodifiableObservableList;
import seedu.todo.commons.exceptions.IllegalValueException;
import seedu.todo.model.task.ReadOnlyTask;
import seedu.todo.model.task.Task;

public class TodoList implements ReadOnlyTodoList, TodoModel {
private ObservableList<Task> tasks;
private FilteredList<Task> filteredTasks;
private SortedList<Task> sortedTasks;
private SortedList<Task> pinnedTasks;

public TodoList() {
tasks = FXCollections.observableArrayList();
filteredTasks = new FilteredList<>(tasks, p -> true);
sortedTasks = new SortedList<>(filteredTasks);
pinnedTasks = new SortedList<>(sortedTasks, (a, b) -> Boolean.compare(a.isPinned(), b.isPinned()));
}

@Override
public void add(ReadOnlyTask task) {
tasks.add(Task.unwrap(task));
}

@Override
public void delete(ReadOnlyTask task) throws IllegalValueException {
if (!tasks.remove(task)) {
throw new IllegalValueException("Task not found in todo list");
}
}

@Override
public void update(ReadOnlyTask task) throws IllegalValueException {
int index = tasks.indexOf(task);

if (index == -1) {
throw new IllegalValueException("Task not found in todo list");
} else {
tasks.set(index, Task.unwrap(task));
}
}

@Override
public void view(Predicate<ReadOnlyTask> filter, Comparator<ReadOnlyTask> comparator) {
filteredTasks.setPredicate(filter);
sortedTasks.setComparator(comparator);
}

@Override
public UnmodifiableObservableList<ReadOnlyTask> getObserveableList() {
return new UnmodifiableObservableList<>(pinnedTasks);
}

@Override
public List<ReadOnlyTask> getTasks() {
return Collections.unmodifiableList(tasks);
}
}
37 changes: 37 additions & 0 deletions src/main/java/seedu/todo/model/TodoModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package seedu.todo.model;

import java.util.Comparator;
import java.util.function.Predicate;

import seedu.todo.commons.core.UnmodifiableObservableList;
import seedu.todo.commons.exceptions.IllegalValueException;
import seedu.todo.model.task.ReadOnlyTask;

public interface TodoModel {
/**
* Adds a new task to the todo list
*/
public void add(ReadOnlyTask task);

/**
* Deletes the given task
* @throws IllegalValueException if the task does not exist
*/
public void delete(ReadOnlyTask task) throws IllegalValueException;

/**
* Replace the provided task with the given task
* @throws IllegalValueException if the task does not exist
*/
public void update(ReadOnlyTask task) throws IllegalValueException;

/**
* Update the filter predicate and sort comparator used to display the tasks
*/
public void view(Predicate<ReadOnlyTask> filter, Comparator<ReadOnlyTask> comparator);

/**
* Get an unmodifiable observable list of tasks. Used mainly by the UI.
*/
public UnmodifiableObservableList<ReadOnlyTask> getObserveableList();
}
33 changes: 33 additions & 0 deletions src/main/java/seedu/todo/model/task/ReadOnlyTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.todo.model.task;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;

import com.google.common.collect.ImmutableSet;

import seedu.todo.model.tag.Tag;

public interface ReadOnlyTask {
public String getTitle();

public Optional<String> getDescription();

public Optional<String> getLocation();

public Optional<LocalDateTime> getStartTime();

public Optional<LocalDateTime> getEndTime();

public boolean isPinned();

public boolean isCompleted();

default public boolean isEvent() {
return this.getStartTime().isPresent();
}

public ImmutableSet<Tag> getTags();

public UUID getUUID();
}
147 changes: 147 additions & 0 deletions src/main/java/seedu/todo/model/task/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package seedu.todo.model.task;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import com.google.common.collect.ImmutableSet;

import seedu.todo.model.tag.Tag;

public class Task implements ReadOnlyTask {
private String title;
private String description;
private String location;
private boolean pinned;
private boolean completed;
private LocalDateTime startTime;
private LocalDateTime endTime;
private Set<Tag> tags = new HashSet<>();
private UUID uuid;

/**
* Creates a new task
*/
public Task(String title) {
this.title = title;
this.uuid = UUID.randomUUID();
}

/**
* Constructs a Task from a ReadOnlyTask
*/
public Task(ReadOnlyTask task) {
this.title = task.getTitle();
this.description = task.getDescription().orElse(null);
this.location = task.getLocation().orElse(null);
this.startTime = task.getStartTime().orElse(null);
this.endTime = task.getEndTime().orElse(null);
this.completed = task.isCompleted();
this.pinned = task.isPinned();
this.uuid = task.getUUID();
}

/**
* Converts the provided ReadOnlyTask into a Task
*/
public static Task unwrap(ReadOnlyTask task) {
if (task instanceof Task) {
return (Task) task;
}

return new Task(task);
}

@Override
public String getTitle() {
return title;
}

@Override
public Optional<String> getDescription() {
return Optional.of(description);
}

@Override
public Optional<String> getLocation() {
return Optional.of(location);
}

@Override
public Optional<LocalDateTime> getStartTime() {
return Optional.of(startTime);
}

@Override
public Optional<LocalDateTime> getEndTime() {
return Optional.of(endTime);
}

@Override
public boolean isPinned() {
return pinned;
}

@Override
public boolean isCompleted() {
return completed;
}

@Override
public ImmutableSet<Tag> getTags() {
return (ImmutableSet<Tag>) tags;
}

public void setPinned(boolean pinned) {
this.pinned = pinned;
}

public void setCompleted(boolean completed) {
this.completed = completed;
}

public void setDescription(String description) {
this.description = description;
}

public void setLocation(String location) {
this.location = location;
}

public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}

public void setEndTime(LocalDateTime endTime) {
this.endTime = endTime;
}

public void setTags(Set<Tag> tags) {
this.tags = tags;
}

@Override
public UUID getUUID() {
return uuid;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof ReadOnlyTask)) {
return false;
}

return uuid.equals(((ReadOnlyTask) o).getUUID());
}

@Override
public int hashCode() {
return uuid.hashCode();
}
}

0 comments on commit 8100708

Please sign in to comment.