I am writing a flat file parser that reads token/value pairs using a Scanner. The files being read contain the token “class”. The token is later used in a switch statement, and uses the (pre Java 7) valueOf(token) Java idiom to produce an enum value. (I am using Java6 for compatibility with GWT.) As a workaround, I am using uppercase values in the enum, and valueOf(token.toUpperCase()).
public enum ParseTags {
CODE, CLASS, INSTRUCTOR, HOURS;
}
// . . .
token = scanner.next();
value = scanner.next();
switch (ParseTags.valueOf(token.toUpperCase())) {
case CODE:
entry.setCode(value);
break;
case CLASS:
entry.setClass(value);
break;
Because this is being compiled into javascript, I want to avoid the extra “toUpperCase()” operation on each iteration; not sure what performance will be on target platform. Is there a more graceful way to represent reserved words in an enumeration? This would be handled well by Java7’s switch on String, but again, I am confined to Java6sdk.
What you’re doing right now is the preferred way to do it. I would be extraordinarily shocked if the
toUpperCasewere a bottleneck.That said, I might consider something like
so you can do