I currently work on a parser. I walk a tree, most of it being quite determinist (I have a finite number of values I can find).
Usually in these cases, I create an enum with the name of the value I expect to find, like this :
public enum myElements{
like,
not_like,// "not like" is transformed in not_like by parser
exists;
}
in this case, I can check whether a string belongs to the enum this way :
String my_string;
myElements.valueOf(my_string);
The thing is that currently I work with special characters ({“>”, “<“, “<>”, …)
It is not possible to create enum value with special characters.
What I would like is to associate an enum with those special characters elements, something like
public enum myElements{
greather_than(">"),
smaller_than("<")
}
and continue using something as straightforward as:
myElements.valueOf(my_string);
For the moments I use enums everywhere but for these elements, where I use of table. I’d like to harmonize this
BTW, I work with Java6, due to some dependancies.
An idea is welcome,
Thanks !
EDIT :
Here is an example of the current table version I use :
public static String[] compElements = {">", "<", "<>", "=", "<=", ">="};
with the equivalent for the valueOf :
Arrays.asList(compElements).contains(my_string);
It sounds like you just want your enum to store a map of all the “flexible” names, mapping the name to the enum value. You wouldn’t be able to use
valueOf, but you could easily write your own static method to return the relevant mapped value. Create the map in a static initializer block. (Don’t forget that when the constructors are called automatically, static initializers will not have executed.)Sample code: