Skip to content

Commit

Permalink
Merge pull request #246 from AY2223S1-CS2103T-W11-3/revert-244-fix_fi…
Browse files Browse the repository at this point in the history
…nd_findcom_tag_order

Revert "Fix parsing of find and find commission commands"
  • Loading branch information
zlimez authored Nov 7, 2022
2 parents 62a5191 + ad86423 commit 57ffcb3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 44 deletions.
4 changes: 0 additions & 4 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}
73 changes: 53 additions & 20 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,38 +18,76 @@
* Parses input arguments and creates a new FindCommand object
*/
public class FindCommandParser implements Parser<FindCommand> {
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<String> rawKeywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD);
Optional<String> rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL);
Optional<String> 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<String> keywords = new HashSet<>(rawKeywords);

Set<String> keywords = new HashSet<>();
Set<Tag> intersectTags = new HashSet<>();
Set<Tag> unionTags = new HashSet<>();
if (!rawKeywords.isEmpty()) {
// Whitespace required for tokenize()
rawKeywords = " " + rawKeywords;
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(rawKeywords, PREFIX_KEYWORD);
List<String> 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<Tag> 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<Tag> givenUnionTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
unionTags.addAll(givenUnionTags);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,6 +18,8 @@
* Parses input arguments and creates a new FindCommissionCommand object
*/
public class FindCommissionCommandParser implements Parser<FindCommissionCommand> {
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
Expand All @@ -31,34 +28,66 @@ public class FindCommissionCommandParser implements Parser<FindCommissionCommand
* @throws ParseException if the user input does not conform the expected format.
*/
public FindCommissionCommand parse(String args) throws ParseException {
requireNonNull(args);
String trimmedArgs = args.trim();
int intersectTagsGroupStart = trimmedArgs.indexOf(INTERSECT_FLAG);
int intersectTagsContentStart = intersectTagsGroupStart + 4;
int unionTagsGroupStart = trimmedArgs.indexOf(UNION_FLAG);
int unionTagsContentStart = unionTagsGroupStart + 4;

ArgumentMultimap mainArgMultimap =
ArgumentTokenizer.tokenize(" " + args, PREFIX_KEYWORD, PREFIX_FIND_ALL, PREFIX_FIND_ANY);
String rawKeywords = "";
String rawIntersectTags = "";
String rawUnionTags = "";

List<String> rawKeywords = mainArgMultimap.getAllValues(PREFIX_KEYWORD);
Optional<String> rawIntersectTags = mainArgMultimap.getValue(PREFIX_FIND_ALL);
Optional<String> 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<String> keywords = new HashSet<>(rawKeywords);

Set<String> keywords = new HashSet<>();
Set<Tag> intersectTags = new HashSet<>();
Set<Tag> unionTags = new HashSet<>();
if (!rawKeywords.isEmpty()) {
rawKeywords = " " + rawKeywords;
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(rawKeywords, PREFIX_KEYWORD);
List<String> 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<Tag> 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<Tag> givenUnionTags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
unionTags.addAll(givenUnionTags);
}
Expand Down

0 comments on commit 57ffcb3

Please sign in to comment.