Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Refactor of Tags #7

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/main/java/tagline/logic/commands/note/DeleteNoteCommand.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package tagline.logic.commands.note;

import static java.util.Objects.requireNonNull;
import static tagline.model.note.NoteModel.PREDICATE_SHOW_ALL_NOTES;

import java.util.List;
import java.util.Optional;

import tagline.commons.core.Messages;
import tagline.logic.commands.CommandResult;
import tagline.logic.commands.exceptions.CommandException;
import tagline.model.Model;
import tagline.model.note.Note;
import tagline.model.note.NoteId;
import tagline.model.note.NoteIdEqualsTargetIdPredicate;

/**
* Deletes a note identified using it's index.
Expand All @@ -37,18 +35,16 @@ public DeleteNoteCommand(NoteId noteId) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
NoteIdEqualsTargetIdPredicate predicate = new NoteIdEqualsTargetIdPredicate(targetId);
model.updateFilteredNoteList(predicate);
List<Note> filteredList = model.getFilteredNoteList();

if (filteredList.size() < 1) {
Optional<Note> noteFound = model.findNote(targetId);

if (noteFound.isEmpty()) {
throw new CommandException(Messages.MESSAGE_INVALID_NOTE_INDEX);
}

Note noteToDelete = filteredList.get(0);
Note noteToDelete = noteFound.get();

model.deleteNote(noteToDelete);
model.updateFilteredNoteList(PREDICATE_SHOW_ALL_NOTES);
return new CommandResult(String.format(MESSAGE_SUCCESS, noteToDelete), CommandResult.ViewType.NOTE);
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tagline/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tagline.model.contact.ContactId;
import tagline.model.contact.ReadOnlyAddressBook;
import tagline.model.note.Note;
import tagline.model.note.NoteId;
import tagline.model.note.ReadOnlyNoteBook;

/**
Expand Down Expand Up @@ -142,6 +143,12 @@ public interface Model {
*/
void deleteNote(Note target);

/**
* Finds a {@code Note} in the note book based on the {@code noteId}.
* @return Optional object if corresponding note is found, empty otherwise
*/
public Optional<Note> findNote(NoteId noteId);

/**
* Returns an unmodifiable view of the filtered note list
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/tagline/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tagline.model.contact.ReadOnlyAddressBook;
import tagline.model.note.Note;
import tagline.model.note.NoteBook;
import tagline.model.note.NoteId;
import tagline.model.note.NoteManager;
import tagline.model.note.ReadOnlyNoteBook;

Expand Down Expand Up @@ -190,6 +191,11 @@ public void deleteNote(Note target) {
noteManager.deleteNote(target);
}

@Override
public Optional<Note> findNote(NoteId noteId) {
return noteManager.findNote(noteId);
}

//=========== Filtered Note List Accessors =============================================================

/**
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/tagline/model/contact/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void resetData(ReadOnlyAddressBook newData) {
*/
public boolean hasContact(Contact contact) {
requireNonNull(contact);
return contacts.contains(contact);
return contacts.containsContact(contact);
}

/**
Expand All @@ -73,7 +73,7 @@ public boolean hasContact(Contact contact) {
public void addContact(Contact p) {
requireNonNull(p.getContactId());
assert (findContact(p.getContactId()).isEmpty()) : "Contact id is not unique";
contacts.add(p);
contacts.addContact(p);
}

/**
Expand All @@ -94,12 +94,16 @@ public void setContact(Contact target, Contact editedContact) {
* {@code key} must exist in the address book.
*/
public void removeContact(Contact key) {
contacts.remove(key);
contacts.removeContact(key);
}

/**
* Finds a contact with ID equal to {@code contactId}.
*/
public Optional<Contact> findContact(ContactId contactId) {
return contacts.findContact(contactId);
}

