forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab792c4
commit e79eb84
Showing
1 changed file
with
51 additions
and
54 deletions.
There are no files selected for viewing
105 changes: 51 additions & 54 deletions
105
src/main/java/seedu/address/logic/parser/AddTagCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,60 @@ | ||
package seedu.address.logic.parser; | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.AddTagCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddTagCommand object | ||
*/ | ||
public class AddTagCommandParser implements Parser<AddTagCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddTagCommand | ||
* and returns an AddTagCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddTagCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_TAG); | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
Index index; | ||
|
||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (ParseException pe) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTagCommand.MESSAGE_USAGE), pe); | ||
} | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
AddTagCommand.AddTagDescriptor addTagDescriptor = new AddTagCommand.AddTagDescriptor(); | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.AddTagCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.tag.Tag; | ||
parseTagsToAdd(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(addTagDescriptor::setTags); | ||
|
||
return new AddTagCommand(index, addTagDescriptor); | ||
} | ||
|
||
/** | ||
* Parses input arguments and creates a new AddTagCommand object | ||
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty. | ||
* If {@code tags} contain only one element which is an empty string, it will be parsed into a | ||
* {@code Set<Tag>} containing zero tags. | ||
*/ | ||
public class AddTagCommandParser implements Parser<AddTagCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddTagCommand | ||
* and returns an AddTagCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddTagCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_TAG); | ||
|
||
Index index; | ||
|
||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (ParseException pe) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTagCommand.MESSAGE_USAGE), pe); | ||
} | ||
|
||
AddTagCommand.AddTagDescriptor addTagDescriptor = new AddTagCommand.AddTagDescriptor(); | ||
|
||
parseTagsToAdd(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(addTagDescriptor::setTags); | ||
|
||
return new AddTagCommand(index, addTagDescriptor); | ||
} | ||
private Optional<Set<Tag>> parseTagsToAdd(Collection<String> tags) throws ParseException { | ||
assert tags != null; | ||
|
||
/** | ||
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty. | ||
* If {@code tags} contain only one element which is an empty string, it will be parsed into a | ||
* {@code Set<Tag>} containing zero tags. | ||
*/ | ||
private Optional<Set<Tag>> parseTagsToAdd(Collection<String> tags) throws ParseException { | ||
assert tags != null; | ||
|
||
if (tags.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags; | ||
return Optional.of(ParserUtil.parseTags(tagSet)); | ||
if (tags.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags; | ||
return Optional.of(ParserUtil.parseTags(tagSet)); | ||
} | ||
} |