Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#88 from Yukun99/v1.2_RefactorC…
Browse files Browse the repository at this point in the history
…ommands

Refactor: Refactor Command Parsing and Execution
  • Loading branch information
EltonGohJH authored Oct 11, 2021
2 parents eefb42c + 019a4e0 commit c76ea5d
Show file tree
Hide file tree
Showing 119 changed files with 1,701 additions and 1,057 deletions.
2 changes: 1 addition & 1 deletion docs/tutorials/TracingCode.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ Try to keep track of what happens inside the component and where the execution t
public CommandResult execute(Model model) throws CommandException {
...
Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);
Person editedPerson = createEditedPerson(personToEdit, personEditDescriptor);
if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* This is a workaround for the following error when MainApp is made the
* entry point of the application:
*
* Error: JavaFX runtime components are missing, and are required to run this application
* Error: JavaFX runtime components are missing, and are required to run this application
*
* The reason is that MainApp extends Application. In that case, the
* LauncherHelper will check for the javafx.graphics module to be present
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.logic.executors.Executor;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
Expand Down Expand Up @@ -69,6 +70,8 @@ public void init() throws Exception {
logic = new LogicManager(model, storage);

ui = new UiManager(logic);

Executor.setup(model);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/seedu/address/commons/core/LogsCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* Configures and manages loggers and handlers, including their logging level
* Named {@link Logger}s can be obtained from this class<br>
* These loggers have been configured to output messages to the console and a {@code .log} file by default,
* at the {@code INFO} level. A new {@code .log} file with a new numbering will be created after the log
* file reaches 5MB big, up to a maximum of 5 files.<br>
* at the {@code INFO} level. A new {@code .log} file with a new numbering will be created after the log
* file reaches 5MB big, up to a maximum of 5 files.<br>
*/
public class LogsCenter {
private static final int MAX_FILE_COUNT = 5;
Expand Down Expand Up @@ -95,6 +95,7 @@ private static void addFileHandler(Logger logger) {

/**
* Creates a {@code FileHandler} for the log file.
*
* @throws IOException if there are problems opening the file.
*/
private static FileHandler createFileHandler() throws IOException {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_GROUP_DISPLAYED_INDEX = "The group index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_GROUPS_LISTED_OVERVIEW = "%1$d groups listed!";

}
1 change: 1 addition & 0 deletions src/main/java/seedu/address/commons/core/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public boolean isEarlyAccess() {

/**
* Parses a version number string in the format V1.2.3.
*
* @param versionString version number string
* @return a Version object
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/commons/util/CollectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/
public class CollectionUtil {

/** @see #requireAllNonNull(Collection) */
/**
* @see #requireAllNonNull(Collection)
*/
public static void requireAllNonNull(Object... items) {
requireNonNull(items);
Stream.of(items).forEach(Objects::requireNonNull);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/commons/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static boolean isFileExists(Path file) {
/**
* Returns true if {@code path} can be converted into a {@code Path} via {@link Paths#get(String)},
* otherwise returns false.
*
* @param path A string representing the file path. Cannot be null.
*/
public static boolean isValidPath(String path) {
Expand All @@ -33,6 +34,7 @@ public static boolean isValidPath(String path) {

/**
* Creates a file if it does not exist along with its missing parent directories.
*
* @throws IOException if the file or directory cannot be created.
*/
public static void createIfMissing(Path file) throws IOException {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/address/commons/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static <T> T deserializeObjectFromJsonFile(Path jsonFile, Class<T> classOfObject
/**
* Returns the Json object from the given file or {@code Optional.empty()} object if the file is not found.
* If any values are missing from the file, default values will be used, as long as the file is a valid json file.
*
* @param filePath cannot be null.
* @param classOfObjectToDeserialize Json file has to correspond to the structure in the class given here.
* @throws DataConversionException if the file format is not as expected.
Expand Down Expand Up @@ -79,6 +80,7 @@ public static <T> Optional<T> readJsonFile(
/**
* Saves the Json object to the specified file.
* Overwrites existing file if it exists, creates a new file if it doesn't.
*
* @param jsonFile cannot be null
* @param filePath cannot be null
* @throws IOException if there was an error during writing to the file
Expand All @@ -93,6 +95,7 @@ public static <T> void saveJsonFile(T jsonFile, Path filePath) throws IOExceptio

/**
* Converts a given string representation of a JSON data to instance of a class
*
* @param <T> The generic type to create an instance of
* @return The instance of T with the specified values in the JSON string
*/
Expand All @@ -102,6 +105,7 @@ public static <T> T fromJsonString(String json, Class<T> instanceClass) throws I

/**
* Converts a given instance of a class into its JSON data string representation
*
* @param instance The T object to be converted into the JSON string
* @param <T> The generic type to create an instance of
* @return JSON data representation of the given class instance, in string
Expand All @@ -128,7 +132,6 @@ protected Level _deserialize(String value, DeserializationContext ctxt) {
* Gets the logging level that matches loggingLevelString
* <p>
* Returns null if there are no matches
*
*/
private Level getLoggingLevel(String loggingLevelString) {
return Level.parse(loggingLevelString);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/seedu/address/commons/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ public class StringUtil {

/**
* Returns true if the {@code sentence} contains the {@code word}.
* Ignores case, but a full word match is required.
* <br>examples:<pre>
* Ignores case, but a full word match is required.
* <br>examples:<pre>
* containsWordIgnoreCase("ABc def", "abc") == true
* containsWordIgnoreCase("ABc def", "DEF") == true
* containsWordIgnoreCase("ABc def", "AB") == false //not a full word match
* </pre>
*
* @param sentence cannot be null
* @param word cannot be null, cannot be empty, must be a single word
*/
Expand Down Expand Up @@ -53,6 +54,7 @@ public static String getDetails(Throwable t) {
* e.g. 1, 2, 3, ..., {@code Integer.MAX_VALUE} <br>
* Will return false for any other non-null string input
* e.g. empty string, "-1", "0", "+1", and " 2 " (untrimmed), "3 0" (contains whitespace), "1 a" (contains letters)
*
* @throws NullPointerException if {@code s} is null.
*/
public static boolean isNonZeroUnsignedInteger(String s) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.executors.exceptions.ExecuteException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.Person;
Expand All @@ -16,12 +17,13 @@
public interface Logic {
/**
* Executes the command and returns the result.
*
* @param commandText The command as entered by the user.
* @return the result of the command execution.
* @throws CommandException If an error occurs during command execution.
* @throws ParseException If an error occurs during parsing.
*/
CommandResult execute(String commandText) throws CommandException, ParseException;
CommandResult execute(String commandText) throws CommandException, ParseException, ExecuteException;

/**
* Returns the AddressBook.
Expand All @@ -30,7 +32,9 @@ public interface Logic {
*/
ReadOnlyAddressBook getAddressBook();

/** Returns an unmodifiable view of the filtered list of persons */
/**
* Returns an unmodifiable view of the filtered list of persons
*/
ObservableList<Person> getFilteredPersonList();

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.executors.exceptions.ExecuteException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
Expand Down Expand Up @@ -38,7 +39,7 @@ public LogicManager(Model model, Storage storage) {
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
public CommandResult execute(String commandText) throws CommandException, ParseException, ExecuteException {
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
* Clears the address book.
*/
public class ClearCommand extends Command {

public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Address book has been cleared!";


@Override
public CommandResult execute(Model model) {
requireNonNull(model);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package seedu.address.logic.commands;

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

/**
* Represents a command with hidden internal logic and the ability to be executed.
*/
public abstract class Command {

/**
* Executes the command and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
public abstract CommandResult execute(Model model) throws CommandException;

public abstract CommandResult execute(Model model) throws CommandException, ExecuteException;
}
8 changes: 6 additions & 2 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ public class CommandResult {

private final String feedbackToUser;

/** Help information should be shown to the user. */
/**
* Help information should be shown to the user.
*/
private final boolean showHelp;

/** The application should exit. */
/**
* The application should exit.
*/
private final boolean exit;

/**
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java

This file was deleted.

Loading

0 comments on commit c76ea5d

Please sign in to comment.