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

Poor implementation of Undo Memento Pattern. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion src/seedu/addressbook/commands/CommandResult.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.ReadOnlyPerson;

import java.util.List;
Expand All @@ -15,15 +16,28 @@ public class CommandResult {

/** The list of persons that was produced by the command */
private final List<? extends ReadOnlyPerson> relevantPersons;

private final AddressBook state;
private boolean isUndone;

public CommandResult(String feedbackToUser) {
this.feedbackToUser = feedbackToUser;
relevantPersons = null;
state = null;
}

public CommandResult(String feedbackToUser, List<? extends ReadOnlyPerson> relevantPersons) {
this.feedbackToUser = feedbackToUser;
this.relevantPersons = relevantPersons;
state = null;
}

public CommandResult(String feedbackToUser, AddressBook state, boolean undoneStatus) {
this.feedbackToUser = feedbackToUser;
this.relevantPersons = null;
this.state = state;
isUndone = undoneStatus;
System.out.println("isUndone " + isUndone);
}

/**
Expand All @@ -32,5 +46,15 @@ public CommandResult(String feedbackToUser, List<? extends ReadOnlyPerson> relev
public Optional<List<? extends ReadOnlyPerson>> getRelevantPersons() {
return Optional.ofNullable(relevantPersons);
}


/**
* Returns previous state of addressBook to command command result, if succeeded
*/
public AddressBook getRevertState() {
return state;
}

public boolean getUndoStatus() {
return isUndone;
}
}
56 changes: 56 additions & 0 deletions src/seedu/addressbook/commands/UndoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.data.tag.UniqueTagList.DuplicateTagException;
import seedu.addressbook.logic.Logic;
import seedu.addressbook.revision_control.CareTaker;
import seedu.addressbook.revision_control.Originator;

/**
* Undo most recently successful command which mutates the data of AddressBook.
*/
public class UndoCommand extends Command {

public static final String COMMAND_WORD = "undo";

public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n"
+ "Undo your most recent command which mutates the data such as add,delete,edit,clear" + "Example: "
+ COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "Your AddressBook action has been reverted successfully";

private Originator originator;
private CareTaker careTaker;


/**
* To request logic to give additional input for executing Undo.
*/
public UndoCommand(Logic logic) {

}

public UndoCommand(Originator originator, CareTaker careTaker, int numCmd, int numUndo) {
this.originator = originator;
this.careTaker = careTaker;

}

@Override
public CommandResult execute() {

/*
* new AddressBook(new UniquePersonList(personList), new UniqueTagList(tagList))
*
*/
originator.getStateFromMemento(careTaker.get(0)); // does not work when ported here
return new CommandResult(String.format(MESSAGE_SUCCESS),
new AddressBook(new UniquePersonList( originator.getState().getAllPersons()),
new UniqueTagList(originator.getState().getAllTags())),// does not work.
true);
}

}
66 changes: 65 additions & 1 deletion src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.CommandResult;
import seedu.addressbook.commands.UndoCommand;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.parser.Parser;
import seedu.addressbook.revision_control.CareTaker;
import seedu.addressbook.revision_control.Originator;
import seedu.addressbook.storage.StorageFile;
import seedu.addressbook.storage.jaxb.AdaptedAddressBook;

import java.util.Collections;
import java.util.List;
Expand All @@ -19,26 +25,45 @@ public class Logic {

private StorageFile storage;
private AddressBook addressBook;
private Originator originator;
private CareTaker careTaker;
private int numCmd = 0;
private int numUndo = 0;

/** The list of person shown to the user most recently. */
private List<? extends ReadOnlyPerson> lastShownList = Collections.emptyList();

public Logic() throws Exception{
setStorage(initializeStorage());
setAddressBook(storage.load());
originator = new Originator();
setCareTaker();

}

Logic(StorageFile storageFile, AddressBook addressBook){
setStorage(storageFile);
setAddressBook(addressBook);
}
originator = new Originator();
setCareTaker();
}

void setStorage(StorageFile storage){
this.storage = storage;
}

void setAddressBook(AddressBook addressBook){
this.addressBook = addressBook;
}

