diff --git a/docs/UserGuide.md b/docs/UserGuide.md index aa89951e6a8..d007de2cdd4 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -41,7 +41,7 @@ SteveJobs++ (SJ++) is a **desktop app for managing job applications and contacts e.g `--name NAME [--tag TAG]` can be used as `--name John Doe --tag friend` or as `--name John Doe`. * Items in angled brackets describe what the content should be.
- e.g. `--date ` means the parameter `DATE`, which is supplied after the `--date` prefix, should be formated as `yyyymmdd`. + e.g. `--date ` means the parameter `DATE`, which is supplied after the `--date` flag, should be formated as `yyyymmdd`. * Items with `...` after them can be used multiple times including zero times.
e.g. `[--tag TAG]...` can be used as ` ` (i.e. 0 times), `--tag friend`, `--tag friend --tag family` etc. diff --git a/docs/diagrams/ParserClasses.puml b/docs/diagrams/ParserClasses.puml index 0c7424de6e0..43216a34d15 100644 --- a/docs/diagrams/ParserClasses.puml +++ b/docs/diagrams/ParserClasses.puml @@ -15,7 +15,7 @@ Class CliSyntax Class ParserUtil Class ArgumentMultimap Class ArgumentTokenizer -Class Prefix +Class Flag } Class HiddenOutside #FFFFFF @@ -30,9 +30,9 @@ XYZCommandParser ..> ArgumentMultimap XYZCommandParser ..> ArgumentTokenizer ArgumentTokenizer .left.> ArgumentMultimap XYZCommandParser ..> CliSyntax -CliSyntax ..> Prefix +CliSyntax ..> Flag XYZCommandParser ..> ParserUtil -ParserUtil .down.> Prefix -ArgumentTokenizer .down.> Prefix +ParserUtil .down.> Flag +ArgumentTokenizer .down.> Flag XYZCommand -up-|> Command @enduml diff --git a/docs/tutorials/AddRemark.md b/docs/tutorials/AddRemark.md index c2f935f4808..14fcb0cdd7f 100644 --- a/docs/tutorials/AddRemark.md +++ b/docs/tutorials/AddRemark.md @@ -151,27 +151,27 @@ Thankfully, `ArgumentTokenizer#tokenize()` makes it trivial to parse user input. ``` java /** * Tokenizes an arguments string and returns an {@code ArgumentMultimap} - * object that maps prefixes to their respective argument values. Only the - * given prefixes will be recognized in the arguments string. + * object that maps flags to their respective argument values. Only the + * given flags will be recognized in the arguments string. * * @param argsString Arguments string of the form: - * {@code preamble value value ...} - * @param prefixes Prefixes to tokenize the arguments string with - * @return ArgumentMultimap object that maps prefixes to their + * {@code preamble value value ...} + * @param flags Prefixes to tokenize the arguments string with + * @return ArgumentMultimap object that maps flags to their * arguments */ ``` -We can tell `ArgumentTokenizer#tokenize()` to look out for our new prefix `r/` and it will return us an instance of `ArgumentMultimap`. Now let’s find out what we need to do in order to obtain the Index and String that we need. Let’s look through `ArgumentMultimap` : +We can tell `ArgumentTokenizer#tokenize()` to look out for our new flag `r/` and it will return us an instance of `ArgumentMultimap`. Now let’s find out what we need to do in order to obtain the Index and String that we need. Let’s look through `ArgumentMultimap` : **`ArgumentMultimap.java`:** ``` java /** - * Returns the last value of {@code prefix}. + * Returns the last value of {@code flag}. */ -public Optional getValue(Prefix prefix) { - List values = getAllValues(prefix); +public Optional getValue(Prefix flag) { + List values = getAllValues(flag); return values.isEmpty() ? Optional.empty() : Optional.of(values.get(values.size() - 1)); } diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index bc2315c5b37..35d2083a3e9 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -4,7 +4,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import seedu.address.logic.parser.Prefix; +import seedu.address.logic.parser.Flag; import seedu.address.model.person.Contact; /** @@ -20,13 +20,13 @@ public class Messages { "Multiple values specified for the following single-valued field(s): "; /** - * Returns an error message indicating the duplicate prefixes. + * Returns an error message indicating the duplicate flags. */ - public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePrefixes) { - assert duplicatePrefixes.length > 0; + public static String getErrorMessageForDuplicateFlags(Flag... duplicateFlags) { + assert duplicateFlags.length > 0; Set duplicateFields = - Stream.of(duplicatePrefixes).map(Prefix::toString).collect(Collectors.toSet()); + Stream.of(duplicateFlags).map(Flag::toString).collect(Collectors.toSet()); return MESSAGE_DUPLICATE_FIELDS + String.join(" ", duplicateFields); } diff --git a/src/main/java/seedu/address/logic/commands/AddCommand.java b/src/main/java/seedu/address/logic/commands/AddCommand.java index df2dd326a74..c320ebc73c4 100644 --- a/src/main/java/seedu/address/logic/commands/AddCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddCommand.java @@ -1,11 +1,11 @@ package seedu.address.logic.commands; 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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_NAME; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_TAG; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; @@ -22,18 +22,18 @@ public class AddCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a contact to the address book. " + "Parameters: " - + PREFIX_NAME + "NAME " - + PREFIX_PHONE + "PHONE " - + PREFIX_EMAIL + "EMAIL " - + PREFIX_ADDRESS + "ADDRESS " - + "[" + PREFIX_TAG + "TAG]...\n" + + FLAG_NAME + " NAME " + + FLAG_PHONE + " PHONE " + + FLAG_EMAIL + " EMAIL " + + FLAG_ADDRESS + " ADDRESS " + + "[" + FLAG_TAG + " TAG]...\n" + "Example: " + COMMAND_WORD + " " - + PREFIX_NAME + "John Doe " - + PREFIX_PHONE + "98765432 " - + PREFIX_EMAIL + "johnd@example.com " - + PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 " - + PREFIX_TAG + "friends " - + PREFIX_TAG + "owesMoney"; + + FLAG_NAME + " John Doe " + + FLAG_PHONE + " 98765432 " + + FLAG_EMAIL + " johnd@example.com " + + FLAG_ADDRESS + " 311, Clementi Ave 2, #02-25 " + + FLAG_TAG + " friends " + + FLAG_TAG + " owesMoney"; public static final String MESSAGE_SUCCESS = "New contact added: %1$s"; public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the address book"; diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index f5738e7d73b..bb7c80fb9c8 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -1,11 +1,11 @@ package seedu.address.logic.commands; 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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_NAME; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_TAG; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CONTACTS; import java.util.Collections; @@ -39,14 +39,14 @@ public class EditCommand extends Command { + "by the index number used in the displayed contact list. " + "Existing values will be overwritten by the input values.\n" + "Parameters: INDEX (must be a positive integer) " - + "[" + PREFIX_NAME + "NAME] " - + "[" + PREFIX_PHONE + "PHONE] " - + "[" + PREFIX_EMAIL + "EMAIL] " - + "[" + PREFIX_ADDRESS + "ADDRESS] " - + "[" + PREFIX_TAG + "TAG]...\n" + + "[" + FLAG_NAME + " NAME] " + + "[" + FLAG_PHONE + " PHONE] " + + "[" + FLAG_EMAIL + " EMAIL] " + + "[" + FLAG_ADDRESS + " ADDRESS] " + + "[" + FLAG_TAG + " TAG]...\n" + "Example: " + COMMAND_WORD + " 1 " - + PREFIX_PHONE + "91234567 " - + PREFIX_EMAIL + "johndoe@example.com"; + + FLAG_PHONE + " 91234567 " + + FLAG_EMAIL + " johndoe@example.com"; public static final String MESSAGE_EDIT_CONTACT_SUCCESS = "Edited Contact: %1$s"; public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; diff --git a/src/main/java/seedu/address/logic/parser/AddCommandParser.java b/src/main/java/seedu/address/logic/parser/AddCommandParser.java index edf447aba45..f5e76199e34 100644 --- a/src/main/java/seedu/address/logic/parser/AddCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddCommandParser.java @@ -1,11 +1,11 @@ package seedu.address.logic.parser; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_NAME; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_TAG; import java.util.Set; import java.util.stream.Stream; @@ -31,19 +31,19 @@ public class AddCommandParser implements Parser { */ public AddCommand parse(String args) throws ParseException { ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG); + ArgumentTokenizer.tokenize(args, FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS, FLAG_TAG); - if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL) + if (!areFlagsPresent(argMultimap, FLAG_NAME, FLAG_ADDRESS, FLAG_PHONE, FLAG_EMAIL) || !argMultimap.getPreamble().isEmpty()) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE)); } - argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS); - Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()); - Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()); - Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()); - Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()); - Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); + argMultimap.verifyNoDuplicateFlagsFor(FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS); + Name name = ParserUtil.parseName(argMultimap.getValue(FLAG_NAME).get()); + Phone phone = ParserUtil.parsePhone(argMultimap.getValue(FLAG_PHONE).get()); + Email email = ParserUtil.parseEmail(argMultimap.getValue(FLAG_EMAIL).get()); + Address address = ParserUtil.parseAddress(argMultimap.getValue(FLAG_ADDRESS).get()); + Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(FLAG_TAG)); Contact contact = new Contact(name, phone, email, address, tagList); @@ -51,11 +51,11 @@ public AddCommand parse(String args) throws ParseException { } /** - * Returns true if none of the prefixes contains empty {@code Optional} values in the given + * Returns true if none of the flags 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()); + private static boolean areFlagsPresent(ArgumentMultimap argumentMultimap, Flag... flags) { + return Stream.of(flags).allMatch(flag -> argumentMultimap.getValue(flag).isPresent()); } } diff --git a/src/main/java/seedu/address/logic/parser/ArgumentMultimap.java b/src/main/java/seedu/address/logic/parser/ArgumentMultimap.java index 21e26887a83..86f66de7ecb 100644 --- a/src/main/java/seedu/address/logic/parser/ArgumentMultimap.java +++ b/src/main/java/seedu/address/logic/parser/ArgumentMultimap.java @@ -11,68 +11,68 @@ import seedu.address.logic.parser.exceptions.ParseException; /** - * Stores mapping of prefixes to their respective arguments. + * Stores mapping of flags to their respective arguments. * Each key may be associated with multiple argument values. * Values for a given key are stored in a list, and the insertion ordering is maintained. * Keys are unique, but the list of argument values may contain duplicate argument values, i.e. the same argument value - * can be inserted multiple times for the same prefix. + * can be inserted multiple times for the same flag. */ public class ArgumentMultimap { /** Prefixes mapped to their respective arguments**/ - private final Map> argMultimap = new HashMap<>(); + private final Map> argMultimap = new HashMap<>(); /** - * Associates the specified argument value with {@code prefix} key in this map. + * Associates the specified argument value with {@code flag} key in this map. * If the map previously contained a mapping for the key, the new value is appended to the list of existing values. * - * @param prefix Prefix key with which the specified argument value is to be associated - * @param argValue Argument value to be associated with the specified prefix key + * @param flag Prefix key with which the specified argument value is to be associated + * @param argValue Argument value to be associated with the specified flag key */ - public void put(Prefix prefix, String argValue) { - List argValues = getAllValues(prefix); + public void put(Flag flag, String argValue) { + List argValues = getAllValues(flag); argValues.add(argValue); - argMultimap.put(prefix, argValues); + argMultimap.put(flag, argValues); } /** - * Returns the last value of {@code prefix}. + * Returns the last value of {@code flag}. */ - public Optional getValue(Prefix prefix) { - List values = getAllValues(prefix); + public Optional getValue(Flag flag) { + List values = getAllValues(flag); return values.isEmpty() ? Optional.empty() : Optional.of(values.get(values.size() - 1)); } /** - * Returns all values of {@code prefix}. - * If the prefix does not exist or has no values, this will return an empty list. + * Returns all values of {@code flag}. + * If the flag does not exist or has no values, this will return an empty list. * Modifying the returned list will not affect the underlying data structure of the ArgumentMultimap. */ - public List getAllValues(Prefix prefix) { - if (!argMultimap.containsKey(prefix)) { + public List getAllValues(Flag flag) { + if (!argMultimap.containsKey(flag)) { return new ArrayList<>(); } - return new ArrayList<>(argMultimap.get(prefix)); + return new ArrayList<>(argMultimap.get(flag)); } /** - * Returns the preamble (text before the first valid prefix). Trims any leading/trailing spaces. + * Returns the preamble (text before the first valid flag). Trims any leading/trailing spaces. */ public String getPreamble() { - return getValue(new Prefix("")).orElse(""); + return getValue(new Flag("", null, null)).orElse(""); } /** - * Throws a {@code ParseException} if any of the prefixes given in {@code prefixes} appeared more than + * Throws a {@code ParseException} if any of the flags given in {@code flags} appeared more than * once among the arguments. */ - public void verifyNoDuplicatePrefixesFor(Prefix... prefixes) throws ParseException { - Prefix[] duplicatedPrefixes = Stream.of(prefixes).distinct() - .filter(prefix -> argMultimap.containsKey(prefix) && argMultimap.get(prefix).size() > 1) - .toArray(Prefix[]::new); + public void verifyNoDuplicateFlagsFor(Flag... flags) throws ParseException { + Flag[] duplicatedFlags = Stream.of(flags).distinct() + .filter(flag -> argMultimap.containsKey(flag) && argMultimap.get(flag).size() > 1) + .toArray(Flag[]::new); - if (duplicatedPrefixes.length > 0) { - throw new ParseException(Messages.getErrorMessageForDuplicatePrefixes(duplicatedPrefixes)); + if (duplicatedFlags.length > 0) { + throw new ParseException(Messages.getErrorMessageForDuplicateFlags(duplicatedFlags)); } } } diff --git a/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java b/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java index 5c9aebfa488..330645e214f 100644 --- a/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java +++ b/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java @@ -6,8 +6,8 @@ import java.util.stream.Collectors; /** - * Tokenizes arguments string of the form: {@code preamble value value ...}
- * e.g. {@code some preamble text t/ 11.00 t/12.00 k/ m/ July} where prefixes are {@code t/ k/ m/}.
+ * Tokenizes arguments string of the form: {@code preamble value value ...}
+ * e.g. {@code some preamble text t/ 11.00 t/12.00 k/ m/ July} where flag are {@code t/ k/ m/}.
* 1. An argument's value can be an empty string e.g. the value of {@code k/} in the above example.
* 2. Leading and trailing whitespaces of an argument value will be discarded.
* 3. An argument may be repeated and all its values will be accumulated e.g. the value of {@code t/} @@ -16,123 +16,124 @@ public class ArgumentTokenizer { /** - * Tokenizes an arguments string and returns an {@code ArgumentMultimap} object that maps prefixes to their - * respective argument values. Only the given prefixes will be recognized in the arguments string. + * Tokenizes an arguments string and returns an {@code ArgumentMultimap} object that maps flag to their + * respective argument values. Only the given flag will be recognized in the arguments string. * - * @param argsString Arguments string of the form: {@code preamble value value ...} - * @param prefixes Prefixes to tokenize the arguments string with - * @return ArgumentMultimap object that maps prefixes to their arguments + * @param argsString Arguments string of the form: {@code preamble value value ...} + * @param flags Flags to tokenize the arguments string with + * @return ArgumentMultimap object that maps flag to their arguments */ - public static ArgumentMultimap tokenize(String argsString, Prefix... prefixes) { - List positions = findAllPrefixPositions(argsString, prefixes); + public static ArgumentMultimap tokenize(String argsString, Flag... flags) { + List positions = findAllFlagPositions(argsString, flags); return extractArguments(argsString, positions); } /** - * Finds all zero-based prefix positions in the given arguments string. + * Finds all zero-based flag positions in the given arguments string. * - * @param argsString Arguments string of the form: {@code preamble value value ...} - * @param prefixes Prefixes to find in the arguments string - * @return List of zero-based prefix positions in the given arguments string + * @param argsString Arguments string of the form: {@code preamble value value ...} + * @param flags Flags to find in the arguments string + * @return List of zero-based flag positions in the given arguments string */ - private static List findAllPrefixPositions(String argsString, Prefix... prefixes) { - return Arrays.stream(prefixes) - .flatMap(prefix -> findPrefixPositions(argsString, prefix).stream()) + private static List findAllFlagPositions(String argsString, Flag... flags) { + return Arrays.stream(flags) + .flatMap(flag -> findFlagPositions(argsString, flag).stream()) .collect(Collectors.toList()); } /** - * {@see findAllPrefixPositions} + * {@see findAllFlagPositions} */ - private static List findPrefixPositions(String argsString, Prefix prefix) { - List positions = new ArrayList<>(); - - int prefixPosition = findPrefixPosition(argsString, prefix.getPrefix(), 0); - while (prefixPosition != -1) { - PrefixPosition extendedPrefix = new PrefixPosition(prefix, prefixPosition); - positions.add(extendedPrefix); - prefixPosition = findPrefixPosition(argsString, prefix.getPrefix(), prefixPosition); + private static List findFlagPositions(String argsString, Flag flag) { + List positions = new ArrayList<>(); + + int flagPosition = findFlagPosition(argsString, flag.getFlagString(), 0); + while (flagPosition != -1) { + FlagPosition extendedFlag = new FlagPosition(flag, flagPosition); + positions.add(extendedFlag); + flagPosition = findFlagPosition(argsString, flag.getFlagString(), flagPosition); } return positions; } /** - * Returns the index of the first occurrence of {@code prefix} in + * Returns the index of the first occurrence of {@code flag} in * {@code argsString} starting from index {@code fromIndex}. An occurrence - * is valid if there is a whitespace before {@code prefix}. Returns -1 if no + * is valid if there is a whitespace before {@code flag}. Returns -1 if no * such occurrence can be found. * - * E.g if {@code argsString} = "e/hip/900", {@code prefix} = "p/" and + * E.g if {@code argsString} = "e/hip/900", {@code flag} = "p/" and * {@code fromIndex} = 0, this method returns -1 as there are no valid * occurrences of "p/" with whitespace before it. However, if - * {@code argsString} = "e/hi p/900", {@code prefix} = "p/" and + * {@code argsString} = "e/hi p/900", {@code flag} = "p/" and * {@code fromIndex} = 0, this method returns 5. */ - private static int findPrefixPosition(String argsString, String prefix, int fromIndex) { - int prefixIndex = argsString.indexOf(" " + prefix, fromIndex); - return prefixIndex == -1 ? -1 - : prefixIndex + 1; // +1 as offset for whitespace + private static int findFlagPosition(String argsString, String flag, int fromIndex) { + int flagIndex = argsString.indexOf(" " + flag, fromIndex); + return flagIndex == -1 ? -1 + : flagIndex + 1; // +1 as offset for whitespace } /** - * Extracts prefixes and their argument values, and returns an {@code ArgumentMultimap} object that maps the - * extracted prefixes to their respective arguments. Prefixes are extracted based on their zero-based positions in + * Extracts flag and their argument values, and returns an {@code ArgumentMultimap} object that maps the + * extracted flag to their respective arguments. Flags are extracted based on their zero-based positions in * {@code argsString}. * - * @param argsString Arguments string of the form: {@code preamble value value ...} - * @param prefixPositions Zero-based positions of all prefixes in {@code argsString} - * @return ArgumentMultimap object that maps prefixes to their arguments + * @param argsString Arguments string of the form: {@code preamble value value ...} + * @param flagPositions Zero-based positions of all flag in {@code argsString} + * @return ArgumentMultimap object that maps flag to their arguments */ - private static ArgumentMultimap extractArguments(String argsString, List prefixPositions) { + private static ArgumentMultimap extractArguments(String argsString, List flagPositions) { // Sort by start position - prefixPositions.sort((prefix1, prefix2) -> prefix1.getStartPosition() - prefix2.getStartPosition()); + flagPositions.sort((flag1, flag2) -> flag1.getStartPosition() - flag2.getStartPosition()); - // Insert a PrefixPosition to represent the preamble - PrefixPosition preambleMarker = new PrefixPosition(new Prefix(""), 0); - prefixPositions.add(0, preambleMarker); + // Insert a FlagPosition to represent the preamble + FlagPosition preambleMarker = new FlagPosition(new Flag("", null, null), 0); + flagPositions.add(0, preambleMarker); - // Add a dummy PrefixPosition to represent the end of the string - PrefixPosition endPositionMarker = new PrefixPosition(new Prefix(""), argsString.length()); - prefixPositions.add(endPositionMarker); + // Add a dummy FlagPosition to represent the end of the string + FlagPosition endPositionMarker = new FlagPosition(new Flag("", null, null), argsString.length()); + flagPositions.add(endPositionMarker); - // Map prefixes to their argument values (if any) + // Map flag to their argument values (if any) ArgumentMultimap argMultimap = new ArgumentMultimap(); - for (int i = 0; i < prefixPositions.size() - 1; i++) { - // Extract and store prefixes and their arguments - Prefix argPrefix = prefixPositions.get(i).getPrefix(); - String argValue = extractArgumentValue(argsString, prefixPositions.get(i), prefixPositions.get(i + 1)); - argMultimap.put(argPrefix, argValue); + for (int i = 0; i < flagPositions.size() - 1; i++) { + // Extract and store flag and their arguments + Flag argFlag = flagPositions.get(i).getFlag(); + String argValue = extractArgumentValue(argsString, flagPositions.get(i), flagPositions.get(i + 1)); + argMultimap.put(argFlag, argValue); } return argMultimap; } /** - * Returns the trimmed value of the argument in the arguments string specified by {@code currentPrefixPosition}. - * The end position of the value is determined by {@code nextPrefixPosition}. + * Returns the trimmed value of the argument in the arguments string specified by {@code currentFlagPosition}. + * The end position of the value is determined by {@code nextFlagPosition}. */ private static String extractArgumentValue(String argsString, - PrefixPosition currentPrefixPosition, - PrefixPosition nextPrefixPosition) { - Prefix prefix = currentPrefixPosition.getPrefix(); + FlagPosition currentFlagPosition, + FlagPosition nextFlagPosition + ) { + Flag flag = currentFlagPosition.getFlag(); - int valueStartPos = currentPrefixPosition.getStartPosition() + prefix.getPrefix().length(); - String value = argsString.substring(valueStartPos, nextPrefixPosition.getStartPosition()); + int valueStartPos = currentFlagPosition.getStartPosition() + flag.getFlagString().length(); + String value = argsString.substring(valueStartPos, nextFlagPosition.getStartPosition()); return value.trim(); } /** - * Represents a prefix's position in an arguments string. + * Represents a flag's position in an arguments string. */ - private static class PrefixPosition { + private static class FlagPosition { private int startPosition; - private final Prefix prefix; + private final Flag flag; - PrefixPosition(Prefix prefix, int startPosition) { - this.prefix = prefix; + FlagPosition(Flag flag, int startPosition) { + this.flag = flag; this.startPosition = startPosition; } @@ -140,8 +141,8 @@ int getStartPosition() { return startPosition; } - Prefix getPrefix() { - return prefix; + Flag getFlag() { + return flag; } } diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 75b1a9bf119..400c063a849 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -5,11 +5,11 @@ */ public class CliSyntax { - /* Prefix definitions */ - public static final Prefix PREFIX_NAME = new Prefix("n/"); - public static final Prefix PREFIX_PHONE = new Prefix("p/"); - public static final Prefix PREFIX_EMAIL = new Prefix("e/"); - public static final Prefix PREFIX_ADDRESS = new Prefix("a/"); - public static final Prefix PREFIX_TAG = new Prefix("t/"); + /* Flag definitions */ + public static final Flag FLAG_NAME = new Flag("name"); + public static final Flag FLAG_PHONE = new Flag("phone"); + public static final Flag FLAG_EMAIL = new Flag("email"); + public static final Flag FLAG_ADDRESS = new Flag("addr"); + public static final Flag FLAG_TAG = new Flag("tag"); } diff --git a/src/main/java/seedu/address/logic/parser/EditCommandParser.java b/src/main/java/seedu/address/logic/parser/EditCommandParser.java index 22848ca8c1a..f22cbf49f7c 100644 --- a/src/main/java/seedu/address/logic/parser/EditCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/EditCommandParser.java @@ -2,11 +2,11 @@ import static java.util.Objects.requireNonNull; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_NAME; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_TAG; import java.util.Collection; import java.util.Collections; @@ -32,7 +32,7 @@ public class EditCommandParser implements Parser { public EditCommand parse(String args) throws ParseException { requireNonNull(args); ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG); + ArgumentTokenizer.tokenize(args, FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS, FLAG_TAG); Index index; @@ -42,23 +42,23 @@ public EditCommand parse(String args) throws ParseException { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe); } - argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS); + argMultimap.verifyNoDuplicateFlagsFor(FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS); EditContactDescriptor editContactDescriptor = new EditContactDescriptor(); - if (argMultimap.getValue(PREFIX_NAME).isPresent()) { - editContactDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get())); + if (argMultimap.getValue(FLAG_NAME).isPresent()) { + editContactDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(FLAG_NAME).get())); } - if (argMultimap.getValue(PREFIX_PHONE).isPresent()) { - editContactDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get())); + if (argMultimap.getValue(FLAG_PHONE).isPresent()) { + editContactDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(FLAG_PHONE).get())); } - if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) { - editContactDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get())); + if (argMultimap.getValue(FLAG_EMAIL).isPresent()) { + editContactDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(FLAG_EMAIL).get())); } - if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) { - editContactDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get())); + if (argMultimap.getValue(FLAG_ADDRESS).isPresent()) { + editContactDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(FLAG_ADDRESS).get())); } - parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editContactDescriptor::setTags); + parseTagsForEdit(argMultimap.getAllValues(FLAG_TAG)).ifPresent(editContactDescriptor::setTags); if (!editContactDescriptor.isAnyFieldEdited()) { throw new ParseException(EditCommand.MESSAGE_NOT_EDITED); diff --git a/src/main/java/seedu/address/logic/parser/Flag.java b/src/main/java/seedu/address/logic/parser/Flag.java new file mode 100644 index 00000000000..95cadfca45a --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/Flag.java @@ -0,0 +1,100 @@ +package seedu.address.logic.parser; + +import java.util.Objects; + +/** + * A flag is an argument in and of itself. It functions as a option specifier, or as a marker for the beginning of a + * command argument. + * E.g. '--t' in 'add James --t friend'. + */ +public class Flag { + + public static final String DEFAULT_PREFIX = "--"; + public static final String DEFAULT_POSTFIX = ""; + + private final String name; + private final String prefix; + private final String postfix; + + /** + * Constructs a flag with the {@link #DEFAULT_PREFIX} and {@link #DEFAULT_POSTFIX} surrounding the name. + * If the name has any leading or trailing whitespace, it'll be trimmed. + * + * @param name The name of the flag. May be null, which will set it to an empty string. + */ + public Flag(String name) { + this(name, DEFAULT_PREFIX, DEFAULT_POSTFIX); + } + + /** + * Constructs a flag with a custom prefix and custom postfix. + * Any fields with leading or trailing whitespace are trimmed. + * + * @param name The name of the flag. May be null, which will set it to an empty string. + * @param prefix The prefix of the flag. May be null, which will set it to an empty string. + * @param postfix The postfix of the flag. May be null, which will set it to an empty string. + */ + public Flag(String name, String prefix, String postfix) { + this.name = name == null ? "" : name.trim(); + this.prefix = prefix == null ? "" : prefix.trim(); + this.postfix = postfix == null ? "" : postfix.trim(); + } + + public String getName() { + return name; + } + + public String getPrefix() { + return prefix; + } + + public String getPostfix() { + return postfix; + } + + + /** + * Returns the full string that would be used by the user to input a flag. + * Equivalent to calling {@link #toString()}. + */ + public String getFlagString() { + return this.toString(); + } + + /** + * Returns a string representation of this flag. + * + *

+ * This is the full string that would be used by a user to input a flag. + * This means it's the concatenated result of prefix, name, postfix together. + *

+ * + * @return The string representation of this flag. + */ + @Override + public String toString() { + return this.getPrefix() + this.getName() + this.getPostfix(); + } + + @Override + public int hashCode() { + return name == null ? 0 : name.hashCode(); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof Flag)) { + return false; + } + + Flag otherFlag = (Flag) other; + return Objects.equals(name, otherFlag.name) + && Objects.equals(prefix, otherFlag.prefix) + && Objects.equals(postfix, otherFlag.postfix); + } +} diff --git a/src/main/java/seedu/address/logic/parser/Prefix.java b/src/main/java/seedu/address/logic/parser/Prefix.java deleted file mode 100644 index 348b7686c8a..00000000000 --- a/src/main/java/seedu/address/logic/parser/Prefix.java +++ /dev/null @@ -1,42 +0,0 @@ -package seedu.address.logic.parser; - -/** - * A prefix that marks the beginning of an argument in an arguments string. - * E.g. 't/' in 'add James t/ friend'. - */ -public class Prefix { - private final String prefix; - - public Prefix(String prefix) { - this.prefix = prefix; - } - - public String getPrefix() { - return prefix; - } - - @Override - public String toString() { - return getPrefix(); - } - - @Override - public int hashCode() { - return prefix == null ? 0 : prefix.hashCode(); - } - - @Override - public boolean equals(Object other) { - if (other == this) { - return true; - } - - // instanceof handles nulls - if (!(other instanceof Prefix)) { - return false; - } - - Prefix otherPrefix = (Prefix) other; - return prefix.equals(otherPrefix.prefix); - } -} diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index a3fd2b3dc9e..e0f1406122d 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -2,11 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_NAME; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_TAG; import static seedu.address.testutil.Assert.assertThrows; import java.util.ArrayList; @@ -37,22 +37,22 @@ public class CommandTestUtil { public static final String VALID_TAG_HUSBAND = "husband"; public static final String VALID_TAG_FRIEND = "friend"; - public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY; - public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB; - public static final String PHONE_DESC_AMY = " " + PREFIX_PHONE + VALID_PHONE_AMY; - public static final String PHONE_DESC_BOB = " " + PREFIX_PHONE + VALID_PHONE_BOB; - public static final String EMAIL_DESC_AMY = " " + PREFIX_EMAIL + VALID_EMAIL_AMY; - public static final String EMAIL_DESC_BOB = " " + PREFIX_EMAIL + VALID_EMAIL_BOB; - public static final String ADDRESS_DESC_AMY = " " + PREFIX_ADDRESS + VALID_ADDRESS_AMY; - public static final String ADDRESS_DESC_BOB = " " + PREFIX_ADDRESS + VALID_ADDRESS_BOB; - public static final String TAG_DESC_FRIEND = " " + PREFIX_TAG + VALID_TAG_FRIEND; - public static final String TAG_DESC_HUSBAND = " " + PREFIX_TAG + VALID_TAG_HUSBAND; - - public static final String INVALID_NAME_DESC = " " + PREFIX_NAME + "James&"; // '&' not allowed in names - public static final String INVALID_PHONE_DESC = " " + PREFIX_PHONE + "911a"; // 'a' not allowed in phones - public static final String INVALID_EMAIL_DESC = " " + PREFIX_EMAIL + "bob!yahoo"; // missing '@' symbol - public static final String INVALID_ADDRESS_DESC = " " + PREFIX_ADDRESS; // empty string not allowed for addresses - public static final String INVALID_TAG_DESC = " " + PREFIX_TAG + "hubby*"; // '*' not allowed in tags + public static final String NAME_DESC_AMY = " " + FLAG_NAME + VALID_NAME_AMY; + public static final String NAME_DESC_BOB = " " + FLAG_NAME + VALID_NAME_BOB; + public static final String PHONE_DESC_AMY = " " + FLAG_PHONE + VALID_PHONE_AMY; + public static final String PHONE_DESC_BOB = " " + FLAG_PHONE + VALID_PHONE_BOB; + public static final String EMAIL_DESC_AMY = " " + FLAG_EMAIL + VALID_EMAIL_AMY; + public static final String EMAIL_DESC_BOB = " " + FLAG_EMAIL + VALID_EMAIL_BOB; + public static final String ADDRESS_DESC_AMY = " " + FLAG_ADDRESS + VALID_ADDRESS_AMY; + public static final String ADDRESS_DESC_BOB = " " + FLAG_ADDRESS + VALID_ADDRESS_BOB; + public static final String TAG_DESC_FRIEND = " " + FLAG_TAG + VALID_TAG_FRIEND; + public static final String TAG_DESC_HUSBAND = " " + FLAG_TAG + VALID_TAG_HUSBAND; + + public static final String INVALID_NAME_DESC = " " + FLAG_NAME + "James&"; // '&' not allowed in names + public static final String INVALID_PHONE_DESC = " " + FLAG_PHONE + "911a"; // 'a' not allowed in phones + public static final String INVALID_EMAIL_DESC = " " + FLAG_EMAIL + "bob!yahoo"; // missing '@' symbol + public static final String INVALID_ADDRESS_DESC = " " + FLAG_ADDRESS; // empty string not allowed for addresses + public static final String INVALID_TAG_DESC = " " + FLAG_TAG + "hubby*"; // '*' not allowed in tags public static final String PREAMBLE_WHITESPACE = "\t \r \n"; public static final String PREAMBLE_NON_EMPTY = "NonEmptyPreamble"; diff --git a/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java index bc4792f5ed2..6b20639266a 100644 --- a/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddCommandParserTest.java @@ -24,10 +24,10 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_FRIEND; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_NAME; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalContacts.AMY; @@ -72,61 +72,61 @@ public void parse_repeatedNonTagValue_failure() { // multiple names assertParseFailure(parser, NAME_DESC_AMY + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NAME)); + Messages.getErrorMessageForDuplicateFlags(FLAG_NAME)); // multiple phones assertParseFailure(parser, PHONE_DESC_AMY + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE)); + Messages.getErrorMessageForDuplicateFlags(FLAG_PHONE)); // multiple emails assertParseFailure(parser, EMAIL_DESC_AMY + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_EMAIL)); + Messages.getErrorMessageForDuplicateFlags(FLAG_EMAIL)); // multiple addresses assertParseFailure(parser, ADDRESS_DESC_AMY + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_ADDRESS)); + Messages.getErrorMessageForDuplicateFlags(FLAG_ADDRESS)); // multiple fields repeated assertParseFailure(parser, validExpectedContactString + PHONE_DESC_AMY + EMAIL_DESC_AMY + NAME_DESC_AMY + ADDRESS_DESC_AMY + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NAME, PREFIX_ADDRESS, PREFIX_EMAIL, PREFIX_PHONE)); + Messages.getErrorMessageForDuplicateFlags(FLAG_NAME, FLAG_ADDRESS, FLAG_EMAIL, FLAG_PHONE)); // invalid value followed by valid value // invalid name assertParseFailure(parser, INVALID_NAME_DESC + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NAME)); + Messages.getErrorMessageForDuplicateFlags(FLAG_NAME)); // invalid email assertParseFailure(parser, INVALID_EMAIL_DESC + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_EMAIL)); + Messages.getErrorMessageForDuplicateFlags(FLAG_EMAIL)); // invalid phone assertParseFailure(parser, INVALID_PHONE_DESC + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE)); + Messages.getErrorMessageForDuplicateFlags(FLAG_PHONE)); // invalid address assertParseFailure(parser, INVALID_ADDRESS_DESC + validExpectedContactString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_ADDRESS)); + Messages.getErrorMessageForDuplicateFlags(FLAG_ADDRESS)); // valid value followed by invalid value // invalid name assertParseFailure(parser, validExpectedContactString + INVALID_NAME_DESC, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NAME)); + Messages.getErrorMessageForDuplicateFlags(FLAG_NAME)); // invalid email assertParseFailure(parser, validExpectedContactString + INVALID_EMAIL_DESC, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_EMAIL)); + Messages.getErrorMessageForDuplicateFlags(FLAG_EMAIL)); // invalid phone assertParseFailure(parser, validExpectedContactString + INVALID_PHONE_DESC, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE)); + Messages.getErrorMessageForDuplicateFlags(FLAG_PHONE)); // invalid address assertParseFailure(parser, validExpectedContactString + INVALID_ADDRESS_DESC, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_ADDRESS)); + Messages.getErrorMessageForDuplicateFlags(FLAG_ADDRESS)); } @Test @@ -141,23 +141,23 @@ public void parse_optionalFieldsMissing_success() { public void parse_compulsoryFieldMissing_failure() { String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE); - // missing name prefix + // missing name flag assertParseFailure(parser, VALID_NAME_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB, expectedMessage); - // missing phone prefix + // missing phone flag assertParseFailure(parser, NAME_DESC_BOB + VALID_PHONE_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB, expectedMessage); - // missing email prefix + // missing email flag assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + VALID_EMAIL_BOB + ADDRESS_DESC_BOB, expectedMessage); - // missing address prefix + // missing address flag assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + VALID_ADDRESS_BOB, expectedMessage); - // all prefixes missing + // all flags missing assertParseFailure(parser, VALID_NAME_BOB + VALID_PHONE_BOB + VALID_EMAIL_BOB + VALID_ADDRESS_BOB, expectedMessage); } diff --git a/src/test/java/seedu/address/logic/parser/ArgumentTokenizerTest.java b/src/test/java/seedu/address/logic/parser/ArgumentTokenizerTest.java index c97308935f5..5b750d413e2 100644 --- a/src/test/java/seedu/address/logic/parser/ArgumentTokenizerTest.java +++ b/src/test/java/seedu/address/logic/parser/ArgumentTokenizerTest.java @@ -9,10 +9,10 @@ public class ArgumentTokenizerTest { - private final Prefix unknownPrefix = new Prefix("--u"); - private final Prefix pSlash = new Prefix("p/"); - private final Prefix dashT = new Prefix("-t"); - private final Prefix hatQ = new Prefix("^Q"); + private final Flag unknownFlag = new Flag("u", "--", null); + private final Flag pSlash = new Flag("p", null, "/"); + private final Flag dashT = new Flag("t", "-", null); + private final Flag hatQ = new Flag("Q", "^", null); @Test public void tokenize_emptyArgsString_noValues() { @@ -32,29 +32,29 @@ private void assertPreambleEmpty(ArgumentMultimap argMultimap) { } /** - * Asserts all the arguments in {@code argMultimap} with {@code prefix} match the {@code expectedValues} + * Asserts all the arguments in {@code argMultimap} with {@code flag} match the {@code expectedValues} * and only the last value is returned upon calling {@code ArgumentMultimap#getValue(Prefix)}. */ - private void assertArgumentPresent(ArgumentMultimap argMultimap, Prefix prefix, String... expectedValues) { + private void assertArgumentPresent(ArgumentMultimap argMultimap, Flag flag, String... expectedValues) { // Verify the last value is returned - assertEquals(expectedValues[expectedValues.length - 1], argMultimap.getValue(prefix).get()); + assertEquals(expectedValues[expectedValues.length - 1], argMultimap.getValue(flag).get()); // Verify the number of values returned is as expected - assertEquals(expectedValues.length, argMultimap.getAllValues(prefix).size()); + assertEquals(expectedValues.length, argMultimap.getAllValues(flag).size()); // Verify all values returned are as expected and in order for (int i = 0; i < expectedValues.length; i++) { - assertEquals(expectedValues[i], argMultimap.getAllValues(prefix).get(i)); + assertEquals(expectedValues[i], argMultimap.getAllValues(flag).get(i)); } } - private void assertArgumentAbsent(ArgumentMultimap argMultimap, Prefix prefix) { - assertFalse(argMultimap.getValue(prefix).isPresent()); + private void assertArgumentAbsent(ArgumentMultimap argMultimap, Flag flag) { + assertFalse(argMultimap.getValue(flag).isPresent()); } @Test - public void tokenize_noPrefixes_allTakenAsPreamble() { + public void tokenize_noFlags_allTakenAsPreamble() { String argsString = " some random string /t tag with leading and trailing spaces "; ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(argsString); @@ -106,13 +106,13 @@ public void tokenize_multipleArguments() { assertPreambleEmpty(argMultimap); assertArgumentAbsent(argMultimap, pSlash); - /* Also covers: testing for prefixes not specified as a prefix */ + /* Also covers: testing for flags not specified as a flag */ // Prefixes not previously given to the tokenizer should not return any values - argsString = unknownPrefix + "some value"; + argsString = unknownFlag + "some value"; argMultimap = ArgumentTokenizer.tokenize(argsString, pSlash, dashT, hatQ); - assertArgumentAbsent(argMultimap, unknownPrefix); - assertPreamblePresent(argMultimap, argsString); // Unknown prefix is taken as part of preamble + assertArgumentAbsent(argMultimap, unknownFlag); + assertPreamblePresent(argMultimap, argsString); // Unknown flag is taken as part of preamble } @Test @@ -138,13 +138,14 @@ public void tokenize_multipleArgumentsJoined() { @Test public void equalsMethod() { - Prefix aaa = new Prefix("aaa"); + Flag aaa = new Flag("aaa", "-", ""); assertEquals(aaa, aaa); - assertEquals(aaa, new Prefix("aaa")); + assertEquals(aaa, new Flag("aaa", "-", null)); - assertNotEquals(aaa, "aaa"); - assertNotEquals(aaa, new Prefix("aab")); + assertNotEquals(aaa, "-aaa"); + assertNotEquals(aaa, new Flag("aab", "-", null)); + assertNotEquals(aaa, new Flag("aaa", null, "/")); } } diff --git a/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java b/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java index a16f9c254b1..0c2d08c3eb6 100644 --- a/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java @@ -22,10 +22,10 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_FRIEND; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; -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_TAG; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_TAG; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_CONTACT; @@ -47,7 +47,7 @@ public class EditCommandParserTest { - private static final String TAG_EMPTY = " " + PREFIX_TAG; + private static final String TAG_EMPTY = " " + FLAG_TAG; private static final String MESSAGE_INVALID_FORMAT = String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE); @@ -77,7 +77,7 @@ public void parse_invalidPreamble_failure() { // invalid arguments being parsed as preamble assertParseFailure(parser, "1 some random string", MESSAGE_INVALID_FORMAT); - // invalid prefix being parsed as preamble + // invalid flag being parsed as preamble assertParseFailure(parser, "1 i/ string", MESSAGE_INVALID_FORMAT); } @@ -172,12 +172,12 @@ public void parse_multipleRepeatedFields_failure() { Index targetIndex = INDEX_FIRST_CONTACT; String userInput = targetIndex.getOneBased() + INVALID_PHONE_DESC + PHONE_DESC_BOB; - assertParseFailure(parser, userInput, Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE)); + assertParseFailure(parser, userInput, Messages.getErrorMessageForDuplicateFlags(FLAG_PHONE)); // invalid followed by valid userInput = targetIndex.getOneBased() + PHONE_DESC_BOB + INVALID_PHONE_DESC; - assertParseFailure(parser, userInput, Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE)); + assertParseFailure(parser, userInput, Messages.getErrorMessageForDuplicateFlags(FLAG_PHONE)); // mulltiple valid fields repeated userInput = targetIndex.getOneBased() + PHONE_DESC_AMY + ADDRESS_DESC_AMY + EMAIL_DESC_AMY @@ -185,14 +185,14 @@ public void parse_multipleRepeatedFields_failure() { + PHONE_DESC_BOB + ADDRESS_DESC_BOB + EMAIL_DESC_BOB + TAG_DESC_HUSBAND; assertParseFailure(parser, userInput, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS)); + Messages.getErrorMessageForDuplicateFlags(FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS)); // multiple invalid values userInput = targetIndex.getOneBased() + INVALID_PHONE_DESC + INVALID_ADDRESS_DESC + INVALID_EMAIL_DESC + INVALID_PHONE_DESC + INVALID_ADDRESS_DESC + INVALID_EMAIL_DESC; assertParseFailure(parser, userInput, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS)); + Messages.getErrorMessageForDuplicateFlags(FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS)); } @Test diff --git a/src/test/java/seedu/address/testutil/ContactUtil.java b/src/test/java/seedu/address/testutil/ContactUtil.java index 069e03b189b..0c2c1f3e1da 100644 --- a/src/test/java/seedu/address/testutil/ContactUtil.java +++ b/src/test/java/seedu/address/testutil/ContactUtil.java @@ -1,10 +1,10 @@ package seedu.address.testutil; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.FLAG_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL; +import static seedu.address.logic.parser.CliSyntax.FLAG_NAME; +import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE; +import static seedu.address.logic.parser.CliSyntax.FLAG_TAG; import java.util.Set; @@ -30,12 +30,12 @@ public static String getAddCommand(Contact contact) { */ public static String getContactDetails(Contact contact) { StringBuilder sb = new StringBuilder(); - sb.append(PREFIX_NAME + contact.getName().fullName + " "); - sb.append(PREFIX_PHONE + contact.getPhone().value + " "); - sb.append(PREFIX_EMAIL + contact.getEmail().value + " "); - sb.append(PREFIX_ADDRESS + contact.getAddress().value + " "); + sb.append(FLAG_NAME + contact.getName().fullName + " "); + sb.append(FLAG_PHONE + contact.getPhone().value + " "); + sb.append(FLAG_EMAIL + contact.getEmail().value + " "); + sb.append(FLAG_ADDRESS + contact.getAddress().value + " "); contact.getTags().stream().forEach( - s -> sb.append(PREFIX_TAG + s.tagName + " ") + s -> sb.append(FLAG_TAG + s.tagName + " ") ); return sb.toString(); } @@ -45,16 +45,16 @@ public static String getContactDetails(Contact contact) { */ public static String getEditContactDescriptorDetails(EditContactDescriptor descriptor) { StringBuilder sb = new StringBuilder(); - descriptor.getName().ifPresent(name -> sb.append(PREFIX_NAME).append(name.fullName).append(" ")); - descriptor.getPhone().ifPresent(phone -> sb.append(PREFIX_PHONE).append(phone.value).append(" ")); - descriptor.getEmail().ifPresent(email -> sb.append(PREFIX_EMAIL).append(email.value).append(" ")); - descriptor.getAddress().ifPresent(address -> sb.append(PREFIX_ADDRESS).append(address.value).append(" ")); + descriptor.getName().ifPresent(name -> sb.append(FLAG_NAME).append(name.fullName).append(" ")); + descriptor.getPhone().ifPresent(phone -> sb.append(FLAG_PHONE).append(phone.value).append(" ")); + descriptor.getEmail().ifPresent(email -> sb.append(FLAG_EMAIL).append(email.value).append(" ")); + descriptor.getAddress().ifPresent(address -> sb.append(FLAG_ADDRESS).append(address.value).append(" ")); if (descriptor.getTags().isPresent()) { Set tags = descriptor.getTags().get(); if (tags.isEmpty()) { - sb.append(PREFIX_TAG); + sb.append(FLAG_TAG); } else { - tags.forEach(s -> sb.append(PREFIX_TAG).append(s.tagName).append(" ")); + tags.forEach(s -> sb.append(FLAG_TAG).append(s.tagName).append(" ")); } } return sb.toString();