Search code examples
javaenumscamelcasing

Grouped string constants with camel case values


the goal is to have a grouped string constants with camel case values. Ideally would be: public enum Attribute {Measures, MeasuresLevel}. However its is not conform with the naming convention: constant names should be in uppercase. The following solutions looks like a data duplication:

public enum Attribute {
    MEASURES("Measures"), 
    MEASURES_LEVEL("MeasuresLevel");

    private final String value;

    Attribute(String value) {
        this.value = value;
    }
}

Any alternatives, suggestions are very welcome. Thanks.


Solution

  • Put this into your enum

    @Overwrite
    public String toString(){
        String[] share = this.name().split("_");
        String camel = "";
        for(String s : share) {
            camel += s.charAt(0) + s.substring(1).toLowerCase();
        }
        return camel;
    }