/**
* Sets the careTaker to be the initial state upon starting the program.
* CareTaker and originator will reset on every startup.
*/
void setCareTaker() {
careTaker = new CareTaker();
originator.setState(addressBook);
careTaker.add(originator.saveStateToMemento());
}

/**
Expand All @@ -52,6 +77,14 @@ private StorageFile initializeStorage() throws StorageFile.InvalidStorageFilePat
public String getStorageFilePath() {
return storage.getPath();
}

public Originator getOriginator() {
return originator;
}

public CareTaker getCareTaker() {
return careTaker;
}

/**
* Unmodifiable view of the current last shown list.
Expand All @@ -70,6 +103,9 @@ protected void setLastShownList(List<? extends ReadOnlyPerson> newList) {
*/
public CommandResult execute(String userCommandText) throws Exception {
Command command = new Parser().parseCommand(userCommandText);
if (isUndo(command)) {
command = new UndoCommand(originator, careTaker, numCmd, numUndo);
}
CommandResult result = execute(command);
recordResult(result);
return result;
Expand All @@ -84,8 +120,32 @@ public CommandResult execute(String userCommandText) throws Exception {
*/
private CommandResult execute(Command command) throws Exception {
command.setData(addressBook, lastShownList);
numCmd++;
CommandResult result = command.execute();
//TODO: save only if execute successfully mutates the data.
storage.save(addressBook);
if (!result.getUndoStatus()) {
originator.setState(addressBook);
System.out.println(originator.getState() + " 1");
careTaker.add(originator.saveStateToMemento());
System.out.println(result.getUndoStatus());
}
else {
// logic issues with num counter
numUndo++;
System.out.println(careTaker.mementoList.size());
System.out.println(originator.getState() + " 2");

/*
originator.getStateFromMemento(careTaker.get(0));
addressBook = new AddressBook(new UniquePersonList( originator.getState().getAllPersons()),
new UniqueTagList(originator.getState().getAllTags()));// works for delete, To convert to command
*/

//AddressBook(UniquePersonList persons, UniqueTagList tags)
}


return result;
}

Expand All @@ -96,4 +156,8 @@ private void recordResult(CommandResult result) {
lastShownList = personList.get();
}
}

private boolean isUndo(Command command) {
return (command.getClass().equals( new UndoCommand(null).getClass()) ? true : false);
}
}
5 changes: 3 additions & 2 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import seedu.addressbook.commands.*;
import seedu.addressbook.data.exception.IllegalValueException;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -93,6 +92,9 @@ public Command parseCommand(String userInput) {

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case UndoCommand.COMMAND_WORD:
return new UndoCommand(null);

case HelpCommand.COMMAND_WORD: // Fallthrough
default:
Expand Down Expand Up @@ -276,5 +278,4 @@ private Command prepareFind(String args) {
return new FindCommand(keywordSet);
}


}
20 changes: 20 additions & 0 deletions src/seedu/addressbook/revision_control/CareTaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package seedu.addressbook.revision_control;

import java.util.ArrayList;

public class CareTaker {

public ArrayList<Memento> mementoList;

public CareTaker() {
mementoList = new ArrayList<Memento>();
}

public void add(Memento state) {
mementoList.add(state);
}

public Memento get(int index) {
return mementoList.get(index);
}
}
18 changes: 18 additions & 0 deletions src/seedu/addressbook/revision_control/Memento.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package seedu.addressbook.revision_control;

import seedu.addressbook.data.AddressBook;

public class Memento {

private AddressBook state;


public Memento(AddressBook state) {
this.state = state;
}

public AddressBook getState() {
return state;
}

}
25 changes: 25 additions & 0 deletions src/seedu/addressbook/revision_control/Originator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.addressbook.revision_control;

import seedu.addressbook.data.AddressBook;

public class Originator {

private AddressBook state;

public void setState(AddressBook state) {
this.state = state;
}

public AddressBook getState() {
return state;
}

public Memento saveStateToMemento() {
return new Memento(state);
}

public void getStateFromMemento(Memento Memento) {
state = Memento.getState();
}

}