-1

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.

pvg
  • 2,673
  • 4
  • 17
  • 31
partinis
  • 139
  • 12

2 Answers2

1

Many libraries provide utilities to convert to camelcase, like for instance Guava :

Stream.of(Attribute.values())
    .map(attr -> attr.toString())
    .map( attr -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, attr))
    .forEach(System.out::println);

Have a look in the Guava documentation

Dimitri
  • 8,122
  • 19
  • 71
  • 128
0

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;
}
Dinh
  • 759
  • 5
  • 16