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
Refactor undo #130
Refactor undo #130
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! You might want to consider some of the minor readability changes I suggested. Otherwise, LGTM! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to merge!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, just some feedback on the undo functionality.
|
||
|
||
@Override | ||
public CommandResult execute(ItemModel model) { | ||
requireNonNull(model); | ||
beforeClear = model.getItemStorage().deepCopy(); | ||
System.out.println(beforeClear.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line for debugging purposes? Perhaps we should remove it?
|
||
@Override | ||
public void reverse(ItemModel model) throws CommandException { | ||
System.out.println(beforeClear.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for this line too!
@@ -16,4 +17,7 @@ public CommandResult execute(ItemModel model) { | |||
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true); | |||
} | |||
|
|||
@Override | |||
public void reverse(ItemModel model) throws CommandException { } | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps include a comment here to explain why this is an empty code block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think that this might not be a good practice cause we should not have a method block just for the sake of having one. Perhaps you can consider creating an abstract class called UndoableCommand from which all the command that have reverse extends from while for the rest you can just extend from command. This will also make it easier to catch for error if a command cannot be undo.
model.findItem(searchString); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_ITEM_LISTED_OVERVIEW, model.getVisualList().size())); | ||
} | ||
|
||
@Override | ||
public void reverse(ItemModel model) throws CommandException { | ||
model.setVisualizeList(beforeFilter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
} | ||
|
||
@Override | ||
public CommandResult execute(ItemModel model) throws CommandException { | ||
if (elisaStateHistory.size() <= 1) { | ||
if (elisaCommandHistory.size() <= 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case it is fine to use ==
instead as there is no way that the size will be less than 0?
return new CommandResult("Undo successful!", true); | ||
Command lastDone = elisaCommandHistory.popCommand(); | ||
lastDone.reverse(model); | ||
return new CommandResult("Undo successful!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you are able to undo the command, maybe you can consider adding the command that you just undo so that the user is able to tell what was the command that was undone. I believe that it can be done by Command.COMMAND_WORD
to get the command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Command class doesn't have the COMMAND_WORD, I will create a getter for it
* */ | ||
@Override | ||
public void pushCommand(Command command) { | ||
if (!(command instanceof UndoCommand || command instanceof ExitCommand)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one case where having a separate abstract class called UndoableCommand might be good.
if (undoStack.size() > 0) { | ||
return undoStack.pop(); | ||
} else { | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps consider throwing the exception that there is no command in the undo stack from here so that there is no need to do 2 checks within the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually I don't need to check here since the only usage of pop is inside UndoCommand where there is the check for empty stack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some small comments that can be changed in the next PR. Good to go!
@@ -17,6 +17,6 @@ | |||
*/ | |||
public abstract CommandResult execute(ItemModel model) throws CommandException; | |||
|
|||
public abstract void reverse(ItemModel model) throws CommandException; | |||
public abstract String getCommandWord(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should have been implemented in UndoableCommand but just change it in the next iteration.
lastDone.reverse(model); | ||
return new CommandResult("Undo successful!"); | ||
return new CommandResult("Undo [" + lastDone.getCommandWord() + "] command successful!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can attempt using String.format()
next time to make it less messy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Fixes #133