带有驼峰大小写值

时间:2017-10-12 13:26:36

标签: java enums camelcasing

目标是使用驼峰大小写值组合字符串常量。 理想情况下是:public enum Attribute {Measures, MeasuresLevel}。 但是它不符合命名约定:常量名称应为大写。 以下解决方案看起来像数据重复:

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

    private final String value;

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

任何替代方案,建议都非常欢迎。感谢。

2 个答案:

答案 0 :(得分:1)

许多库提供转换为camelcase的实用程序,例如Guava

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

查看番石榴documentation

答案 1 :(得分:0)

将它放入你的枚举

@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;
}
相关问题