-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
megamek/src/megamek/server/commands/arguments/OptionalEnumArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package megamek.server.commands.arguments; | ||
|
||
import megamek.client.ui.Messages; | ||
|
||
import java.util.Arrays; | ||
|
||
public class OptionalEnumArgument<E extends Enum<E>> extends EnumArgument<E> { | ||
|
||
public OptionalEnumArgument(String name, String description, Class<E> enumType) { | ||
super(name, description, enumType, null); | ||
} | ||
|
||
@Override | ||
public void parse(String input) throws IllegalArgumentException { | ||
if (input == null) { | ||
return; | ||
} | ||
try { | ||
value = enumType.getEnumConstants()[Integer.parseInt(input)]; | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException(getName() + " must be one of: " + getEnumConstantsString()); | ||
} | ||
} | ||
|
||
public boolean isPresent() { | ||
return value != null; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return value == null; | ||
} | ||
|
||
private String getEnumConstantsString() { | ||
var sb = new StringBuilder(); | ||
for (int i = 0; i < enumType.getEnumConstants().length; i++) { | ||
sb.append(i).append(": ").append(enumType.getEnumConstants()[i]); | ||
if (i < enumType.getEnumConstants().length - 1) { | ||
sb.append(", "); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public String getHelp() { | ||
return getDescription() + | ||
" [" + getEnumConstantsString() + "] " + | ||
(defaultValue != null ? " [default: " + defaultValue + "]. " : ". ") + | ||
Messages.getString("Gamemaster.cmd.params.optional"); | ||
} | ||
|
||
} |