HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Listing human-readable enums

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
listinghumanreadableenums

Problem

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 > 
    List toHumanReadableCollection(E enumClass){
        Enum[] values = enumClass.getEnumConstants();
        List 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 messagesOfEnum1 = EnumUtils.toHumanReadableCollection(MyEnum1.class);
List 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?)

Solution

Method header

Your method header can be improved, the generic type E is not necessary, it can instead be written as:

public static  
    List toHumanReadableCollection(Class enumClass){


Type parameters should generally be one-character names, so this would be better as:

public static  
    List toHumanReadableCollection(Class enumClass) {


Compiler warning

You're getting a compiler warning here:

String stringRepresentation = ((CustomEnum)value).getHumanReadable();


All you care about is the getHumanReadable method, which is in the HumanReadableEnum interface, so make that:

String stringRepresentation = ((HumanReadableEnum)value).getHumanReadable();


Naming and interface

HumanReadableEnum should instead be HumanReadable, there's no need to keep Enum in the name, there's no need to restrict the interface to only enums either.

Your questions

Reflection? Yes, this line of code is using reflection:

Enum[] values = enumClass.getEnumConstants();


Time complexity? \$O(n)\$, it is linear to the number of enum constants.

Java 8

In Java 8, you could replace your method with this:

List list = Arrays.stream(StatusEnum.values())
   .map(HumanReadableEnum::getHumanReadable)
   .collect(Collectors.toList());


Or, as a general method, and also incorporating the earlier suggestions:

public static 
  List toHumanReadableCollection(Class enumClass) {
    Enum[] values = enumClass.getEnumConstants();
    return Arrays.stream(values)
       .map(e -> ((HumanReadable)e).getHumanReadable())
       .collect(Collectors.toList());
}


There's often a benefit of using Streams, I suggest you try to learn the Java 8 Stream API at some point.

Code Snippets

public static <CustomEnum extends Enum & HumanReadableEnum> 
    List<String> toHumanReadableCollection(Class<CustomEnum> enumClass){
public static <E extends Enum & HumanReadableEnum> 
    List<String> toHumanReadableCollection(Class<E> enumClass) {
String stringRepresentation = ((CustomEnum)value).getHumanReadable();
String stringRepresentation = ((HumanReadableEnum)value).getHumanReadable();
Enum[] values = enumClass.getEnumConstants();

Context

StackExchange Code Review Q#106339, answer score: 10

Revisions (0)

No revisions yet.