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

Commit

Permalink
Merge 3829ab6 into 1bee16f
Browse files Browse the repository at this point in the history
  • Loading branch information
yamidark committed Jan 16, 2018
2 parents 1bee16f + 3829ab6 commit 77246d9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/main/java/seedu/address/logic/commands/UndoableCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.function.Predicate;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.ReadOnlyPerson;

/**
* Represents a command which can be undone and redone.
*/
public abstract class UndoableCommand extends Command {
private ReadOnlyAddressBook previousAddressBook;
private Predicate<ReadOnlyPerson> previousPredicate;

protected abstract CommandResult executeUndoableCommand() throws CommandException;

Expand All @@ -24,6 +28,26 @@ private void saveAddressBookSnapshot() {
this.previousAddressBook = new AddressBook(model.getAddressBook());
}

/**
* Stores the predicate used in {@code model#filteredPersons}.
*/
private void savePredicateSnapshot() {
previousPredicate = model.getFilteredPersonListPredicate();
}

/**
* Returns the previousPredicate stored.
* Returns a default predicate to show all persons
* if previousPredicate is null.
*/
private Predicate<ReadOnlyPerson> getPreviousPredicate() {
if (previousPredicate == null) {
return PREDICATE_SHOW_ALL_PERSONS;
} else {
return previousPredicate;
}
}

/**
* Reverts the AddressBook to the state before this command
* was executed and updates the filtered person list to
Expand All @@ -36,12 +60,15 @@ protected final void undo() {
}

/**
* Executes the command and updates the filtered person
* list to show all persons.
* Reverts the filtered person list to its previous view
* before executing the command.
* Updates the filtered person list to show all persons
* after execution completes.
*/
protected final void redo() {
requireNonNull(model);
try {
model.updateFilteredPersonList(getPreviousPredicate());
executeUndoableCommand();
} catch (CommandException ce) {
throw new AssertionError("The command has been successfully executed previously; "
Expand All @@ -53,6 +80,7 @@ protected final void redo() {
@Override
public final CommandResult execute() throws CommandException {
saveAddressBookSnapshot();
savePredicateSnapshot();
return executeUndoableCommand();
}
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void updatePerson(ReadOnlyPerson target, ReadOnlyPerson editedPerson)
/** Returns an unmodifiable view of the filtered person list */
ObservableList<ReadOnlyPerson> getFilteredPersonList();

/** Returns the predicate used in the filtered person list */
Predicate<ReadOnlyPerson> getFilteredPersonListPredicate();

/**
* Updates the filter of the filtered person list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public ObservableList<ReadOnlyPerson> getFilteredPersonList() {
return FXCollections.unmodifiableObservableList(filteredPersons);
}

@Override
@SuppressWarnings("unchecked")
public Predicate<ReadOnlyPerson> getFilteredPersonListPredicate() {
return (Predicate<ReadOnlyPerson>) filteredPersons.getPredicate();
}

@Override
public void updateFilteredPersonList(Predicate<ReadOnlyPerson> predicate) {
requireNonNull(predicate);
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public ObservableList<ReadOnlyPerson> getFilteredPersonList() {
return null;
}

@Override
public Predicate<ReadOnlyPerson> getFilteredPersonListPredicate() {
fail("This method should not be called.");
return null;
}

@Override
public void updateFilteredPersonList(Predicate<ReadOnlyPerson> predicate) {
fail("This method should not be called.");
Expand All @@ -144,10 +150,17 @@ public void addPerson(ReadOnlyPerson person) throws DuplicatePersonException {
throw new DuplicatePersonException();
}

/* ---------------These methods below are called prior to calling addPerson(ReadOnlyPerson)--------- */

@Override
public ReadOnlyAddressBook getAddressBook() {
return new AddressBook();
}

@Override
public Predicate<ReadOnlyPerson> getFilteredPersonListPredicate() {
return PREDICATE_SHOW_ALL_PERSONS;
}
}

/**
Expand All @@ -161,10 +174,17 @@ public void addPerson(ReadOnlyPerson person) throws DuplicatePersonException {
personsAdded.add(new Person(person));
}

/* ---------------These methods below are called prior to calling addPerson(ReadOnlyPerson)--------- */

@Override
public ReadOnlyAddressBook getAddressBook() {
return new AddressBook();
}

@Override
public Predicate<ReadOnlyPerson> getFilteredPersonListPredicate() {
return PREDICATE_SHOW_ALL_PERSONS;
}
}

}
12 changes: 12 additions & 0 deletions src/test/java/systemtests/DeleteCommandSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@ public void delete() {
/* ------------------ Performing delete operation while a filtered list is being shown ---------------------- */

/* Case: filtered person list, delete index within bounds of address book and person list -> deleted */
Model modelBeforeDeletingFirstFiltered = getModel();
showPersonsWithName(KEYWORD_MATCHING_MEIER);
Index index = INDEX_FIRST_PERSON;
assertTrue(index.getZeroBased() < getModel().getFilteredPersonList().size());
assertCommandSuccess(index);

/* Case: undo deleting the first person in the filtered list -> first person in filtered list restored */
command = UndoCommand.COMMAND_WORD;
expectedResultMessage = UndoCommand.MESSAGE_SUCCESS;
assertCommandSuccess(command, modelBeforeDeletingFirstFiltered, expectedResultMessage);

/* Case: redo deleting the first person in the filtered list -> first person in filtered list deleted again */
command = RedoCommand.COMMAND_WORD;
removePerson(modelBeforeDeletingFirstFiltered, index);
expectedResultMessage = RedoCommand.MESSAGE_SUCCESS;
assertCommandSuccess(command, modelBeforeDeletingFirstFiltered, expectedResultMessage);

/* Case: filtered person list, delete index within bounds of address book but out of bounds of person list
* -> rejected
*/
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/systemtests/EditCommandSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,25 @@ public void edit() throws Exception {

/* Case: filtered person list, edit index within bounds of address book and person list -> edited */
showPersonsWithName(KEYWORD_MATCHING_MEIER);
model = getModel();
index = INDEX_FIRST_PERSON;
assertTrue(index.getZeroBased() < getModel().getFilteredPersonList().size());
command = EditCommand.COMMAND_WORD + " " + index.getOneBased() + " " + NAME_DESC_BOB;
personToEdit = getModel().getFilteredPersonList().get(index.getZeroBased());
editedPerson = new PersonBuilder(personToEdit).withName(VALID_NAME_BOB).build();
assertCommandSuccess(command, index, editedPerson);

/* Case: undo editing the first person in the filtered list -> first person in filtered list restored */
command = UndoCommand.COMMAND_WORD;
expectedResultMessage = UndoCommand.MESSAGE_SUCCESS;
assertCommandSuccess(command, model, expectedResultMessage);

/* Case: redo editing the first person in the filtered list -> first person in filtered list edited again */
command = RedoCommand.COMMAND_WORD;
expectedResultMessage = RedoCommand.MESSAGE_SUCCESS;
model.updatePerson(personToEdit, editedPerson);
assertCommandSuccess(command, model, expectedResultMessage);

/* Case: filtered person list, edit index within bounds of address book but out of bounds of person list
* -> rejected
*/
Expand Down

0 comments on commit 77246d9

Please sign in to comment.