Skip to content

Commit

Permalink
Merge pull request #280 from AY2324S2-CS2103T-W12-3/logic-defensive-c…
Browse files Browse the repository at this point in the history
…oding

Improve defensiveness of Logic and Model components
  • Loading branch information
aureliony authored Apr 15, 2024
2 parents 9b4d9e3 + 41b6aab commit 9239205
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import static seedu.address.model.person.fields.Phone.PREFIX_PHONE;
import static seedu.address.model.person.fields.Tags.PREFIX_TAG;

import java.util.logging.Logger;
import java.util.stream.Stream;

import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
Expand Down Expand Up @@ -53,6 +55,8 @@ public class AddCommand extends Command {
public static final String MESSAGE_SUCCESS = "New contact added: %1$s";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists.";

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

private final Person toAdd;

/**
Expand All @@ -68,6 +72,7 @@ public String execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
logger.finer("Recognized as duplicate person: " + toAdd.toString());
throw new CommandException(MESSAGE_DUPLICATE_CONTACT);
}

Expand All @@ -81,6 +86,7 @@ public String execute(Model model) throws CommandException {
* @throws IllegalArgumentException if the user input does not conform the expected format
*/
public static AddCommand of(String args) throws IllegalArgumentException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_TAG, PREFIX_ASSET);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/seedu/address/logic/commands/AssetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.logging.Logger;
import java.util.stream.Stream;

import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.util.ArgumentMultimap;
import seedu.address.logic.util.ArgumentTokenizer;
import seedu.address.model.Model;
import seedu.address.model.asset.Asset;
import seedu.address.model.person.fields.Asset;
import seedu.address.model.person.fields.Prefix;

