Skip to content

Commit

Permalink
Add logging for undo/redo commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jiewei98 committed Apr 10, 2022
1 parent 17b288f commit 9f396da
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/commands/RedoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static java.util.Objects.requireNonNull;

import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -14,11 +17,13 @@ public class RedoCommand extends Command {

public static final String MESSAGE_SUCCESS = "Command redone";

private final Logger logger = LogsCenter.getLogger(RedoCommand.class);

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.redoCommand();
logger.info("AddressBook successfully redone");
return new CommandResult(MESSAGE_SUCCESS);
}
}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/commands/UndoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static java.util.Objects.requireNonNull;

import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -14,11 +17,13 @@ public class UndoCommand extends Command {

public static final String MESSAGE_SUCCESS = "Command undone";

private final Logger logger = LogsCenter.getLogger(UndoCommand.class);

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.undoCommand();
logger.info("AddressBook successfully undone");
return new CommandResult(MESSAGE_SUCCESS);
}
}
15 changes: 14 additions & 1 deletion src/main/java/seedu/address/storage/UndoRedoStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;

Expand All @@ -14,6 +17,8 @@ public class UndoRedoStorage {
private List<AddressBook> undoList;
private Stack<AddressBook> redoStack;

private final Logger logger = LogsCenter.getLogger(UndoRedoStorage.class);

/**
* Constructs a {@code UndoRedoStorage}.
*/
Expand All @@ -22,25 +27,31 @@ public UndoRedoStorage() {
redoStack = new Stack<>();
}


private void resize() {
while (undoList.size() > undoRedoLimit) {
logger.info("Undo-able list updated");
undoList.remove(0);
}

while (redoStack.size() > undoRedoLimit) {
logger.info("Redo-able stack updated");
redoStack.remove(0);
}
}

/**
* Resets the stack storing Redo-able AddressBook states.
*/
public void resetRedoStack() {
logger.info("Redo-able stack reset");
redoStack = new Stack<>();
}

/**
* Adds an address book to a list for storage.
*/
public void addToUndo(AddressBook addressBook) {
logger.info("New entry in Undo-able list");
undoList.add(addressBook);
resize();
}
Expand All @@ -50,6 +61,7 @@ public void addToUndo(AddressBook addressBook) {
*/
public AddressBook undo() throws CommandException {
if (undoList.isEmpty() || undoList.size() == 1) {
logger.log(Level.WARNING, "Current AddressBook is not undo-able");
throw new CommandException("No previous command to undo");
}

Expand All @@ -67,6 +79,7 @@ public AddressBook undo() throws CommandException {
*/
public AddressBook redo() throws CommandException {
if (redoStack.isEmpty()) {
logger.log(Level.WARNING, "Current AddressBook is not redo-able");
throw new CommandException("No previous command to redo");
}

Expand Down

0 comments on commit 9f396da

Please sign in to comment.