diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index 3a8305f28..a8075b503 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -2,6 +2,7 @@ import seedu.addressbook.commands.*; import seedu.addressbook.data.exception.IllegalValueException; +import seedu.addressbook.preparedcommands.PreparedCommand; import java.util.*; import java.util.regex.Matcher; @@ -19,14 +20,6 @@ public class Parser { public static final Pattern KEYWORDS_ARGS_FORMAT = Pattern.compile("(?\\S+(?:\\s+\\S+)*)"); // one or more keywords separated by whitespace - public static final Pattern PERSON_DATA_ARGS_FORMAT = // '/' forward slashes are reserved for delimiter prefixes - Pattern.compile("(?[^/]+)" - + " (?p?)p/(?[^/]+)" - + " (?p?)e/(?[^/]+)" - + " (?p?)a/(?
[^/]+)" - + "(?(?: t/[^/]+)*)"); // variable number of tags - - /** * Signals that the user input could not be parsed. */ @@ -57,7 +50,9 @@ public Command parseCommand(String userInput) { final String commandWord = matcher.group("commandWord"); final String arguments = matcher.group("arguments"); - switch (commandWord) { + PreparedCommand pc = new PreparedCommand(arguments); + return pc.prepare(); + /*switch (commandWord) { case AddCommand.COMMAND_WORD: return prepareAdd(arguments); @@ -87,6 +82,7 @@ public Command parseCommand(String userInput) { default: return new HelpCommand(); } + */ } /** @@ -95,52 +91,6 @@ public Command parseCommand(String userInput) { * @param args full command args string * @return the prepared command */ - private Command prepareAdd(String args){ - final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(args.trim()); - // Validate arg string format - if (!matcher.matches()) { - return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); - } - try { - return new AddCommand( - matcher.group("name"), - - matcher.group("phone"), - isPrivatePrefixPresent(matcher.group("isPhonePrivate")), - - matcher.group("email"), - isPrivatePrefixPresent(matcher.group("isEmailPrivate")), - - matcher.group("address"), - isPrivatePrefixPresent(matcher.group("isAddressPrivate")), - - getTagsFromArgs(matcher.group("tagArguments")) - ); - } catch (IllegalValueException ive) { - return new IncorrectCommand(ive.getMessage()); - } - } - - /** - * Checks whether the private prefix of a contact detail in the add command's arguments string is present. - */ - private static boolean isPrivatePrefixPresent(String matchedPrefix) { - return matchedPrefix.equals("p"); - } - - /** - * Extracts the new person's tags from the add command's tag arguments string. - * Merges duplicate tag strings. - */ - private static Set getTagsFromArgs(String tagArguments) throws IllegalValueException { - // no tags - if (tagArguments.isEmpty()) { - return Collections.emptySet(); - } - // replace first delimiter prefix, then split - final Collection tagStrings = Arrays.asList(tagArguments.replaceFirst(" t/", "").split(" t/")); - return new HashSet<>(tagStrings); - } /** diff --git a/src/seedu/addressbook/preparedcommands/PreparedAddCommand.java b/src/seedu/addressbook/preparedcommands/PreparedAddCommand.java new file mode 100644 index 000000000..35ef94054 --- /dev/null +++ b/src/seedu/addressbook/preparedcommands/PreparedAddCommand.java @@ -0,0 +1,80 @@ +package seedu.addressbook.preparedcommands; + +import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import seedu.addressbook.commands.AddCommand; +import seedu.addressbook.commands.Command; +import seedu.addressbook.commands.IncorrectCommand; +import seedu.addressbook.data.exception.IllegalValueException; + +public class PreparedAddCommand extends PreparedCommand { + + public static final Pattern PERSON_DATA_ARGS_FORMAT = // '/' forward slashes are reserved for delimiter prefixes + Pattern.compile("(?[^/]+)" + + " (?p?)p/(?[^/]+)" + + " (?p?)e/(?[^/]+)" + + " (?p?)a/(?
[^/]+)" + + "(?(?: t/[^/]+)*)"); // variable number of tags + + + PreparedAddCommand(String args) { + super(args); + } + + @Override + public Command prepare(){ + final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(arguments.trim()); + // Validate arg string format + if (!matcher.matches()) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); + } + try { + return new AddCommand( + matcher.group("name"), + + matcher.group("phone"), + isPrivatePrefixPresent(matcher.group("isPhonePrivate")), + + matcher.group("email"), + isPrivatePrefixPresent(matcher.group("isEmailPrivate")), + + matcher.group("address"), + isPrivatePrefixPresent(matcher.group("isAddressPrivate")), + + getTagsFromArgs(matcher.group("tagArguments")) + ); + } catch (IllegalValueException ive) { + return new IncorrectCommand(ive.getMessage()); + } + } + + /** + * Checks whether the private prefix of a contact detail in the add command's arguments string is present. + */ + private static boolean isPrivatePrefixPresent(String matchedPrefix) { + return matchedPrefix.equals("p"); + } + + /** + * Extracts the new person's tags from the add command's tag arguments string. + * Merges duplicate tag strings. + */ + private static Set getTagsFromArgs(String tagArguments) throws IllegalValueException { + // no tags + if (tagArguments.isEmpty()) { + return Collections.emptySet(); + } + // replace first delimiter prefix, then split + final Collection tagStrings = Arrays.asList(tagArguments.replaceFirst(" t/", "").split(" t/")); + return new HashSet<>(tagStrings); + } + +} diff --git a/src/seedu/addressbook/preparedcommands/PreparedCommand.java b/src/seedu/addressbook/preparedcommands/PreparedCommand.java new file mode 100644 index 000000000..4a4fb66b5 --- /dev/null +++ b/src/seedu/addressbook/preparedcommands/PreparedCommand.java @@ -0,0 +1,17 @@ +package seedu.addressbook.preparedcommands; + +import seedu.addressbook.commands.Command; + +public class PreparedCommand { + + protected final String arguments; + + public PreparedCommand(String args) { + this.arguments = args; + } + + public Command prepare() { + return null; + } + +}