Skip to content

Commit

Permalink
Merge branch 'branch-BCD-Extension'
Browse files Browse the repository at this point in the history
  • Loading branch information
C5hives committed Sep 18, 2024
2 parents c338338 + 79d4ec3 commit 0f27ba5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/main/java/lawrence/command/CommandType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lawrence.command;

import java.util.ArrayList;

/**
* Represents the different commands that can be issued by the user.
*/
Expand Down Expand Up @@ -30,27 +32,41 @@ public enum CommandType {
}

/**
* Converts a text string into its relevant enum counterpart.
* Converts a text string into its relevant enum counterpart. Full string matching
* and partial string matching are both done.
* <p>
* If input does not match any known types, the {@link #INVALID} type
* is returned.
* IF the input matches multiple types, the {@link #INVALID} type is also returned.
* </p>
*
* @param input the text containing an enum value
* @return an enum type matching the input
*/
public static CommandType fromString(String input) {
ArrayList<CommandType> matches = new ArrayList<>();
for (CommandType type : CommandType.values()) {
// do not attempt to match with type INVALID as it is reserved to signify a failed parse
if (type == INVALID) {
continue;
}

if (type.getCommandType().equalsIgnoreCase(input)) {
return type;
String typeString = type.getCommandType();
if (typeString.equalsIgnoreCase(input)) {
matches.add(type);
continue;
}

// Check for partial match
if (typeString.startsWith(input.toLowerCase())) {
matches.add(type);
}
}
return INVALID;

if (matches.size() != 1) {
return INVALID;
}
return matches.get(0);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/lawrence/parser/CommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void createCommand_invalidCommand_throwsIllegalArgumentException() {
CommandParser.createCommand("blah blah blah");
fail();
} catch (IllegalArgumentException e) {
assertEquals("No command type found for: blah.", e.getMessage());
assertEquals("Unknown command: blah.", e.getMessage());
}
}

Expand Down

0 comments on commit 0f27ba5

Please sign in to comment.