forked from nus-cs2103-AY1819S1/addressbook-level4
-
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.
Merge branch 'master' of https://github.com/CS2103-AY1819S1-F10-2/main …
…into fix-ug-issues
- Loading branch information
Showing
8 changed files
with
151 additions
and
120 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package loanbook.logic.parser; | ||
|
||
import static loanbook.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import loanbook.logic.commands.Command; | ||
import loanbook.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Represents a Parser that parses a use input of only prefixed arguments | ||
* for use by a {@code Command} of type {@code T}. | ||
*/ | ||
public abstract class ArgumentParser<T extends Command> implements Parser<T> { | ||
|
||
/** | ||
* Generates an ArgumentMultimap from the given argument String and data specifications. | ||
* @param argsString The String passed to the command, containing all the arguments. | ||
* @param requiredArgs An array of Prefixes representing required data fields. | ||
* @param optionalArgs An array of Prefixes representing optional data fields. | ||
* @param msgUsage The message informing the user of the correct command usage. | ||
* @return an ArgumentMultimap containing the data from {@code args}. | ||
* @throws ParseException if the command does not have all required arguments, or has a pre-amble. | ||
*/ | ||
protected ArgumentMultimap getArgumentMultimap( | ||
String argsString, | ||
List<Prefix> requiredArgs, | ||
List<Prefix> optionalArgs, | ||
String msgUsage) throws ParseException { | ||
|
||
List<Prefix> allArgs = new ArrayList(); | ||
allArgs.addAll(requiredArgs); | ||
allArgs.addAll(optionalArgs); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(argsString, allArgs); | ||
|
||
if (!arePrefixesPresent(argMultimap, requiredArgs) || !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, msgUsage)); | ||
} | ||
|
||
return argMultimap; | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, List<Prefix> prefixes) { | ||
return prefixes.stream().allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
Oops, something went wrong.