/**
Expand All @@ -33,6 +35,8 @@ public class AssetCommand extends Command {
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_INVALID_ASSET_NAME = "The asset name provided is invalid";

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

private final Asset target;
private final Asset editedAsset;

Expand All @@ -52,9 +56,11 @@ public String execute(Model model) throws CommandException {
requireNonNull(model);

if (target.equals(editedAsset)) {
logger.finer("Asset recognized as unedited: " + editedAsset);
throw new CommandException(MESSAGE_NOT_EDITED);
}
if (!model.hasAsset(target)) {
logger.finer("Asset recognized as non-existing: " + editedAsset);
throw new CommandException(MESSAGE_INVALID_ASSET_NAME);
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/logic/commands/CopyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import static seedu.address.model.person.fields.Tags.PREFIX_TAG;

import java.util.List;
import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand Down Expand Up @@ -42,6 +44,8 @@ public class CopyCommand extends Command {
public static final String MESSAGE_NO_PARAM = "One field to copy must be provided.";
public static final String MESSAGE_EXTRA_PARAM = "Only one field can be copied.";

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

private final Index index;
private final boolean[] info;

Expand Down Expand Up @@ -78,21 +82,27 @@ private static String copyToClipboard(Person personToCopy, boolean[] info) {
requireNonNull(personToCopy);

if (info[0]) {
logger.finest("Copied name to clipboard");
return personToCopy.getName().toString();
}
if (info[1]) {
logger.finest("Copied phone to clipboard");
return personToCopy.getPhone().toString();
}
if (info[2]) {
logger.finest("Copied email to clipboard");
return personToCopy.getEmail().toString();
}
if (info[3]) {
logger.finest("Copied address to clipboard");
return personToCopy.getAddress().toString();
}
if (info[4]) {
logger.finest("Copied tags to clipboard");
return personToCopy.getTags().toString();
}
if (info[5]) {
logger.finest("Copied assets to clipboard");
return personToCopy.getAssets().toString();
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;
import static seedu.address.model.person.fields.Address.PREFIX_ADDRESS;
Expand All @@ -15,7 +16,9 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Logger;

import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
Expand Down Expand Up @@ -58,6 +61,8 @@ public class EditCommand extends Command {
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists.";

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

private final Index index;
private final EditPersonDescriptor editPersonDescriptor;

Expand Down Expand Up @@ -86,6 +91,7 @@ public String execute(Model model) throws CommandException {
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
logger.finer("Recognized as duplicate person: " + editedPerson);
throw new CommandException(MESSAGE_DUPLICATE_CONTACT);
}

Expand All @@ -99,7 +105,7 @@ public String execute(Model model) throws CommandException {
* edited with {@code editPersonDescriptor}.
*/
private static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
assert personToEdit != null;
requireAllNonNull(personToEdit, editPersonDescriptor);

Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/commands/RedoCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -19,6 +21,7 @@ public class RedoCommand extends Command {

@Override
public String execute(Model model) throws CommandException {
requireNonNull(model);
if (!model.canRedo()) {
throw new CommandException(MESSAGE_REDO_EXCEPTION);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/commands/UndoCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -18,6 +20,7 @@ public class UndoCommand extends Command {

@Override
public String execute(Model model) throws CommandException {
requireNonNull(model);
if (!model.canUndo()) {
throw new CommandException(MESSAGE_UNDO_EXCEPTION);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import javafx.collections.ObservableList;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.asset.Asset;
import seedu.address.model.exceptions.AddressBookException;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniquePersonList;
import seedu.address.model.person.fields.Asset;

/**
* Wraps all data at the address-book level.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.asset.Asset;
import seedu.address.model.exceptions.AddressBookException;
import seedu.address.model.person.Person;
import seedu.address.model.person.fields.Asset;

/**
* The API of the Model component.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.asset.Asset;
import seedu.address.model.person.Person;
import seedu.address.model.person.fields.Asset;

/**
* Represents the in-memory model of the address book data.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.asset.Asset;
import seedu.address.model.person.fields.Address;
import seedu.address.model.person.fields.Asset;
import seedu.address.model.person.fields.Assets;
import seedu.address.model.person.fields.Email;
import seedu.address.model.person.fields.Name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.asset;
package seedu.address.model.person.fields;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/address/model/person/fields/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.model.asset.Asset;

/**
* Represents a collection of assets associated to a person in the address book.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.tag;
package seedu.address.model.person.fields;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/address/model/person/fields/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.model.tag.Tag;

//@@author aureliony
/**
* Represents an abstraction for a list of tags.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import seedu.address.model.asset.Asset;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;
import seedu.address.model.person.fields.Asset;
import seedu.address.model.person.fields.Tag;

/**
* An UI component that displays information of a {@code Person}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.asset.Asset;
import seedu.address.model.person.Person;
import seedu.address.model.person.fields.Asset;
import seedu.address.testutil.PersonBuilder;

public class AddCommandTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import static seedu.address.logic.commands.AssetCommand.MESSAGE_NOT_EDITED;
import static seedu.address.logic.commands.AssetCommand.MESSAGE_SUCCESS;
import static seedu.address.logic.commands.CommandTestUtil.assertParseFailure;
import static seedu.address.model.asset.Asset.MESSAGE_CONSTRAINTS;
import static seedu.address.model.person.fields.Asset.MESSAGE_CONSTRAINTS;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

Expand All @@ -20,8 +20,8 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.asset.Asset;
import seedu.address.model.person.Person;
import seedu.address.model.person.fields.Asset;
import seedu.address.testutil.AssetBuilder;
import seedu.address.testutil.PersonBuilder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.asset;
package seedu.address.model.person.fields;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import org.junit.jupiter.api.Test;

import seedu.address.model.asset.Asset;

class AssetsTest {

private static final String INVALID_ASSET = " ";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.tag;
package seedu.address.model.person.fields;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/seedu/address/model/person/fields/TagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import org.junit.jupiter.api.Test;

import seedu.address.model.tag.Tag;

class TagsTest {

private static final String INVALID_TAG = "#friend";
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/testutil/AssetBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package seedu.address.testutil;

import seedu.address.model.asset.Asset;
import seedu.address.model.person.fields.Asset;

/**
* A utility class to help with building Person objects.
Expand Down

0 comments on commit 9239205

Please sign in to comment.