Skip to content

Instantly share code, notes, and snippets.

@patrickmmartin
Created December 21, 2012 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickmmartin/4352311 to your computer and use it in GitHub Desktop.
Save patrickmmartin/4352311 to your computer and use it in GitHub Desktop.
I was experimenting with the Java enum constructors
/**
*
*/
package martin.patrick;
import java.util.Arrays;
/**
* @author Patrick
*
* Command concept for the interpreter commands
*
*/
public class Command {
// instance variables
private CommandType _commandType;
private String[] _tokens = {};
private String[] _params = {};
public String[] getTokens () { return _tokens; }
public String[] getParams () { return _params; }
public CommandType getCommandType () { return _commandType; }
public boolean isValid() {
return _commandType.validate(_tokens);
}
private Command(final CommandType commandType, final String[] tokens) {
// set the properties
_commandType = commandType;
// store for reference
_tokens = tokens;
// if there are parameters, store them
if (0 != _tokens.length)
_params = Arrays.copyOfRange(_tokens, 1, _tokens.length);
};
public static Command commandFromString(String commandStr) {
String[] tokens = commandStr.split("\\s+");
CommandType commandType = CommandType.ERROR;
if (tokens.length > 0 ) {
if (1 == tokens[0].length()) {
// identify
commandType = CommandType.fromChar(tokens[0].charAt(0));
// check is valid
if (!commandType.validate(tokens)) {
commandType = CommandType.ERROR;
}
//
}
}
return new Command(commandType, tokens);
}
public static enum ParamType {CHAR, INT, STRING }
public static enum CommandType {
START (' '),
EXIT ('X'),
INITIALISE ('I', ParamType.INT, ParamType.INT),
RUN ('R', ParamType.STRING),
CLEAR ('C'),
COLOUR ('L', ParamType.INT, ParamType.INT, ParamType.CHAR),
HLINE ('H', ParamType.INT, ParamType.INT, ParamType.INT, ParamType.CHAR),
VLINE ('V', ParamType.INT, ParamType.INT, ParamType.INT, ParamType.CHAR),
FILL ('F', ParamType.INT, ParamType.INT, ParamType.CHAR),
SHOW ('S'),
ERROR (' ') {
@Override
public boolean validate(String[] tokens) {
//NB: invalid by design
return false;
}
};
private final char _charCode;
private ParamType[] _paramtypes;
CommandType (final char charCode, ParamType... paramtypes) {
_charCode = charCode;
_paramtypes = paramtypes;
}
public boolean validate(String[] tokens) {
// base validation on length - tokens includes the command char
int paramlength =(null == _paramtypes)?0:_paramtypes.length;
// check on paramtypes length
boolean valid = (paramlength == tokens.length - 1);
// proceed to checking types
if (paramlength > 0 ) {
int i = 0;
for (ParamType p : _paramtypes) {
i++;
switch (p) {
case CHAR:
valid = (1 == tokens[i].length()) & (Character.isLetter(tokens[i].charAt(0)));
break;
case INT:
try {
Integer.parseInt(tokens[i]);
}
catch (NumberFormatException e) {
valid = false;
}
break;
case STRING:
//NB: check for string requires anything?
break;
}
// we can exit early if anything is invalid
if (!valid)
break;
}
}
return valid;
}
public char charCode() { return _charCode ; };
public static CommandType fromChar(final char code) {
for (CommandType c : CommandType.values()) {
if (code == (c.charCode()))
return c;
}
return ERROR;
}
} // CommandType
} // Command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment