Skip to content

Commit

Permalink
Change command type parsing to be flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
C5hives committed Sep 18, 2024
1 parent c338338 commit f66473e
Showing 1 changed file with 20 additions and 4 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

0 comments on commit f66473e

Please sign in to comment.