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 7e8070f6f1c..ab21a8691d2 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -1,14 +1,19 @@ 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.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; import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import java.util.HashSet; import java.util.List; +import java.util.Objects; +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 +23,38 @@ * 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 = ""; + requireNonNull(args); + ArgumentMultimap mainArgMultimap = + ArgumentTokenizer.tokenize(" " + args, PREFIX_KEYWORD, PREFIX_FIND_ALL, PREFIX_FIND_ANY); - // Improve readability only - String possibleRawUnionTags = unionTagsContentStart >= trimmedArgs.length() ? "" - : trimmedArgs.substring(unionTagsContentStart).trim(); + List rawKeywords = 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; - } + if (rawKeywords.stream().anyMatch(keyword -> Objects.equals(keyword.strip(), ""))) { + throw new ParseException(MESSAGE_KEYWORD_EMPTY); } - - Set keywords = new HashSet<>(); + Set keywords = new HashSet<>(rawKeywords); Set intersectTags = new HashSet<>(); Set unionTags = new HashSet<>(); - 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 69eaf612eff..284d90bc6e3 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java @@ -1,14 +1,19 @@ 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.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; import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import java.util.HashSet; import java.util.List; +import java.util.Objects; +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 +23,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 +31,34 @@ public class FindCommissionCommandParser implements Parser= trimmedArgs.length() ? "" - : trimmedArgs.substring(unionTagsContentStart).trim(); + List rawKeywords = 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; - } + if (rawKeywords.stream().anyMatch(keyword -> Objects.equals(keyword.strip(), ""))) { + throw new ParseException(MESSAGE_KEYWORD_EMPTY); } - - Set keywords = new HashSet<>(); + Set keywords = new HashSet<>(rawKeywords); Set intersectTags = new HashSet<>(); Set unionTags = new HashSet<>(); - 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); }