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

[W6.4d][F09-B4]Zhong ZhengXin #731

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public CommandResult execute() {
}
}

@Override
public boolean isMutating() {
return true;
}

}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public CommandResult execute() {
addressBook.clear();
return new CommandResult(MESSAGE_SUCCESS);
}

@Override
public boolean isMutating() {
return true;
}
}
6 changes: 6 additions & 0 deletions src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ public int getTargetIndex() {
public void setTargetIndex(int targetIndex) {
this.targetIndex = targetIndex;
}

public abstract boolean isMutating( );
/**
* Checks whether the commands change the data
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more precise phrasing would be Returns true if.....

It is better to mention that it returns true if the command potentially changes the data. Try to be as precise as possible.

*/

}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public CommandResult execute() {
}
}

@Override
public boolean isMutating() {
return true;
}

}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public CommandResult execute() {
return new CommandResult(MESSAGE_EXIT_ACKNOWEDGEMENT);
}

@Override
public boolean isMutating() {
return false;
}

}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public CommandResult execute() {
return new CommandResult(getMessageForPersonListShownSummary(personsFound), personsFound);
}

@Override
public boolean isMutating() {
return false;
}

/**
* Retrieve all persons in the address book whose names contain some of the specified keywords.
*
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public class HelpCommand extends Command {
public CommandResult execute() {
return new CommandResult(MESSAGE_ALL_USAGES);
}

@Override
public boolean isMutating() {
return false;
}
}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/IncorrectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public CommandResult execute() {
return new CommandResult(feedbackToUser);
}

@Override
public boolean isMutating() {
return false;
}

}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public CommandResult execute() {
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(getMessageForPersonListShownSummary(allPersons), allPersons);
}

@Override
public boolean isMutating() {
return false;
}
}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ViewAllCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public CommandResult execute() {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
}

@Override
public boolean isMutating() {
return false;
}
}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public CommandResult execute() {
}
}

@Override
public boolean isMutating() {
return false;
}

}
6 changes: 4 additions & 2 deletions src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public CommandResult execute(String userCommandText) throws Exception {
}

/**
* Executes the command, updates storage, and returns the result.
* Executes the command, updates storage only when the data is mutated, and returns the result.
*
* @param command user command
* @return result of the command
Expand All @@ -85,7 +85,9 @@ public CommandResult execute(String userCommandText) throws Exception {
private CommandResult execute(Command command) throws Exception {
command.setData(addressBook, lastShownList);
CommandResult result = command.execute();
storage.save(addressBook);
if(command.isMutating()){
storage.save(addressBook);
}
return result;
}

Expand Down
6 changes: 5 additions & 1 deletion test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.storage.StorageFile;
import seedu.addressbook.parser.Parser;

import java.util.*;

Expand Down Expand Up @@ -90,7 +91,10 @@ private void assertCommandBehavior(String inputCommand,
//Confirm the state of data is as expected
assertEquals(expectedAddressBook, addressBook);
assertEquals(lastShownList, logic.getLastShownList());
assertEquals(addressBook, saveFile.load());
if(new Parser().parseCommand(inputCommand).isMutating()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change does not help as it does not confirm that saving happens only on mutating commands. Tests will pass even without this change. To be more complete, you should have an else branch to confirm that saving does not happen if the command is non mutating.

assertEquals(addressBook, saveFile.load());
}

}


Expand Down