From dc1ce5055bf848bbc9dc727cc504bc90a43123e1 Mon Sep 17 00:00:00 2001 From: Ng Xing Yu Date: Tue, 8 Nov 2022 02:17:57 +1100 Subject: [PATCH 1/2] Fix parsing of find and find commission commands use argmultimap to parse higher level tags (-all, -any, k/) to avoid bugs due to manual parsing and allow reversing of tags. --- .../seedu/address/logic/parser/CliSyntax.java | 4 ++ .../logic/parser/FindCommandParser.java | 69 ++++--------------- .../parser/FindCommissionCommandParser.java | 66 ++++-------------- 3 files changed, 34 insertions(+), 105 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index df8ff6fd486..890fbaab204 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -34,4 +34,8 @@ public class CliSyntax { /* Filtering Prefix */ public static final Prefix PREFIX_KEYWORD = new Prefix("k/"); + + public static final Prefix PREFIX_FIND_ALL = new Prefix("-all"); + public static final Prefix PREFIX_FIND_ANY = new Prefix("-any"); + } diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommandParser.java index 30122700407..90611120785 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -1,14 +1,17 @@ package seedu.address.logic.parser; +import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ALL; +import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ANY; import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYWORD; import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.Set; -import seedu.address.commons.core.Messages; import seedu.address.logic.commands.FindCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.commission.CompositeCustomerPredicate; @@ -18,76 +21,34 @@ * Parses input arguments and creates a new FindCommand object */ public class FindCommandParser implements Parser { - private static final String INTERSECT_FLAG = "-all"; - private static final String UNION_FLAG = "-any"; - /** * 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 { - String trimmedArgs = args.trim(); - int intersectTagsGroupStart = trimmedArgs.indexOf(INTERSECT_FLAG); - int intersectTagsContentStart = intersectTagsGroupStart + 4; - int unionTagsGroupStart = trimmedArgs.indexOf(UNION_FLAG); - int unionTagsContentStart = unionTagsGroupStart + 4; - - String rawKeywords = ""; - String rawIntersectTags = ""; - String rawUnionTags = ""; - - // Improve readability only - String possibleRawUnionTags = unionTagsContentStart >= trimmedArgs.length() ? "" - : trimmedArgs.substring(unionTagsContentStart).trim(); + requireNonNull(args); + ArgumentMultimap mainArgMultimap = + ArgumentTokenizer.tokenize(" " + args, PREFIX_KEYWORD, PREFIX_FIND_ALL, PREFIX_FIND_ANY); - if (intersectTagsGroupStart != -1) { - rawKeywords = trimmedArgs.substring(0, intersectTagsGroupStart).trim(); - if (unionTagsGroupStart != -1) { - rawIntersectTags = trimmedArgs.substring(intersectTagsGroupStart + 4, unionTagsGroupStart); - rawUnionTags = possibleRawUnionTags; - } else { - rawIntersectTags = intersectTagsContentStart >= trimmedArgs.length() ? "" - : trimmedArgs.substring(intersectTagsContentStart).trim(); - } - } else { - if (unionTagsGroupStart != -1) { - rawKeywords = trimmedArgs.substring(0, unionTagsGroupStart).trim(); - rawUnionTags = possibleRawUnionTags; - } else { - rawKeywords = trimmedArgs; - } - } + List keywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); + Optional rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL); + Optional rawUnionTags = mainArgMultimap.getValue(PREFIX_FIND_ANY); - List keywords = new ArrayList<>(); List intersectTags = new ArrayList<>(); List unionTags = new ArrayList<>(); - if (!rawKeywords.isEmpty()) { - // Whitespace required for tokenize() - rawKeywords = " " + rawKeywords; - ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawKeywords, PREFIX_KEYWORD); - List givenKeywords = argMultimap.getAllValues(PREFIX_KEYWORD); - for (String givenKeyword : givenKeywords) { - if (givenKeyword.isBlank()) { - throw new ParseException(Messages.MESSAGE_KEYWORD_EMPTY); - } - keywords.add(givenKeyword); - } - } - if (!rawIntersectTags.isEmpty()) { - rawIntersectTags = " " + rawIntersectTags; + if (rawIntersectTags.isPresent()) { ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawIntersectTags, PREFIX_TAG); + ArgumentTokenizer.tokenize(rawIntersectTags.map(rawCommand -> " " + rawCommand).orElse(""), + PREFIX_TAG); Set givenIntersectTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); intersectTags.addAll(givenIntersectTags); } - if (!rawUnionTags.isEmpty()) { - rawUnionTags = " " + rawUnionTags; + if (rawUnionTags.isPresent()) { ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawUnionTags, PREFIX_TAG); + ArgumentTokenizer.tokenize(rawUnionTags.map(rawCommand -> " " + rawCommand).orElse(""), PREFIX_TAG); Set givenUnionTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); unionTags.addAll(givenUnionTags); } diff --git a/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java index 7d3e512c2e0..bb3351b93d4 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java @@ -1,14 +1,17 @@ package seedu.address.logic.parser; +import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ALL; +import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ANY; import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYWORD; import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.Set; -import seedu.address.commons.core.Messages; import seedu.address.logic.commands.FindCommissionCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.commission.CompositeCommissionPredicate; @@ -18,8 +21,6 @@ * Parses input arguments and creates a new FindCommissionCommand object */ public class FindCommissionCommandParser implements Parser { - private static final String INTERSECT_FLAG = "-all"; - private static final String UNION_FLAG = "-any"; /** * Parses the given {@code String} of arguments in the context of the FindCommissionCommand @@ -28,66 +29,29 @@ public class FindCommissionCommandParser implements Parser= trimmedArgs.length() ? "" - : trimmedArgs.substring(unionTagsContentStart).trim(); + List keywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); + Optional rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL); + Optional rawUnionTags = mainArgMultimap.getValue(PREFIX_FIND_ANY); - if (intersectTagsGroupStart != -1) { - rawKeywords = trimmedArgs.substring(0, intersectTagsGroupStart).trim(); - if (unionTagsGroupStart != -1) { - rawIntersectTags = trimmedArgs.substring(intersectTagsGroupStart + 4, unionTagsGroupStart); - rawUnionTags = possibleRawUnionTags; - } else { - rawIntersectTags = intersectTagsContentStart >= trimmedArgs.length() ? "" - : trimmedArgs.substring(intersectTagsContentStart).trim(); - } - } else { - if (unionTagsGroupStart != -1) { - rawKeywords = trimmedArgs.substring(0, unionTagsGroupStart).trim(); - rawUnionTags = possibleRawUnionTags; - } else { - rawKeywords = trimmedArgs; - } - } - - - List keywords = new ArrayList<>(); List intersectTags = new ArrayList<>(); List unionTags = new ArrayList<>(); - if (!rawKeywords.isEmpty()) { - rawKeywords = " " + rawKeywords; - ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawKeywords, PREFIX_KEYWORD); - List givenKeywords = argMultimap.getAllValues(PREFIX_KEYWORD); - for (String givenKeyword : givenKeywords) { - if (givenKeyword.isBlank()) { - throw new ParseException(Messages.MESSAGE_KEYWORD_EMPTY); - } - keywords.add(givenKeyword); - } - } - if (!rawIntersectTags.isEmpty()) { - rawIntersectTags = " " + rawIntersectTags; + if (rawIntersectTags.isPresent()) { ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawIntersectTags, PREFIX_TAG); + ArgumentTokenizer.tokenize(rawIntersectTags.map(rawCommand -> " " + rawCommand).orElse(""), + PREFIX_TAG); Set givenIntersectTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); intersectTags.addAll(givenIntersectTags); } - if (!rawUnionTags.isEmpty()) { - rawUnionTags = " " + rawUnionTags; + if (rawUnionTags.isPresent()) { ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawUnionTags, PREFIX_TAG); + ArgumentTokenizer.tokenize(rawUnionTags.map(rawCommand -> " " + rawCommand).orElse(""), PREFIX_TAG); Set givenUnionTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); unionTags.addAll(givenUnionTags); } From 2f752890ee00b6e04890d89830559d9ba5bd86d7 Mon Sep 17 00:00:00 2001 From: Ng Xing Yu Date: Tue, 8 Nov 2022 02:41:02 +1100 Subject: [PATCH 2/2] Merge changes from master update list to set --- .../seedu/address/logic/parser/FindCommandParser.java | 8 +++++++- .../logic/parser/FindCommissionCommandParser.java | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/FindCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommandParser.java index f93af654c93..ab21a8691d2 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -2,6 +2,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_KEYWORD_EMPTY; import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ALL; import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ANY; import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYWORD; @@ -9,6 +10,7 @@ import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -31,10 +33,14 @@ public FindCommand parse(String args) throws ParseException { ArgumentMultimap mainArgMultimap = ArgumentTokenizer.tokenize(" " + args, PREFIX_KEYWORD, PREFIX_FIND_ALL, PREFIX_FIND_ANY); - List keywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); + List rawKeywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); Optional rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL); Optional rawUnionTags = mainArgMultimap.getValue(PREFIX_FIND_ANY); + if (rawKeywords.stream().anyMatch(keyword -> Objects.equals(keyword.strip(), ""))) { + throw new ParseException(MESSAGE_KEYWORD_EMPTY); + } + Set keywords = new HashSet<>(rawKeywords); Set intersectTags = new HashSet<>(); Set unionTags = new HashSet<>(); diff --git a/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java b/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java index 19b7bf1c3d0..284d90bc6e3 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java @@ -2,6 +2,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_KEYWORD_EMPTY; import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ALL; import static seedu.address.logic.parser.CliSyntax.PREFIX_FIND_ANY; import static seedu.address.logic.parser.CliSyntax.PREFIX_KEYWORD; @@ -9,6 +10,7 @@ import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -34,10 +36,15 @@ public FindCommissionCommand parse(String args) throws ParseException { ArgumentMultimap mainArgMultimap = ArgumentTokenizer.tokenize(" " + args, PREFIX_KEYWORD, PREFIX_FIND_ALL, PREFIX_FIND_ANY); - List keywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); + List rawKeywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); Optional rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL); Optional rawUnionTags = mainArgMultimap.getValue(PREFIX_FIND_ANY); + if (rawKeywords.stream().anyMatch(keyword -> Objects.equals(keyword.strip(), ""))) { + throw new ParseException(MESSAGE_KEYWORD_EMPTY); + } + + Set keywords = new HashSet<>(rawKeywords); Set intersectTags = new HashSet<>(); Set unionTags = new HashSet<>();