I'm trying to do some util class to operate on enums - convert Enums to its special strings representation via interface method. String stored in enum constructors.
public enum StatusEnum implements HumanReadableEnum {
NOT_VERIFIED("Not verified message"),
INVALID("Invalid message"),
APPROVED("Approved message");
private String text;
public String getHumanReadable(){ // single method of HumanReadableEnum
return text;
}
For example instead of HumanReadableEnum[] v = StatusEnum.values()
I want to get List of inner text messages and do it in generic style.
So now I made a toHumanReadableCollection
method:
public class EnumUtils {
public static <CustomEnum extends Enum & HumanReadableEnum, E extends Class<CustomEnum>>
List<String> toHumanReadableCollection(E enumClass){
Enum[] values = enumClass.getEnumConstants();
List<String> humanReadableCollection = new ArrayList<>(values.length);
for (Enum value : values) {
String stringRepresentation = ((CustomEnum)value).getHumanReadable();
humanReadableCollection.add(stringRepresentation);
}
return humanReadableCollection;
}
}
As a result I can do something like:
List<String> messagesOfEnum1 = EnumUtils.toHumanReadableCollection(MyEnum1.class);
List<String> messagesOfEnum2 = EnumUtils.toHumanReadableCollection(MyEnum2.class);
And have code of toHumanReadableCollection
in single place. But I have some doubts about performance.
So could somebody help me to understand the cost of my code? :) Did I touched the reflection?)
toString()
method? \$\endgroup\$