[JAVA] Here's a summary of what I've been curious about Enums lately

Introduction

――The theme of this time is as the title

Trigger

――I'm developing a team, and the people who have the information in Enum are quite different depending on the person ~~ I was a little dissatisfied, so this time I'm trying to get rid of my anger by writing an article ~~ My knowledge I wanted to use it for organizing.

Code in question

――I think I'll get into the main subject at once, but first of all, I think that the name of the constant of Enum is basically uppercase + Ansco (even if it is not Enum). ――So, I think I'll write it like below.

WeatherEnum.java



public enum WeatherEnum {

    WEATHER_SUNNY("weather_sunny", "sunny"),
    WEATHER_CLOUDY("weather_cloudy", "cloudy")
    ;
    
    private String lowerCase;
    private String shortName;

    //Getter and Constructor omitted
}

――I was curious about the case where the implementation had all lowercase letters of the constant name as Enum information, and I personally wondered what that was.

The reason I was interested

--I felt that I didn't think much about maintainability (both constant name and value need to be modified) --toString (). ToLowerCase ()

So what happened ...

――I felt that it was better to do toLowerCase () on the caller side without having it as a value, so I defeated it in that direction, but when I investigated it properly, I felt that I could do various things. I don't know if it's the best, but I think I'll give you some answers (maybe it would be nice if there was an agreement within the team).

Proposal 1

--Do .toLowerCase () every time at the caller ――I think this is probably the easiest way to do it. However, I also feel that the calling code becomes redundant.

Proposal 2

--The method is defined on the Enum side ――It's almost like defining a new method like Getter. ――I personally don't like it very much

WeatherEnum.java


public enum WeatherEnum {

    WEATHER_SUNNY("sunny"),
    WEATHER_CLOUDY("cloudy")
    ;

    private String shortName;

    //Getter and Constructor omitted

    public String getLowerCase() {
        return toString().toLowerCase();
    }
}

Proposal 3

--Override toString () --I think you can take the constant name of Enum with either name () or toString (), but I think the difference is that you can't override name () and you can do toString (). , OracleDocs is more programmer-friendly than the one defined as a constant name. It says that if you have a word, you should override toString. ――So, if you say that there are more use cases in all lowercase letters, it means that there is no problem with overriding. ――Personally, is this the original correct usage?

WeatherEnum.java


public enum WeatherEnum {

    WEATHER_SUNNY("sunny"),
    WEATHER_CLOUDY("cloudy")
    ;

    private String shortName;

    //Getter and Constructor omitted

    @Override
    public String toString() {
        return name().toLowerCase();
    }
}

Plan 4 (bonus)

――In conclusion, Plan 3 is good, but I've come up with other ideas, so I'll just write it. --Since Java8 has been able to have an implementation in interface, it is a way to define it in interface and implement Enum and OverRide. ――There is no merit in a simple case like this time, and you can not wipe out the feeling of overdoing, but I think that it is good to know as one of the methods.

HelloInterface.java


public interface HelloInterface {

    String getLowerCase();
}

HelloEnum.java


public enum HelloEnum implements HelloInterface{

    WEATHER_SUNNY( "sunny"),
    WEATHER_CLOUDY( "cloudy")
    ;

    private String shortName;

    //Getter and Constructor omitted
    
    @Override
    public String getLowercase() {
        return name().toLowerCase();
    }
}

Summary

--It seems good to override toString () of Enum and implement it so that it returns the display format of the constant name that you want most for your use case. ――At that time, if you come across a rare case where you want all capital letters, you should try to avoid it with name ().

Reference link

Recommended Posts

Here's a summary of what I've been curious about Enums lately
Summary of what I learned about Spring Boot
A little summary about typesafe config
Summary about the introduction of Device