From 2820b59a87eed3ec763a8b1cb49d7128d7d0ea84 Mon Sep 17 00:00:00 2001 From: notnotmax <156508404+notnotmax@users.noreply.github.com> Date: Mon, 14 Oct 2024 23:48:46 +0800 Subject: [PATCH] Revert abstraction of FindCommand --- .../address/logic/commands/FindCommand.java | 22 ++++++------ .../logic/commands/FindNameCommand.java | 19 ---------- .../logic/parser/FindCommandParser.java | 35 ++++++------------- ...eCommandTest.java => FindCommandTest.java} | 22 ++++++++---- .../logic/parser/AddressBookParserTest.java | 10 +++--- .../logic/parser/FindCommandParserTest.java | 16 ++++----- 6 files changed, 46 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/seedu/address/logic/commands/FindNameCommand.java rename src/test/java/seedu/address/logic/commands/{FindNameCommandTest.java => FindCommandTest.java} (80%) diff --git a/src/main/java/seedu/address/logic/commands/FindCommand.java b/src/main/java/seedu/address/logic/commands/FindCommand.java index ff56b5a1610..4235b51ceb7 100644 --- a/src/main/java/seedu/address/logic/commands/FindCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindCommand.java @@ -2,29 +2,27 @@ import static java.util.Objects.requireNonNull; -import java.util.function.Predicate; - import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.model.Model; -import seedu.address.model.student.Student; +import seedu.address.model.student.NameContainsKeywordsPredicate; /** - * Finds and lists all persons in address book whose attribute satisfies the given predicate. + * Finds and lists all persons in address book whose name contains any of the argument keywords. + * Keyword matching is case-insensitive. */ -public abstract class FindCommand extends Command { +public class FindCommand extends Command { public static final String COMMAND_WORD = "find"; - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons with the specified attribute " - + "containing any of the specified keywords (case-insensitive)" - + "and displays them as a list with index numbers.\n" - + "Parameters: PREFIX/KEYWORD [PREFIX/MORE_KEYWORDS]...\n" - + "Example: " + COMMAND_WORD + " n/alice n/bob"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of " + + "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" + + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + + "Example: " + COMMAND_WORD + " alice bob charlie"; - protected final Predicate predicate; + private final NameContainsKeywordsPredicate predicate; - public FindCommand(Predicate predicate) { + public FindCommand(NameContainsKeywordsPredicate predicate) { this.predicate = predicate; } diff --git a/src/main/java/seedu/address/logic/commands/FindNameCommand.java b/src/main/java/seedu/address/logic/commands/FindNameCommand.java deleted file mode 100644 index b62a4dbbf24..00000000000 --- a/src/main/java/seedu/address/logic/commands/FindNameCommand.java +++ /dev/null @@ -1,19 +0,0 @@ -package seedu.address.logic.commands; - -import seedu.address.model.student.NameContainsKeywordsPredicate; - -/** - * Finds and lists all persons in address book whose name contains any of the argument keywords. - * Keyword matching is case insensitive. - */ -public class FindNameCommand extends FindCommand { - - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of " - + "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" - + "Parameters: n/KEYWORD [n/MORE_KEYWORDS]...\n" - + "Example: " + COMMAND_WORD + " n/alice n/bob n/charlie"; - - public FindNameCommand(NameContainsKeywordsPredicate predicate) { - super(predicate); - } -} diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommandParser.java index 0829c28fe55..9de73c23cdc 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -1,47 +1,32 @@ package seedu.address.logic.parser; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; -import java.util.List; -import java.util.stream.Stream; +import java.util.Arrays; import seedu.address.logic.commands.FindCommand; -import seedu.address.logic.commands.FindNameCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.student.NameContainsKeywordsPredicate; /** - * Parses input arguments and creates a new FindNameCommand object + * Parses input arguments and creates a new FindCommand object */ public class FindCommandParser implements Parser { /** - * Parses the given {@code String} of arguments in the context of the FindNameCommand - * and returns a FindNameCommand object for execution. + * Parses the given {@code String} of arguments in the context of the FindCommand + * and returns a FindCommand object for execution. * @throws ParseException if the user input does not conform the expected format */ public FindCommand parse(String args) throws ParseException { - ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME); - - if (!argMultimap.getPreamble().isEmpty()) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE)); - } - - if (!arePrefixesPresent(argMultimap, PREFIX_NAME)) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE)); + String trimmedArgs = args.trim(); + if (trimmedArgs.isEmpty()) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); } - List nameKeywords = argMultimap.getAllValues(PREFIX_NAME); + String[] nameKeywords = trimmedArgs.split("\\s+"); - return new FindNameCommand(new NameContainsKeywordsPredicate(nameKeywords)); - } - - /** - * Returns true if none of the prefixes contains empty {@code Optional} values in the given - * {@code ArgumentMultimap}. - */ - private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { - return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); + return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); } } diff --git a/src/test/java/seedu/address/logic/commands/FindNameCommandTest.java b/src/test/java/seedu/address/logic/commands/FindCommandTest.java similarity index 80% rename from src/test/java/seedu/address/logic/commands/FindNameCommandTest.java rename to src/test/java/seedu/address/logic/commands/FindCommandTest.java index cfe758de24d..7991b8cb621 100644 --- a/src/test/java/seedu/address/logic/commands/FindNameCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindCommandTest.java @@ -21,9 +21,9 @@ import seedu.address.model.student.NameContainsKeywordsPredicate; /** - * Contains integration tests (interaction with the Model) for {@code FindNameCommand}. + * Contains integration tests (interaction with the Model) for {@code FindCommand}. */ -public class FindNameCommandTest { +public class FindCommandTest { private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); @@ -34,14 +34,14 @@ public void equals() { NameContainsKeywordsPredicate secondPredicate = new NameContainsKeywordsPredicate(Collections.singletonList("second")); - FindNameCommand findFirstCommand = new FindNameCommand(firstPredicate); - FindNameCommand findSecondCommand = new FindNameCommand(secondPredicate); + FindCommand findFirstCommand = new FindCommand(firstPredicate); + FindCommand findSecondCommand = new FindCommand(secondPredicate); // same object -> returns true assertTrue(findFirstCommand.equals(findFirstCommand)); // same values -> returns true - FindNameCommand findFirstCommandCopy = new FindNameCommand(firstPredicate); + FindCommand findFirstCommandCopy = new FindCommand(firstPredicate); assertTrue(findFirstCommand.equals(findFirstCommandCopy)); // different types -> returns false @@ -58,7 +58,7 @@ public void equals() { public void execute_zeroKeywords_noPersonFound() { String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); NameContainsKeywordsPredicate predicate = preparePredicate(" "); - FindNameCommand command = new FindNameCommand(predicate); + FindCommand command = new FindCommand(predicate); expectedModel.updateFilteredPersonList(predicate); assertCommandSuccess(command, model, expectedMessage, expectedModel); assertEquals(Collections.emptyList(), model.getFilteredPersonList()); @@ -68,12 +68,20 @@ public void execute_zeroKeywords_noPersonFound() { public void execute_multipleKeywords_multiplePersonsFound() { String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz"); - FindNameCommand command = new FindNameCommand(predicate); + FindCommand command = new FindCommand(predicate); expectedModel.updateFilteredPersonList(predicate); assertCommandSuccess(command, model, expectedMessage, expectedModel); assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList()); } + @Test + public void toStringMethod() { + NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword")); + FindCommand findCommand = new FindCommand(predicate); + String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; + assertEquals(expected, findCommand.toString()); + } + /** * Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. */ diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 164ac435595..7765bd2d40d 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -24,7 +24,6 @@ import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; import seedu.address.logic.commands.ExitCommand; import seedu.address.logic.commands.FindCommand; -import seedu.address.logic.commands.FindNameCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -79,10 +78,9 @@ public void parseCommand_exit() throws Exception { @Test public void parseCommand_find() throws Exception { List keywords = Arrays.asList("foo", "bar", "baz"); - FindNameCommand command = (FindNameCommand) parser.parseCommand( - FindCommand.COMMAND_WORD + " " - + keywords.stream().map(k -> "n/" + k).collect(Collectors.joining(" "))); - assertEquals(new FindNameCommand(new NameContainsKeywordsPredicate(keywords)), command); + FindCommand command = (FindCommand) parser.parseCommand( + FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" "))); + assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command); } @Test @@ -100,7 +98,7 @@ public void parseCommand_list() throws Exception { @Test public void parseCommand_unrecognisedInput_throwsParseException() { assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), () - -> parser.parseCommand("")); + -> parser.parseCommand("")); } @Test diff --git a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java b/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java index 4079e6a725d..9d3a116ca70 100644 --- a/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/FindCommandParserTest.java @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test; -import seedu.address.logic.commands.FindNameCommand; +import seedu.address.logic.commands.FindCommand; import seedu.address.model.student.NameContainsKeywordsPredicate; public class FindCommandParserTest { @@ -17,19 +17,17 @@ public class FindCommandParserTest { @Test public void parse_emptyArg_throwsParseException() { - assertParseFailure(parser, " ", - String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE)); + assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); } @Test - public void parse_validArgs_returnsFindNameCommand() { + public void parse_validArgs_returnsFindCommand() { // no leading and trailing whitespaces - FindNameCommand expectedFindCommand = - new FindNameCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"))); - assertParseSuccess(parser, " n/Alice n/Bob", expectedFindCommand); + FindCommand expectedFindCommand = + new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"))); + assertParseSuccess(parser, "Alice Bob", expectedFindCommand); // multiple whitespaces between keywords - assertParseSuccess(parser, " \n n/Alice \n \t n/Bob \t", expectedFindCommand); + assertParseSuccess(parser, " \n Alice \n \t Bob \t", expectedFindCommand); } - }