diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 890fbaab204..df8ff6fd486 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -34,8 +34,4 @@ 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 ab21a8691d2..7e8070f6f1c 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommandParser.java @@ -1,19 +1,14 @@ 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; @@ -23,38 +18,76 @@ * 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 { - requireNonNull(args); - ArgumentMultimap mainArgMultimap = - ArgumentTokenizer.tokenize(" " + args, PREFIX_KEYWORD, PREFIX_FIND_ALL, PREFIX_FIND_ANY); + 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 = ""; - List rawKeywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); - Optional rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL); - Optional rawUnionTags = mainArgMultimap.getValue(PREFIX_FIND_ANY); + // Improve readability only + String possibleRawUnionTags = unionTagsContentStart >= trimmedArgs.length() ? "" + : trimmedArgs.substring(unionTagsContentStart).trim(); - if (rawKeywords.stream().anyMatch(keyword -> Objects.equals(keyword.strip(), ""))) { - throw new ParseException(MESSAGE_KEYWORD_EMPTY); + 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; + } } - Set keywords = new HashSet<>(rawKeywords); + + Set keywords = new HashSet<>(); 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.isPresent()) { + if (!rawIntersectTags.isEmpty()) { + rawIntersectTags = " " + rawIntersectTags; ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawIntersectTags.map(rawCommand -> " " + rawCommand).orElse(""), - PREFIX_TAG); + ArgumentTokenizer.tokenize(rawIntersectTags, PREFIX_TAG); Set givenIntersectTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); intersectTags.addAll(givenIntersectTags); } - if (rawUnionTags.isPresent()) { + if (!rawUnionTags.isEmpty()) { + rawUnionTags = " " + rawUnionTags; ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawUnionTags.map(rawCommand -> " " + rawCommand).orElse(""), PREFIX_TAG); + ArgumentTokenizer.tokenize(rawUnionTags, 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 284d90bc6e3..69eaf612eff 100644 --- a/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/FindCommissionCommandParser.java @@ -1,19 +1,14 @@ 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; @@ -23,6 +18,8 @@ * 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 @@ -31,34 +28,66 @@ public class FindCommissionCommandParser implements Parser rawKeywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD); - Optional rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL); - Optional rawUnionTags = mainArgMultimap.getValue(PREFIX_FIND_ANY); + String possibleRawUnionTags = unionTagsContentStart >= trimmedArgs.length() ? "" + : trimmedArgs.substring(unionTagsContentStart).trim(); - if (rawKeywords.stream().anyMatch(keyword -> Objects.equals(keyword.strip(), ""))) { - throw new ParseException(MESSAGE_KEYWORD_EMPTY); + 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; + } } - Set keywords = new HashSet<>(rawKeywords); + + Set keywords = new HashSet<>(); 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.isPresent()) { + if (!rawIntersectTags.isEmpty()) { + rawIntersectTags = " " + rawIntersectTags; ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawIntersectTags.map(rawCommand -> " " + rawCommand).orElse(""), - PREFIX_TAG); + ArgumentTokenizer.tokenize(rawIntersectTags, PREFIX_TAG); Set givenIntersectTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); intersectTags.addAll(givenIntersectTags); } - if (rawUnionTags.isPresent()) { + if (!rawUnionTags.isEmpty()) { + rawUnionTags = " " + rawUnionTags; ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(rawUnionTags.map(rawCommand -> " " + rawCommand).orElse(""), PREFIX_TAG); + ArgumentTokenizer.tokenize(rawUnionTags, PREFIX_TAG); Set givenUnionTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); unionTags.addAll(givenUnionTags); }