I will often need to translate from the string code into its Enum
type.
See snippet below. I think this is highly inefficient. How can I improve
the method deref
so that:
- It will be threadsafe.
- It is faster.
Code:
public enum SigninErrorCodes
{
InvalidUser("a0"), InvalidPassword("b5"), NoServerResponse("s2");
SigninErrorCodes( String code ) { _code = code; }
public String code() { return _code; }
public static SigninErrorCodes deref( String code )
{
SigninErrorCodes[] verbs = values();
Map<String, SigninErrorCodes> m = new HashMap<String,SigninErrorCodes>(3);
for( int i=0; i<verbs.length; i++ )
m.put( verbs[i].code(), verbs[i] );
return m.get( code );
}
private final String _code;
}