Reputation: 1517
I have enum like :
class enum ErrorType(value:String){
TYPE_A("typeA"), TYPE_B("typeB"), TYPE_C("typeC"), UNKNOWN("unknown")
}
I want to get the appropriate Enum relative to the value given, for example :
val errorType = ErrorType.valueOf("typeB")
but it returns an exception. How should I proceed ?
Upvotes: 0
Views: 135
Reputation: 334
I use a lookup method (I use it so much I have it as a template:))
public enum ErrorType
{
TYPE_A("typeA"),
TYPE_B("typeB"),
TYPE_C("typeC"),
UNKNOWN("unknown");
private String ivLookup;
private ErrorType( String lookup )
{
ivLookup = lookup;
}
public String getLookup()
{
return ivLookup;
}
/**
* Return the enum value for the given lookup key
*/
public static ErrorType getErrorType( String lookUp ) throws EnumNotFoundException
{
if (lookUp != null)
for (ErrorType type : ErrorType.values())
if (type.getLookup().equals( lookUp ))
return type;
throw new EnumNotFoundException( ErrorType.class.getName(), lookUp );
}
}
The EnumNotFoundException
is my own.
If you don't want the overhead of looping through the enum values, and/or you have a lot of enum values:
public enum ErrorType
{
TYPE_A("typeA"),
TYPE_B("typeB"),
TYPE_C("typeC"),
UNKNOWN("unknown");
private String ivLookup;
private static HashMap<String, ErrorType> ivMap = null;
private ErrorType( String lookup )
{
ivLookup = lookup;
}
public String getLookup()
{
return ivLookup;
}
/**
* Return the enum value for the given lookup key
*/
public static ErrorType getErrorType( String lookUp ) throws EnumNotFoundException
{
ErrorType errorType = null;
if (ivMap == null)
{
ivMap = new HashMap<>();
for (ErrorType type : ErrorType.values())
ivMap.put( type.getLookup(), type );
}
errorType = ivMap.get( lookUp );
if (errorType != null)
return errorType;
throw new EnumNotFoundException( ErrorType.class.getName(), lookUp );
}
}
And you use it:
ErrorType errorType = ErrorType.getErrorType("typeB");
With a try/catch of course
Upvotes: 0
Reputation: 93609
valueOf
finds the Enum by its instance name, which in your example is "TYPE_B"
, not "typeB"
.
To use the value
property to find the associated Enum, you have to manually do the search. There's no built-in shortcut for this because the compiler doesn't know how the properties of any particular Enum will be used.
val errorType = ErrorType.values().first { it.value == "typeB" }
If you want to have UNKNOWN
be a fallback for invalid Strings instead of throwing:
val errorType = ErrorType.values().firstOrNull { it.value == "typeB" } ?: ErrorType.UNKNOWN
Upvotes: 4
Reputation: 466
Please send minimal compilable code - class enum ErrorType
is no valid java code.
Provided your constructor parameter value is backed by a private member errorValue, in your enum you could implement a static method
public static ErrorType getValueOf(String value){
for(ErrorType et:ErrorType.values()){
if(et.errorValue.equals(value){
return et;
}
}
return ErrorType.UNKOWN;
}
Upvotes: 1
Reputation: 329
According to the documentation: https://kotlinlang.org/docs/reference/enum-classes.html
The valueOf() method throws an IllegalArgumentException if the specified name does not match any of the enum constants defined in the class.
But, this method should be match with the name of the Enum, so you should be call this method something like this:
val errorType = ErrorType.valueOf("TYPE_B")
you should be call this method with the name
instead of it's parameter
.
Upvotes: 0