forked from AY1920S1-CS2103T-W11-3/main
/
AddFeedCommandParser.java
48 lines (37 loc) · 1.88 KB
/
AddFeedCommandParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package seedu.address.logic.parser;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import java.util.stream.Stream;
import seedu.address.logic.commands.AddFeedCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.feed.Feed;
/**
* Parses input arguments and creates a new AddFeedCommand object
*/
public class AddFeedCommandParser implements Parser<AddFeedCommand> {
/**
* Parses the given {@code String} of arguments in the context of the AddFeedCommand
* and returns an AddFeedCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddFeedCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_ADDRESS);
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddFeedCommand.MESSAGE_USAGE));
}
String name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()).fullName;
String address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()).value;
Feed feed = new Feed(name, address);
return new AddFeedCommand(feed);
}
/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}