forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 6
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
2a2188a
commit 050885d
Showing
3 changed files
with
87 additions
and
20 deletions.
There are no files selected for viewing
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
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
69 changes: 69 additions & 0 deletions
69
src/main/java/seedu/address/logic/parser/SelfEditCommandParser.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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PROJECT; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; | ||
import seedu.address.logic.commands.SelfEditCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.project.Project; | ||
|
||
/** | ||
* Parses input arguments and creates a new EditCommand object | ||
*/ | ||
public class SelfEditCommandParser implements Parser<SelfEditCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the EditCommand | ||
* and returns an EditCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public SelfEditCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, | ||
PREFIX_PROJECT); | ||
|
||
EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor(); | ||
if (argMultimap.getValue(PREFIX_PHONE).isPresent()) { | ||
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get())); | ||
} | ||
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) { | ||
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get())); | ||
} | ||
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) { | ||
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get())); | ||
} | ||
parseProjectsForEdit(argMultimap.getAllValues(PREFIX_PROJECT)).ifPresent(editPersonDescriptor::setProjects); | ||
|
||
if (!editPersonDescriptor.isAnyFieldEdited()) { | ||
throw new ParseException(SelfEditCommand.MESSAGE_NOT_EDITED); | ||
} | ||
|
||
return new SelfEditCommand(editPersonDescriptor); | ||
} | ||
|
||
/** | ||
* 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<Project>> parseProjectsForEdit(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.parseProjects(tagSet)); | ||
} | ||
|
||
} |