//// util methods

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/tagline/model/contact/UniqueContactList.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class UniqueContactList implements Iterable<Contact> {
/**
* Returns true if the list contains an equivalent contact as the given argument.
*/
public boolean contains(Contact toCheck) {
public boolean containsContact(Contact toCheck) {
requireNonNull(toCheck);
return internalList.stream().anyMatch(toCheck::isSameContact);
}
Expand Down Expand Up @@ -64,9 +64,9 @@ public int size() {
* Adds a contact to the list.
* The contact must not already exist in the list.
*/
public void add(Contact toAdd) {
public void addContact(Contact toAdd) {
requireNonNull(toAdd);
if (contains(toAdd)) {
if (containsContact(toAdd)) {
throw new DuplicateContactException();
}
internalList.add(toAdd);
Expand All @@ -85,7 +85,7 @@ public void setContact(Contact target, Contact editedContact) {
throw new ContactNotFoundException();
}

if (!target.isSameContact(editedContact) && contains(editedContact)) {
if (!target.isSameContact(editedContact) && containsContact(editedContact)) {
throw new DuplicateContactException();
}

Expand All @@ -96,7 +96,7 @@ public void setContact(Contact target, Contact editedContact) {
* Removes the equivalent contact from the list.
* The contact must exist in the list.
*/
public void remove(Contact toRemove) {
public void removeContact(Contact toRemove) {
requireNonNull(toRemove);
if (!internalList.remove(toRemove)) {
throw new ContactNotFoundException();
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/tagline/model/note/NoteBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.Optional;

import javafx.collections.ObservableList;

/**
* Wraps all data at the address-book level
* Wraps all data at the note book level
* Duplicates are not allowed (by .isSameNote comparison)
*/
public class NoteBook implements ReadOnlyNoteBook {
Expand Down Expand Up @@ -61,15 +62,23 @@ public void resetData(ReadOnlyNoteBook newData) {
*/
public boolean hasNote(Note note) {
requireNonNull(note);
return notes.contains(note);
return notes.containsNote(note);
}

/**
* Adds a note to the address book.
* The note must not already exist in the address book.
*/
public void addNote(Note p) {
notes.add(p);
notes.addNote(p);
}

/**
* Finds a {@code Note} based on the {@code noteId}.
* @return Optional object if corresponding note is found, empty otherwise
*/
public Optional<Note> findNote(NoteId noteId) {
return notes.findNote(noteId);
}

/**
Expand All @@ -88,7 +97,7 @@ public void setNote(Note target, Note editedNote) {
* {@code key} must exist in the address book.
*/
public void removeNote(Note key) {
notes.remove(key);
notes.removeNote(key);
}

//// util methods
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/tagline/model/note/NoteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static tagline.commons.util.CollectionUtil.requireAllNonNull;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;

Expand Down Expand Up @@ -121,6 +122,11 @@ public void setNote(Note target, Note editedNote) {
noteBook.setNote(target, editedNote);
}

@Override
public Optional<Note> findNote(NoteId noteId) {
return noteBook.findNote(noteId);
}

//=========== Filtered Note List Accessors =============================================================

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tagline/model/note/NoteModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tagline.model.note;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;

import javafx.collections.ObservableList;
Expand Down Expand Up @@ -76,6 +77,12 @@ public interface NoteModel {
*/
void setNote(Note target, Note editedNote);

/**
* Finds a {@code Note} based on the {@code noteId}.
* @return Optional object if corresponding note is found, empty otherwise
*/
public Optional<Note> findNote(NoteId noteId);

/** Returns an unmodifiable view of the filtered note list */
ObservableList<Note> getFilteredNoteList();

Expand Down
28 changes: 23 additions & 5 deletions src/main/java/tagline/model/note/UniqueNoteList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand All @@ -31,7 +32,7 @@ public class UniqueNoteList implements Iterable<Note> {
/**
* Returns true if the list contains an equivalent note as the given argument.
*/
public boolean contains(Note toCheck) {
public boolean containsNote(Note toCheck) {
requireNonNull(toCheck);
return internalList.stream().anyMatch(toCheck::isSameNote);
}
Expand All @@ -40,14 +41,31 @@ public boolean contains(Note toCheck) {
* Adds a note to the list.
* The note must not already exist in the list.
*/
public void add(Note toAdd) {
public void addNote(Note toAdd) {
requireNonNull(toAdd);
if (contains(toAdd)) {
if (containsNote(toAdd)) {
throw new DuplicateNoteException();
}
internalList.add(toAdd);
}

/**
* Find a note by id.
*
* @param id of the note
* @return an optional object which implies whether the corresponding note is found or not.
*/
public Optional<Note> findNote(NoteId id) {
var it = iterator();
while (it.hasNext()) {
Note currentNote = it.next();
if (currentNote.getNoteId().equals(id)) {
return Optional.of(currentNote);
}
}
return Optional.empty();
}

/**
* Replaces the note {@code target} in the list with {@code editedNote}.
* {@code target} must exist in the list.
Expand All @@ -61,7 +79,7 @@ public void setNote(Note target, Note editedNote) {
throw new NoteNotFoundException();
}

if (!target.isSameNote(editedNote) && contains(editedNote)) {
if (!target.isSameNote(editedNote) && containsNote(editedNote)) {
throw new DuplicateNoteException();
}

Expand All @@ -72,7 +90,7 @@ public void setNote(Note target, Note editedNote) {
* Removes the equivalent note from the list.
* The note must exist in the list.
*/
public void remove(Note toRemove) {
public void removeNote(Note toRemove) {
requireNonNull(toRemove);
if (!internalList.remove(toRemove)) {
throw new NoteNotFoundException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Unmodifiable view of a tag list.
*/
public interface ReadOnlyTagList {
public interface ReadOnlyTagBook {

/**
* Returns an unmodifiable view of the tag list.
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/tagline/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public enum TagType {
GROUP_TAG,
}

private static int nextId = 1; //temporary implementation of an incrementing tag ID

public final TagId tagId;
public final TagType tagType;

Expand All @@ -42,11 +40,26 @@ public Tag(TagId tagId, TagType tagType) {
this.tagType = tagType;
}

/**
* Returns true if {@code other} has the same data and ID as this object.
* This defines a stronger notion of equality between two tags.
*/
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Tag // instanceof handles nulls
&& tagType.equals(((Tag) other).tagType)); // state check
&& tagType.equals(((Tag) other).tagType) // state check
&& tagId.equals(((Tag) other).tagId));
}

/**
* Returns true if {@code other} has the same data as this object.
* This defines a weaker notion of equality between two tags.
*/
public boolean isSameContent(Object other) {
return other == this // short circuit if same object
|| (other instanceof Tag // instanceof handles nulls
&& tagType.equals(((Tag) other).tagType));
}

@Override
Expand Down
Loading