Skip to content

Commit

Permalink
Merge pull request #238 from yucongkoo/branch-prefix-enhancement
Browse files Browse the repository at this point in the history
Updated Prefix to include a secondaryPrefix
  • Loading branch information
TehOPanas authored Nov 3, 2023
2 parents 91cfd8c + 0113c97 commit d0dd51e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 143 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref
assert duplicatePrefixes.length > 0;

Set<String> duplicateFields =
Stream.of(duplicatePrefixes).map(Prefix::toString).collect(Collectors.toSet());
Stream.of(duplicatePrefixes).map(Prefix::getDisplayMessage).collect(Collectors.toSet());

return MESSAGE_DUPLICATE_FIELDS + String.join(" ", duplicateFields);
}
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public Command parseCommand(String userInput) throws ParseException {
final String arguments = matcher.group("arguments");

// Convert all Uppercase to lowercase
final String processedArguments = ArgumentTokenizer.preprocessArgsString(arguments);
final String processedCommandWord = commandWord.toLowerCase();

// Note to developers: Change the log level in config.json to enable lower level (i.e., FINE, FINER and lower)
Expand All @@ -66,19 +65,19 @@ public Command parseCommand(String userInput) throws ParseException {
switch (processedCommandWord) {

case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(processedArguments);
return new AddCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(processedArguments);
return new EditCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(processedArguments);
return new DeleteCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(processedArguments);
return new FindCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
return new ListCommand();
Expand All @@ -90,28 +89,28 @@ public Command parseCommand(String userInput) throws ParseException {
return new HelpCommand();

case RemarkCommand.COMMAND_WORD:
return new RemarkCommandParser().parse(processedArguments);
return new RemarkCommandParser().parse(arguments);

case TagCommand.COMMAND_WORD:
return new TagCommandParser().parse(processedArguments);
return new TagCommandParser().parse(arguments);

case PriorityCommand.COMMAND_WORD:
return new PriorityCommandParser().parse(processedArguments);
return new PriorityCommandParser().parse(arguments);

case InsuranceCommand.COMMAND_WORD:
return new InsuranceCommandParser().parse(processedArguments);
return new InsuranceCommandParser().parse(arguments);

case AppointmentCommand.COMMAND_WORD:
return new AppointmentCommandParser().parse(processedArguments);
return new AppointmentCommandParser().parse(arguments);

case DeleteAppointmentCommand.COMMAND_WORD:
return new DeleteAppointmentCommandParser().parse(processedArguments);
return new DeleteAppointmentCommandParser().parse(arguments);

case MarkAppointmentCommand.COMMAND_WORD:
return new MarkAppointmentCommandParser().parse(processedArguments);
return new MarkAppointmentCommandParser().parse(arguments);

case UnmarkAppointmentCommand.COMMAND_WORD:
return new UnmarkAppointmentCommandParser().parse(processedArguments);
return new UnmarkAppointmentCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
Expand Down
100 changes: 41 additions & 59 deletions src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADD_INSURANCE_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADD_TAG_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_APPOINTMENT_TIME_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_APPOINTMENT_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_APPOINTMENT_VENUE_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DELETE_INSURANCE_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DELETE_TAG_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMPTY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INSURANCE_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRIORITY_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK_UPPER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG_UPPER;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -46,33 +30,6 @@ public static ArgumentMultimap tokenize(String argsString, Prefix... prefixes) {
return extractArguments(argsString, positions);
}

/**
* Converts all uppercase {@code Prefix} to lowercase {@code Prefix}
*
* @param argsString arguments that may contain uppercase prefixes
* @return processed arguments containing only lowercase prefixes
*/
public static String preprocessArgsString(String argsString) {
requireNonNull(argsString);

Prefix[] uppercasePrefixes = new Prefix[] { PREFIX_NAME_UPPER, PREFIX_PHONE_UPPER,
PREFIX_EMAIL_UPPER, PREFIX_PRIORITY_UPPER, PREFIX_TAG_UPPER, PREFIX_REMARK_UPPER,
PREFIX_INSURANCE_UPPER, PREFIX_ADDRESS_UPPER, PREFIX_ADD_INSURANCE_UPPER,
PREFIX_DELETE_INSURANCE_UPPER, PREFIX_ADD_TAG_UPPER, PREFIX_DELETE_TAG_UPPER, PREFIX_APPOINTMENT_UPPER,
PREFIX_APPOINTMENT_TIME_UPPER, PREFIX_APPOINTMENT_VENUE_UPPER};

String processedArgs = argsString;

for (Prefix p : uppercasePrefixes) {
String upperPrefix = p.getPrefix();
String lowerPrefix = upperPrefix.toLowerCase();

processedArgs = processedArgs.replaceAll(" " + upperPrefix, " " + lowerPrefix);
}

return processedArgs;
}

/**
* Finds all zero-based prefix positions in the given arguments string.
*
Expand All @@ -92,32 +49,55 @@ private static List<PrefixPosition> findAllPrefixPositions(String argsString, Pr
private static List<PrefixPosition> findPrefixPositions(String argsString, Prefix prefix) {
List<PrefixPosition> 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);
PrefixPosition prefixPosition = findPrefixPosition(argsString, prefix, 0);
while (prefixPosition != null) {
positions.add(prefixPosition);
prefixPosition = findPrefixPosition(argsString, prefix, prefixPosition.getEndPosition());
}

return positions;
}

/**
* Returns the index of the first occurrence of {@code prefix} in
* Returns the {@code PrefixPosition} of the first occurrence of {@code prefix} 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 prefix}. Returns null if no
* such occurrence can be found.
*
* E.g if {@code argsString} = "e/hip/900", {@code prefix} = "p/" and
* {@code fromIndex} = 0, this method returns -1 as there are no valid
* {@code fromIndex} = 0, this method returns null 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 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 PrefixPosition findPrefixPosition(String argsString, Prefix prefix, int fromIndex) {
String convertedArgsString = argsString.toLowerCase();

int prefixIndex = convertedArgsString.indexOf(" " + prefix.getPrefix(), fromIndex);
int secondaryPrefixIndex = convertedArgsString.indexOf(" " + prefix.getSecondaryPrefix(), fromIndex);

if (prefixIndex == -1 && secondaryPrefixIndex == -1) {
return null;
}

if (prefixIndex == -1) {
return new PrefixPosition(prefix, secondaryPrefixIndex + 1,
secondaryPrefixIndex + 1 + prefix.getSecondaryPrefixLength());
}

if (secondaryPrefixIndex == -1) {
return new PrefixPosition(prefix, prefixIndex + 1,
prefixIndex + 1 + prefix.getPrefixLength());
}

// both indexes are present
if (prefixIndex < secondaryPrefixIndex) {
return new PrefixPosition(prefix, prefixIndex + 1,
prefixIndex + 1 + prefix.getPrefixLength());
} else {
return new PrefixPosition(prefix, secondaryPrefixIndex + 1,
secondaryPrefixIndex + 1 + prefix.getSecondaryPrefixLength());
}
}

/**
Expand All @@ -135,11 +115,11 @@ private static ArgumentMultimap extractArguments(String argsString, List<PrefixP
prefixPositions.sort((prefix1, prefix2) -> prefix1.getStartPosition() - prefix2.getStartPosition());

// Insert a PrefixPosition to represent the preamble
PrefixPosition preambleMarker = new PrefixPosition(PREFIX_EMPTY, 0);
PrefixPosition preambleMarker = new PrefixPosition(PREFIX_EMPTY, 0, 0);
prefixPositions.add(0, preambleMarker);

// Add a dummy PrefixPosition to represent the end of the string
PrefixPosition endPositionMarker = new PrefixPosition(PREFIX_EMPTY, argsString.length());
PrefixPosition endPositionMarker = new PrefixPosition(PREFIX_EMPTY, argsString.length(), argsString.length());
prefixPositions.add(endPositionMarker);

// Map prefixes to their argument values (if any)
Expand Down Expand Up @@ -173,11 +153,13 @@ private static String extractArgumentValue(String argsString,
*/
private static class PrefixPosition {
private int startPosition;
private int endPosition;
private final Prefix prefix;

PrefixPosition(Prefix prefix, int startPosition) {
PrefixPosition(Prefix prefix, int startPosition, int endPosition) {
this.prefix = prefix;
this.startPosition = startPosition;
this.endPosition = endPosition;
}

int getStartPosition() {
Expand All @@ -193,7 +175,7 @@ Prefix getPrefix() {
* Returns the end position of the prefix.
*/
int getEndPosition() {
return startPosition + prefix.getPrefixLength();
return endPosition;
}
}

Expand Down
49 changes: 16 additions & 33 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,20 @@
public class CliSyntax {

/* Prefix definitions */
public static final Prefix PREFIX_EMPTY = new Prefix("");
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/");
public static final Prefix PREFIX_REMARK = new Prefix("r/");
public static final Prefix PREFIX_ADD_TAG = new Prefix("at/");
public static final Prefix PREFIX_DELETE_TAG = new Prefix("dt/");
public static final Prefix PREFIX_INSURANCE = new Prefix("i/");
public static final Prefix PREFIX_ADD_INSURANCE = new Prefix("ai/");
public static final Prefix PREFIX_DELETE_INSURANCE = new Prefix("di/");
public static final Prefix PREFIX_PRIORITY = new Prefix("pr/");
public static final Prefix PREFIX_APPOINTMENT = new Prefix("d/");
public static final Prefix PREFIX_APPOINTMENT_TIME = new Prefix("t/");
public static final Prefix PREFIX_APPOINTMENT_VENUE = new Prefix("v/");

// Lower case prefixes
public static final Prefix PREFIX_NAME_UPPER = new Prefix("N/");
public static final Prefix PREFIX_PHONE_UPPER = new Prefix("P/");
public static final Prefix PREFIX_EMAIL_UPPER = new Prefix("E/");
public static final Prefix PREFIX_ADDRESS_UPPER = new Prefix("A/");
public static final Prefix PREFIX_TAG_UPPER = new Prefix("T/");
public static final Prefix PREFIX_REMARK_UPPER = new Prefix("R/");
public static final Prefix PREFIX_ADD_TAG_UPPER = new Prefix("AT/");
public static final Prefix PREFIX_DELETE_TAG_UPPER = new Prefix("DT/");
public static final Prefix PREFIX_INSURANCE_UPPER = new Prefix("I/");
public static final Prefix PREFIX_ADD_INSURANCE_UPPER = new Prefix("AI/");
public static final Prefix PREFIX_DELETE_INSURANCE_UPPER = new Prefix("DI/");
public static final Prefix PREFIX_PRIORITY_UPPER = new Prefix("PR/");
public static final Prefix PREFIX_APPOINTMENT_UPPER = new Prefix("D/");
public static final Prefix PREFIX_APPOINTMENT_TIME_UPPER = new Prefix("T/");
public static final Prefix PREFIX_APPOINTMENT_VENUE_UPPER = new Prefix("V/");
public static final Prefix PREFIX_EMPTY = new Prefix("", "");
public static final Prefix PREFIX_NAME = new Prefix("n/", "name/");
public static final Prefix PREFIX_PHONE = new Prefix("p/", "phone/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/", "email/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/", "address/");
public static final Prefix PREFIX_TAG = new Prefix("t/", "tag/");
public static final Prefix PREFIX_REMARK = new Prefix("r/", "remark/");
public static final Prefix PREFIX_ADD_TAG = new Prefix("at/", "addtag/");
public static final Prefix PREFIX_DELETE_TAG = new Prefix("dt/", "deletetag/");
public static final Prefix PREFIX_INSURANCE = new Prefix("i/", "insurance/");
public static final Prefix PREFIX_ADD_INSURANCE = new Prefix("ai/", "addinsurance/");
public static final Prefix PREFIX_DELETE_INSURANCE = new Prefix("di/", "deleteinsurance/");
public static final Prefix PREFIX_PRIORITY = new Prefix("pr/", "priority/");
public static final Prefix PREFIX_APPOINTMENT = new Prefix("d/", "date/");
public static final Prefix PREFIX_APPOINTMENT_TIME = new Prefix("t/", "time/");
public static final Prefix PREFIX_APPOINTMENT_VENUE = new Prefix("v/", "venue/");
}
32 changes: 30 additions & 2 deletions src/main/java/seedu/address/logic/parser/Prefix.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
package seedu.address.logic.parser;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

/**
* 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;
private final String secondaryPrefix;

/**
* Constructs a {@code Prefix} with {@code prefix} and {@code secondaryPrefix}.
*/
public Prefix(String prefix, String secondaryPrefix) {
requireAllNonNull(prefix, secondaryPrefix);

public Prefix(String prefix) {
this.prefix = prefix;
this.prefix = prefix.toLowerCase();
this.secondaryPrefix = secondaryPrefix.toLowerCase();
}

public String getPrefix() {
return prefix;
}

public String getSecondaryPrefix() {
return secondaryPrefix;
}

/**
* Returns the length of the prefix.
*/
public int getPrefixLength() {
return prefix.length();
}

/**
* Returns the length of the secondaryPrefix.
*/
public int getSecondaryPrefixLength() {
return secondaryPrefix.length();
}

/**
* Returns a formatter display message for this {@code Prefix}.
* @return
*/
public String getDisplayMessage() {
return String.format("%s (%s)", prefix, secondaryPrefix);
}

@Override
public String toString() {
return getPrefix();
Expand Down
Loading

0 comments on commit d0dd51e

Please sign in to comment.